企业微信消息推送的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开源协议

[OSGIFI] - INFORMATION GATHERING TOOL, FROM INSTAGRAM ACCOUNTS.

⚡ OSGIFI THIS TOOL PERMIT YOU TO DISCOVERING & GATHERING INFO FROM INSTAGRAM ACCOUNTS, FOR EXAMPLE: Full Name Verified Account Or Not Private Account

BASILEOLUS 9 Nov 29, 2022
A bot written in python that send prefilled Google Forms. It supports multithreading for faster execution time.

GoogleFormsBot https://flassy.xyz https://github.com/Shawey/GoogleFormsBot Requirements: os (Default) ast (Default) threading (Default) configparser (

Shawey 1 Jul 10, 2022
Github repository started notify 💕

Github repository started notify 💕

4 Aug 06, 2022
Repository to access information of stocks in Bombay Stock Exchange.

BSE Repository to access information of stocks in Bombay Stock Exchange. The code in this repository uses BSE API and conclusions made using the code

1 Nov 13, 2021
SystemSix is an e-Ink "desk accessory" running on a Raspberry Pi. It is a bit of nostalgia that can function as a calendar, display the weather

SystemSix is an e-Ink "desk accessory" running on a Raspberry Pi. It is a bit of nostalgia that can function as a calendar, display the weather, the c

John Calhoun 372 Jan 02, 2023
KalmanFilterExercise - A Kalman Filter is a algorithmic filter that is used to estimate the state of an unknown variable

Kalman Filter Exercise What are Kalman Filters? A Kalman Filter is a algorithmic

4 Feb 26, 2022
A python based all-in-one tool for Google Drive

gdrive-tools A python based all-in-one tool for Google Drive Uses For Gdrive-Tools ✓ generate SA ✓ Add the SA and Add them to TD automatically ✓ Gener

XcodersHub 32 Feb 09, 2022
Asyncevents: a small library to help developers perform asynchronous event handling in Python

asyncevents - Asynchronous event handling for modern Python asyncevents is a small library to help developers perform asynchronous event handling in m

Mattia 5 Aug 07, 2022
An attempt to escape the horrible JIRA editor.

An attempt to escape the horrible JIRA editor. jira_filter.py is a pandoc filter that cleans up some of JIRA's html so that it can be converted to Mar

Stefan Matting 2 Feb 10, 2022
Discord Crypto Payment Cards Selfbot

A Discord selfbot that serves the purpose of displaying text and QR versions of your BTC, LTC & ETH payment information for easy and simple commercial or personal transactions.

2 Apr 12, 2022
Telegram vc userbot

Telegram Vc Userbot Available Commands /ping :- To check whether userbot is up or not /joinvc :- To join vc /leavevc :- To leave vc /join_group :- To

NandyDark 7 Nov 18, 2022
A Python Client for News API

newsapi-python A Python client for the News API. License Provided under MIT License by Matt Lisivick. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRAN

Matt Lisivick 281 Dec 29, 2022
汪汪Bot是一个Telegram Bot,用于帮助你管理一台服务器上的Docker

WangWangBot 汪汪Bot是一个Telegram Bot,用于帮助你管理一台服务器上的Docker运行的Bot。这是使用视频: 部署说明 安装运行 准备 local.env 普通版本 BOT_TOKEN=你的BOT_TOKEN ADMINS=使用,分隔的管理员ID列表 如果你使用doppl

老房东的代码练习册 56 Aug 23, 2022
Modern Desktop Jellyfin Client written in Python and Vue for the UI [WIP]

JellyPlayer Modern Jellyfin Client Installation Install Requirements: Install Python 3 Install dependencies Install node deps for frontend, go to Jell

Prayag Prajapati 57 Dec 12, 2022
Snipe fair coin launches. Contact @dannsniper on telegram for whitelist

Pancakeswap-sniper Pancakeswap Sniper bot Full version of Pancakeswap sniping bot used to snipe during fair coin launches. With advanced options and a

36 Nov 01, 2021
Discord Selfbot, 90+ commands

Setting the bot up. STEP 1: copy the directory yook.club selfbot was downloaded and extracted into, open cmd and type "cd " then paste. STEP 2: python

yook 1 Dec 12, 2021
:snake: Python SDK to query Scaleway APIs.

Scaleway SDK Python SDK to query Scaleway's APIs. Stable release: Development: Installation The package is available on pip. To install it in a virtua

Scaleway 114 Dec 11, 2022
Solcast rooftop api for HA

Solcast Solar Home Assistant(https://www.home-assistant.io/) Component This custom component integrates the Solcast API into Home Assistant. Modified

Greg 1 Oct 11, 2021
Tglogging - A python package to send your app logs to a telegram chat in realtime

Telegram Logger A simple python package to send your app logs to a telegram chat

SUBIN 60 Dec 27, 2022
Un bot leggero basato su py-cord facile da hostare sul cloud

GalbiBot Un bot leggero basato su py-cord facile da hostare sul cloud Guida installazione su una macchina Per far funzionare il bot devi aver installa

Galbaninoh 2 Oct 21, 2022