Skip to main content

WeCom Bot Callback Server Simple Framework

Project description

企业微信机器人回调服务

1. 介绍

本项目是一个基于 Flask企业微信机器人回调功能的接口服务简单框架 。使用者只需要实现两个简单的函数,即可快速搭建一个企业微信机器人回调功能的接口服务。

实现过程参考企业微信机器人回调功能说明文档 ,以及相关文档中指引使用的加解密脚本

2. 使用

2.1 安装

python3 -m pip install wecom_bot_svr

2.1 配置回调相关参数

在企业微信,找到机器人,点击「点击配置」,配置进去准备使用的URL,并自动产生对应的 Token 和 Key:

配置回调

这里的 Token 和 AesKey 是后续会使用的参数。 此时点击保存不能生效,因为企业微信会校验服务返回是否正常。 框架已经实现了校验的逻辑,但是需要先部署服务。

为了安全起见,Token 和 AesKey 不要直接写在代码中,建议通过环境变量传入:

WX_BOT_TOKEN=xxxxx
WX_BOT_AES_KEY=xxxxx
WX_BOT_CORP_ID=xxxxx

2.2 实现回调

参考 demo.py 中的实现,主要步骤:

  • 创建Server对象,如果 token, aes_key, corp_id 打算按照环境变量的方式传入,则这里直接不用填最后三个参数。
  • 定义两个函数:消息处理函数、事件处理函数
  • 将两个函数注册到 Server 对象中
  • 启动服务
import wecom_bot_svr
token = "xxxxxxx"
aes_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
corp_id = ""
host = "0.0.0.0"
port = 5001
server = wecom_bot_svr.Server("wx_bot", host, port, token=token, aes_key=aes_key, corp_id=corp_id)


def msg_handler(user_info, msg_type, content, xml_tree):
    print(user_info, msg_type, content)
    return "hello"


def event_handler(user_info, event_type, xml_tree):
    print(user_info, event_type)
    return "hello"


server.set_message_handler(msg_handler)
server.set_event_handler(event_handler)
server.run()

3. 更多消息处理示例

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wecom_bot_svr-0.1.0.tar.gz (124.7 kB view details)

Uploaded Source

Built Distribution

wecom_bot_svr-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file wecom_bot_svr-0.1.0.tar.gz.

File metadata

  • Download URL: wecom_bot_svr-0.1.0.tar.gz
  • Upload date:
  • Size: 124.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for wecom_bot_svr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 321c8d8af387dce3ea5d2fc6d2f5b4f51a53e02e62a4010f4c7ecdf03338e9f0
MD5 3fd3cee0e0d3f220583663aab11c78fe
BLAKE2b-256 04c1cd1e1f2e5c3386b23908a1f96762c1753833bef496e625bfa3c96c9be0c4

See more details on using hashes here.

File details

Details for the file wecom_bot_svr-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wecom_bot_svr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for wecom_bot_svr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 537cca954df14ffa5780be031ef373dfa850bec985387a890567bba5d59de1d5
MD5 3096edb12774825876144c2d5ef0f8e1
BLAKE2b-256 9808b1b673425f8bd4872f1a651c95938ae7053278387611ee6cc1789e60928c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page