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

Copier template for solving Advent of Code puzzles with Python

Advent of Code Python Template for Copier This template creates scaffolding for one day of Advent of Code. It includes tests and can download your per

Geir Arne Hjelle 6 Dec 25, 2022
Raphtory-client - The python client for the Raphtory project

Raphtory Client This is the python client for the Raphtory project Install via p

Raphtory 5 Apr 28, 2022
un outil pour bypasser les code d'états HTTP négatif coté client ( 4xx )

4xxBypasser un outil pour bypasser les code d'états HTTP négatif coté client ( 4xx ) Liscence : MIT license Creator Installation : git clone https://g

21 Dec 25, 2022
A telegram bot help you to get stylish fonts and text

Stylish Font Bot 🐿 This is a telegram bot help you to get stylish fonts and text. Config Vars 🤖 API_HASH: Get this value from my.telegram.org. API_K

MSTL updates 1 Nov 08, 2021
A play store search telegram bot

Play-Store-Bot A play store search telegram bot Made with Python3 (C) @FayasNoushad Copyright permission under MIT License License - https://github.c

Fayas Noushad 17 Oct 28, 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
This Instagram app created as a clone of instagram.Developed during Moringa Core.

Instagram This Instagram app created as a clone of instagram.Developed during Moringa Core. AUTHOR By: Nyagah Isaac Description This web-app allows a

Nyagah Isaac 1 Nov 01, 2021
Utility for downloading fanfiction in bulk from the Archive of Our Own

What is this? This is a program intended to help you download fanfiction from the Archive of Our Own in bulk. This program is primarily intended to wo

73 Dec 30, 2022
Wetterdienst - Open weather data for humans

We are a group of like-minded people trying to make access to weather data in Python feel like a warm summer breeze, similar to other projects like rdwd for the R language, which originally drew our

226 Jan 04, 2023
Monitor robot of Apple Store's products, using DingTalk notification.

概述 本项目应用主要用来监测Apple Store线下直营店货源情况,主要使用Python实现。 首先感谢iPhone-Pickup-Monitor项目带来的灵感,同时有些实现也直接使用了该项目的一些代码。 本项目在iPhone-Pickup-Monitor原有功能的基础上去掉了声音通知,但添加了多

Lennon Chin 159 Dec 09, 2022
A self hosted slack bot to conduct standups & generate reports.

StandupMonkey A self hosted slack bot to conduct standups & generate reports. Report Bug · Request Feature Installation Install already hosted bot (Us

Muhammad Haseeb 69 Jan 01, 2023
TM1py is a Python package that wraps the TM1 REST API in a simple to use library.

By wrapping the IBM Planning Analytics (TM1) REST API in a concise Python framework, TM1py facilitates Python developments for TM1. Interacting with T

Cubewise CODE 147 Dec 15, 2022
Use Seaborn to visualize interpret the byte layout of Solana account types

solana-account-vis Use Seaborn to visually interpret the byte layout of Solana account types Usage from account_visualization import generate_account_

Jarry Xiao 15 Aug 25, 2022
Telegram Client and Bot that use Artificial Intelligence to auto-reply to scammers and waste their time

scamminator Blocking a scammer is not enough. It is time to fight back. Wouldn't be great if there was a tool that uses Artificial Intelligence to rep

Federico Galatolo 6 Nov 12, 2022
An analysis of the efficiency of the COVID-19 vaccine

VaccineEfficiency 💉 An analysis of the efficiency of the COVID-19 vaccine 3 Methods 1️⃣ Compare country's vaccination data to number of day- to-day c

Stephanie Younes 1 Dec 10, 2021
Script to get a notification when a product, on Amazon Warehouse, is available within a target price

Amazon_Warehouse_Scraping This script aims to scrape Amazon Warehouse and send an email back if there are products whose price matches with the target

2 Oct 25, 2021
Python wrapper for Xeno-canto API 2.0. Enables downloading bird data with one command line

Python wrapper for Xeno-canto API 2.0. Enables downloading bird data with one command line. Supports multithreading

_zza 9 Dec 10, 2022
StudyLion is a Discord bot that tracks members' study and work time while offering members to view their statistics and use productivity tools such as: To-do lists, Pomodoro timers, reminders, and much more.

StudyLion - Discord Productivity Bot StudyLion is a Discord bot that tracks members' study and work time while offering members the ability to view th

45 Dec 26, 2022
Asca - Antiscam Discord Bot With Python

asca Antiscam Discord Bot Asca moderates scammers and deletes scam messages Opti

11 Nov 01, 2022
Open Source API and interchange format for editorial timeline information.

OpenTimelineIO is currently in Public Beta. That means that it may be missing some essential features and there are large changes planned. During this phase we actively encourage you to provide feedb

Pixar Animation Studios 1.2k Jan 01, 2023