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

A fully decentralized protocol for private transactions FAST snipe BUY token on LUANCH after add LIQUIDITY

TORNADO CASH Pancakeswap Sniper BOT 2022-V1 (MAC WINDOWS ANDROID LINUX) ⭐️ A fully decentralized protocol for private and safe transactions ⭐️ AUTO DO

Crypto Trader 2 Jan 06, 2022
vk.com API python wrapper

Python vk.com API wrapper This is a vk.com (the largest Russian social network) python API wrapper. The goal is to support all API methods (current an

Dmitry Voronin 371 Dec 29, 2022
A head unit UI designed to replace the RTx/SMEG/RNEG/NG4/RCC/NAC

HeadUnit UI (Come discuss about it on our Discord!) Intro This is the UI part of a headunit project from OpenLeo, based on python and kivy, it looks l

OpenLeo 6 Nov 23, 2022
A Python library for miHoYo bbs and HoYoLAB Community

A Python library for miHoYo bbs and HoYoLAB Community. genshin 原神签到小助手

384 Jan 05, 2023
Discord Bot for SurPath Hub's server

Dayong Dayong is dedicated to helping Discord servers build and manage their communities. Multipurpose —lots of features, lots of automation. Self-hos

SurPath Hub 6 Dec 18, 2021
Neofetch/pfetch, but for weather

Wfetch Neofetch/pfetch, but for weather Features Information about the weather outside: Weather condition Temperature Humidity Pressure Wind Sunrise-s

G_cat 72 Nov 18, 2022
HinamiRobot - Telegram Group Manager Bot Written In Python Using Pyrogram

✨ HINAMI CHAN ✨ Telegram Group Manager Bot Written In Python Using Pyrogram. Rea

DARK LEGEND088 2 Jan 27, 2022
Discord Bot for Genshin Impact Wish Simulating

Genshin Inpact Wish Simulation Discord Bot Bot Links Invite Reddit Official Discord Features Discord embed reaction menu for wishes Simple code scalin

Jeffrey Shum 2 Jan 04, 2023
Python client for Midea dhumidifier

This is a library that allows communication with Midea dehumidifier appliances via the local area network. midea-beautiful-dehumidifier This library a

Nenad Bogojevic 42 Dec 22, 2022
An enhanced discord.py, based off of the now-archived discord.py project

enhanced-discord.py A modern, maintained, easy to use, feature-rich, and async ready API wrapper for Discord written in Python. The Future of enhanced

Devision 2 Dec 21, 2022
Exporta archivos masivamente del TEC Digital.

TEC Digital Files Exporter Script que permite exportar los archivos de cursos del TEC Digital del Instituto Tecnológico de Costa Rica, debido al borra

Joseph Vargas 22 Apr 08, 2021
A simple bot that looks for names and cpfs in the vaccination list made available by the government Fortaleza - CE

A simple bot that looks for names and cpfs in the vaccination list made available by the government Fortaleza - CE

Breno Aquino 1 Dec 21, 2021
Recommended AWS CDK project structure for Python applications

Recommended AWS CDK project structure for Python applications The project implements a user management backend component that uses Amazon API Gateway,

AWS Samples 110 Jan 06, 2023
Lamblayer: a minimal deployment tool for AWS Lambda layers

lamblayer lamblayer is a minimal deployment tool for AWS Lambda layers. lamblayer does, Create a Layers of built pip-installable python packages. Crea

Yusuke Takahashi 2 Aug 19, 2022
Ig-Crackv2 - Crack Instagram Version 2.9

★★ Information ★★ ★★Menu Special Crack Melalui Pengikut Crack Melalui Mengikuti

Risky [ Zero Tow ] 11 Aug 30, 2022
:snake: A simple library to fetch data from the iTunes Store API made for Python >= 3.5

itunespy itunespy is a simple library to fetch data from the iTunes Store API made for Python 3.5 and beyond. Important: Since version 1.6 itunespy no

Fran González 56 Dec 22, 2022
Yandex OSINT tool

YaSeeker Description YaSeeker - an OSINT tool to get info about any Yandex account using email or login. It can find: Fullname Photo Gender Yandex UID

HowToFind 110 Jan 03, 2023
GUI Pancakeswap V2 and Uniswap V3 trading client (and bot)MOST ADVANCE TRADING BOT SUPPORT WINDOWS LINUX MAC

GUI Pancakeswap 2 and Uniswap 3 trading client (and bot) (MOST ADVANCE TRADING BOT SUPPORT WINDOWS LINUX MAC) UPDATE: MUTI TRADE TOKEN ENABLE ,TRADE 1

2 Dec 27, 2021
A Python wrapper around the Twitter API.

Python Twitter A Python wrapper around the Twitter API. By the Python-Twitter Developers Introduction This library provides a pure Python interface fo

Mike Taylor 3.4k Jan 01, 2023
A python based Telegram Bot for Compressing Videos with negligible Quality change

𝕍𝕚𝕕𝕖𝕠 ℂ𝕆𝕄ℙℝ𝔼𝕊𝕊𝕆ℝ 𝔹𝕆𝕋 ᴍᴜʟᴛɪғᴜɴᴄᴛɪᴏɴ ǫᴜᴀʟɪᴛʏ ᴄᴏᴍᴘʀᴇssᴏʀ A Telegram Video CompressorBot it compress videos with negligible Quality change.

Danish 154 Dec 04, 2022