企业微信消息推送的python封装接口,让你轻松用python实现对企业微信的消息推送

Overview

👋 corpwechat-bot是一个python封装的企业机器人&应用消息推送库,通过企业微信提供的api实现。

利用本库,你可以轻松地实现从服务器端发送一条文本、图片、视频、markdown等等消息到你的微信手机端,而不依赖于其他的第三方应用,如ServerChan

如果喜欢该项目,记得给个star,让更多人能够看到本项目 ♥️

为什么要使用corpwechat-bot?

corpwechat-bot的定位是让用户随时随地了解服务器端程序的运行结果,同类型的方法有很多,例如邮件,钉钉(这两个的消息推送我在我的另一个库cptools中实现了,有兴趣的可以去看看),bark等。但对于一般人来说,最方便的接收方式还是微信,但个人微信并不提供给开发者API(以前可以用网页微信,现在被禁了)。

拥有同样功能比较知名的有:ServerChan,之前提供公众号模板消息推送,但由于微信官方原因,在21年4月底即将下线,因此推出了Turbo版,支持企业微信消息推送。我在使用过之后发现几个主要问题 🙋 ,并与corpwechat-bot进行了对比:

对比项 ServerChan corpwechat-bot
消息推送限制 😥 普通用户消息推送次数限制(每天5次,会员不限) ☺️ corpwechat-bot直接和企业微信对接,消息发送仅受微信官方限制(每分钟20条)
个人信息安全性 😩 个人企业号、应用id和密钥由ServerChan云端存储,所有发送的消息也经过ServerChan转发(先到ServerChan后台,再由ServerChan发送到微信) 😇 corpwechat-bot本身不存储任何用户的密钥(由用户本地保存),所有消息也是直接发送到企业微信后台,保证数据安全性
创建、使用便捷性 😣 主要使用步骤:
1. 用户注册企业微信
2. 创建ServerChan应用
3. 提交企业号、应用id、应用密钥到ServerChan,获取sendKey
4. 利用sendKey发送http请求到指定url完成消息发送
😉 主要使用步骤:​
1. 用户注册企业微信
2. 创建个人消息推送应用,获取相应密钥
3. 一条命令pip install -U corpwechatbot安装
4. 利用corpwechatbot的消息发送接口发送消息
消息类型支持 😔 仅支持title+description(支持markdown)的消息发送格式​ 😋 支持文本、markdown,图片、语音、视频、文件、卡片等多个企业微信提供的消息类型​
开源 😢 ​不开源,所有代码均由ServerChan自身管控 😎 开源在Github​,任何人可以依据源代码添加定制自己想要的功能,或贡献自己的一份力到开源项目中

特别说明:虽然上面的对比偏向于突出corpwechat-bot的优势,但并没有刻意贬低ServerChan的意思,作为一个公开的服务,ServerChan的这些特性都是可以被接受的,同时ServerChan也是一款非常优秀的消息推送工具(我之前一直在使用),支持各大类型的消息推送接口(企业微信、钉钉、bark等等),但考虑到上面的几个特性,所以我选择了自己定制并开源了corpwechat-bot,希望给和我一样想法的用户多一份选择,如果你觉得上面的特性切中了你的需求点,麻烦给作者一个小小的star以资鼓励吧 🙏 。未来corpwechat-bot还会添加更多的功能和特性,详情查看Todo,当然你也可以在issue中提出你的合理需求,或提交PR来帮助corpwechat-bot做的更好!!!

ToC

QuickStart

注意,本项目依赖于企业微信创建群聊机器人或应用,要想实现需要先注册一个属于你自己的企业微信号(个人免费),这十分简便,参照官方网址 即可

当你有了企业微信后,你还需要做一些配置,根据你自身的需求来做选择:

  • 应用消息推送 :应用消息推送需要在企业微信中创建一个第三方应用,参照教程
  • 群聊机器人消息推送:群聊机器人消息推送需要在你已有的企业群中添加一个机器人,然后获取相应的机器人keywebhook最后面),参照教程

当确定你的配置可用后(企业微信后台尝试发送消息看手机上能否接收到),安装corpwechatbot到你的pc中,只需要一条命令:

pip install -U corpwechatbot

下面进行消息推送:

  • 应用消息推送:发送一条文本消息到你设置的应用,在手机个人微信上查看接收
from corpwechatbot.app import AppMsgSender

app = AppMsgSender(corpid='',  # 你的企业id
                   corpsecret='',  # 你的应用凭证密钥
                   agentid='')   # 你的应用id
app.send_text(content="如果我是DJ,你会爱我吗?")

推送结果

img.png

  • 动图演示

  • 群聊机器人消息推送:发送一条文本消息到你设置了机器人的群聊
from corpwechatbot.chatbot import CorpWechatBot

bot = CorpWechatBot(key='')  # 你的机器人key,通过群聊添加机器人获取

bot.send_text(content='Hello World')

推送结果:

  • 动图演示

如果你想要更多的使用技巧,请查看Usage

Features

目前实现了两种推送消息方式,应用消息推送群聊机器人消息推送,并且在此基础上,添加了终端一条命令式上述两种消息的推送,具体如下:

  • 应用消息推送:该推送会直接传至你的个人微信上,你会像收到好友消息一样收到通知信息,不需要安装企业微信,具体包括:

    • 文本消息: 最普通的消息,文字内容,最长不超过2048个字节
    • 图片消息:发送一张图片,可选jpg,png,大小不超过2MB,目前仅支持通过图片路径发送.
    • 语音消息:发送一条语音,大小不超过2MB,时长不超过60s,必须是.amr格式
    • 视频消息:发送一段视频,大小不超过10MB,必须是.mp4格式
    • 普通文件:其他类型的文件,大小不超过20MB(不小于5字节)
    • markdown消息:传输markdown类型消息
    • 图文消息:图片文字形式,带有跳转链接,适合做推广
    • mpnews图文消息:有专属页面的图文消息,支持html标签内容
    • 文本卡片消息:以卡片形式呈现的文本,包含跳转链接
    • 小程序通知消息:应用需绑定小程序才可使用,尚未实现
    • 任务卡片消息:允许用户点击做出相应反馈的卡片,需绑定回调函数,尚未实现
  • 群聊机器人消息推送:该推送仅会发送消息到企业微信群聊中,经测试,个人微信的企业群聊不会收到机器人发送的消息,因此要收到消息需安装企业微信,具体包括:

    • 文本消息:普通文字消息,最长不超过2048个字节
    • 图片消息:图片大小不超过2M
    • 图文消息:图片文字形式,带有跳转链接,适合打广告
    • 文件消息:发送单个文件到群聊,大小在5B~20MB之间
  • 终端一条命令式消息推送:不需要写额外的python代码,直接在终端输入一条命令cwb -t='hello world'即可推送消息到手机微信上,具体参考Usage

Usage

⚠️ 当第一种安装方式失效时,请尝试第二种

  • 安装
pip install -U corpwechatbot

或者

git clone https://github.com/GentleCP/corpwechat-bot.git
cd corpwechat-bot
pip install .
  • 使用

点此查看详细使用教程

ChangeLog

你可以在ChangeLog中查看最新版本提交的功能和bug修复信息 点此查看ChangeLog

Todo

后续版本待更新的内容:

  • fix:token存储位置修改,初始设置保存到本地site-packages(后期将其移到site-packages/corpwechatbot目录下,方便统一)
  • docs:readme和usage更新
  • feat更智能的调用接口(如所有发送用同一接口send,根据传入参数的不同自动选择)
  • feat:终端快捷使用,一行命令式消息发送,例如corpwechatbot -s "hello world"直接发送一条文本消息
  • refactor:核心代码优化与重构
  • docs : 添加QuickStart
  • feat添加对应用消息发送到企业微信群聊的支持
  • feat:添加应用消息任务卡片消息推送的功能(小程序消息暂不考虑)
  • feat:允许将企业微信配置信息存储到本地文件读取
  • fix: 修复在指定标签和部门后依然默认发送给全体成员的问题
  • feat: 添加装饰器发送消息支持
  • feat: 支持多个第三方应用消息推送
  • feat: 添加对mpnews的发送支持

Author

@GentleCP

Contibutors

License

本项目遵守GPL v3开源协议

Python bot for send videos of a Youtube channel to a telegram group , channel or chat

py_youtube_to_telegram Usage: If you want to install ytt and use it, run this command: sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/nima

Nima Fanniasl 8 Nov 22, 2022
A simple discord tool that translates english to either spanish, german or french and sends it. Free to rework but please give me credit.

discord-translator A simple discord tool that translates english to either spanish, german or french and sends it. Free to rework but please give me c

TrolledTooHard 2 Oct 04, 2021
Credit Card And SK Checker Written In Python

Credit Card And SK Checker Written In Python

Rimuru Tempest 57 Jan 08, 2023
Herramienta para transferir eventos de Sucuri WAF hacia Azure Monitor Log Analytics.

Transfiere eventos de Sucuri hacia Azure LogAnalytics Script para transferir eventos del Sucuri Web Application Firewall (WAF) hacia Azure LogAnalytic

CSIRT-RD 1 Dec 22, 2021
A cracking tool of Xiaomi Dr AI (Archytas / Archimedes)

Archytas Tool 我们强烈抵制闲鱼平台上未经授权的刷机服务! 我对本人之前在程序中为防止违规刷机服务添加未生效的格机代码感到抱歉,在此声明此过激行为与 Crack Mi Dr AI Team 无关,并将程序开源。 A cracking tool of Xiaomi Dr AI (Archy

rponeawa 5 Oct 25, 2022
A python script that automatically farms the Discord bot 'Dank Memer'.

Dank Farmer A python script that automatically farms the Discord bot 'Dank Memer'. Requirements pynput Disclaimer DO NOT use if you are not willing to

2 Dec 30, 2021
Python API to interact with Uwazi

Python Uwazi API Quick Start To use the API install the requirements pip3 install -r requirements.txt and use it like this: uwazi_adapter = UwaziAdap

HURIDOCS 2 Dec 16, 2021
Read manga from your favourites websites on telegram.

tg-manga-bot Read manga from your favourites websites on telegram. Current Development Bot @idkpythonbot Telegram Channel tg_manga_bot Commands start

Daniel Rivero 41 Dec 22, 2022
A Powerfull Userbot Telegram PandaX_Userbot, Vc Music Userbot + Bot Manager based Telethon

Support ☑ CREDITS THANKS YOU VERRY MUCH FOR ALL Telethon Pyrogram TeamUltroid TeamUserge CatUserbot pytgcalls Dan Lainnya

22 Dec 25, 2022
TG-Streaming-bot - TG Simple Streaming bot

TG Simple Streaming bot telegram video straming bot 🎚️ Features Play youtube li

HyDrix 4 May 05, 2022
Instagram bot that upload images for you which scrape posts from 9gag meme website or other Instagram users , which is 24/7 Automated Runnable.

Autonicgram Automates your Instagram posts by taking images from sites like 9gag or other Instagram accounts and posting it onto your page. Features A

Mastermind 20 Sep 17, 2022
Telegram Bot to Connect Strangers

Telegram Bot to Connect Strangers How to Run Set your telegram bot token as environment variable TELEGRAM_BOT_TOKEN: export TELEGRAM_BOT_TOKEN=your_t

PyTopia 12 Dec 24, 2022
A solution designed to extract, transform and load Chicago crime data from an RDS instance to other services in AWS.

This project is intended to implement a solution designed to extract, transform and load Chicago crime data from an RDS instance to other services in AWS.

Yesaswi Avula 1 Feb 04, 2022
A repository of publicly verifiable token Sale contracts

Token-Sale-Plutus-Contract A repository of publicly verifiable token sale and royalty contracts. This will be the storage solution since it is easily

Logical Mechanism 29 Aug 18, 2022
Tools for use in DeFi. Impermanent Loss calculations, staking and farming strategies, coingecko and pancakeswap API queries, liquidity pools and more

DeFi open source tools Get Started Instalation General Tools Impermanent Loss, simple calculation Compare Buy & Hold with Staking and Farming Complete

Juan Pablo Pisano 467 Jan 08, 2023
Fetch information about a public Google document.

xeuledoc Fetch information about any public Google document. It's working on : Google Docs Google Spreadsheets Google Slides Google Drawning Google My

Malfrats Industries 655 Jan 03, 2023
A method to check whether a Discord user is using the client or not.

Discord Captcha Method This is an example, of a verification trough a check, if the user loads the picture send with the verification-message. This ma

Julien 2 Jan 19, 2022
A basic API to scrape Craigslist.

CLAPI A basic API to scrape Craigslist. Most useful for viewing posts across a broad geographic area or for viewing posts within a specific timeframe.

45 Jan 05, 2023
Charged's cogs for Red!

Light-Cogs Charged's cogs for Red! Read below for more information. Features and Cogs TechInfo Lots of commands helping you and your devices! Extended

Charged 2 Jan 07, 2022
A web app via which users can buy and sell stocks using virtual money

finance Virtual Stock Trader. A web app via which users can buy and sell stocks using virtual money. All stock prices are real and provided by IEX. Fe

Kiron Deb 0 Jan 15, 2022