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

Bancos de Dados Relacionais (SQL) na AWS com Amazon RDS.

Bancos de Dados Relacionais (SQL) na AWS com Amazon RDS Explorando o Amazon RDS, um serviço de provisionamente e gerenciamento de banco de dados relac

Lucas Magalhães 1 Dec 05, 2021
A python SDK for interacting with quantum devices on Amazon Braket

Amazon Braket Python SDK The Amazon Braket Python SDK is an open source library that provides a framework that you can use to interact with quantum co

Amazon Web Services 213 Dec 14, 2022
Python wrapper for the Sportradar APIs ⚽️🏈

Sportradar APIs This is a Python wrapper for the sports APIs provided by Sportradar. You'll need to sign up for an API key to use the service. Sportra

John W. Miller 39 Jan 01, 2023
Invites link generator for telegram(made for channel referral links)

InviteLinkGen Invites link generator for telegram(for channel referral links) made for @HelakuruEsana channel Spotify Giveaway

Jaindu Charindith 7 Feb 01, 2022
This code will guide you on how you can make your own Twitter Bot.

This code will guide you on how you can make your own Twitter Bot. This bot retweets, and likes to tweet with a particular word, here Python3.

Kunal Diwan 1 Oct 14, 2022
Roaster - this gui app + program bundle roasts.

Roaster - this gui app + program bundle roasts.

Harsh ADV) 1 Jan 04, 2022
Housing Price Prediction Using Machine Learning.

HOUSING PRICE PREDICTION USING MACHINE LEARNING DESCRIPTION Housing Price Prediction Using Machine Learning is to predict the data of housings. Here I

Shreya Shree Padhi 1 Aug 03, 2022
This is a very simple botnet with a CnC server, made by me. Feel free to change anything

This is a very simple botnet with a CnC server, made by me. Feel free to change anything

8 Nov 12, 2022
Easy to use Google Pub/Sub

Relé makes integration with Google PubSub straightforward and easy. Motivation and Features The Publish-Subscribe pattern and specifically the Google

Mercadona 188 Jan 06, 2023
Python tool to Check running WebClient services on multiple targets based on @leechristensen

WebClient Service Scanner Python tool to Check running WebClient services on multiple targets based on @tifkin_ idea. This tool uses impacket project.

Pixis 153 Dec 28, 2022
Connects to a local SenseCap M1 Helium Hotspot and pulls API Data.

sensecap_api_checker_HELIUM Connects to a local SenseCap M1 Helium Hotspot and pulls API Data.

Lorentz Factr 1 Nov 03, 2021
An open-source Discord Nuker can be used as a self-bot or a regular bot.

How to use Double click avery.exe, and follow the prompts Features Important! Make sure to use [9] (Scrape Info) before using these, or some things ma

Exortions 3 Jul 03, 2022
A simple script that will watch a stream for you and earn the channel points.

Credits Main idea: https://github.com/gottagofaster236/Twitch-Channel-Points-Miner Bet system (Selenium): https://github.com/ClementRoyer/TwitchAutoCo

Alessandro Maggio 1.1k Jan 08, 2023
Bancos de Dados Relacionais (SQL) na AWS com Amazon RDS

Bancos de Dados Relacionais (SQL) na AWS com Amazon RDS Repositório para o Live Coding DIO do dia 24/11/2021 Serviços utilizados Amazon RDS AWS Lambda

Cassiano Ricardo de Oliveira Peres 4 Jul 30, 2022
SmartFile API Client (Python).

A SmartFile Open Source project. Read more about how SmartFile uses and contributes to Open Source software. Summary This library includes two API cli

SmartFile 19 Jan 11, 2022
Free Game Download Client

XGames Free Game Download Client В проекте была использована библиотека igruha а также PyQt5 WARN ⚠️ Возможно потребуется скачать и установить vc_redi

LORD_CODE 3 Jun 25, 2022
An Advance Discord Generator Written in python Verified Email and Phone Number For Free!

Intro An Advance Discord Generator Written in python It can generate nearly fully verified tokens USAGE put server invite code inside ( invitecode = "

36 May 02, 2022
Powerful Telegram Maintained UserBot in Telethon

Fire-X UserBot The Awaited Bot Fire-X userbot The Most Powerful Telegram Userbot. This Userbot is Safe to use in Your Telegram Account. It is not like

22 Oct 21, 2022
Simplebot-tg - Telegram/DeltaChat Bridge with python

simplebot_tg Telegram/DeltaChat Bridge, is a plugin for https://github.com/simpl

Arián Díaz Cruz 1 Dec 30, 2021
The Python version of the official Discord bot for the Astura Studios Discord community server.

About Astura (Python version) is the official Discord bot for the Astura Studios Discord community server developed and maintained by Ascendus and the

Ascendus 1 Apr 21, 2022