Skip to main content

WeCom Bot Callback Server Simple Framework

Project description

企业微信机器人回调服务

1. 介绍

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

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

2. 使用

2.1 安装

python3 -m pip intall 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.0.1.tar.gz (127.5 kB view details)

Uploaded Source

Built Distribution

wecom_bot_svr-0.0.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wecom_bot_svr-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2611cd25ceac2c980e8d8488f952eff9ecd9a22abdd2ec31e2a30e31062c1684
MD5 db1a34528b5d91ff346a03543962f25b
BLAKE2b-256 d3836fdf780466c611d384001b4561e30f76525093f2c3d53842b969efdbe6d5

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for wecom_bot_svr-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 553077bcc2905c9af3bb6caa1d7ba0548e288d0be6790f027582910d1c6e145f
MD5 7a71c4d9019525ed89eb2a727da8bba0
BLAKE2b-256 0713a8ec4f9737a978b290a9e080dc29d3ce3c1e7f2f0af90f07cadb1246e38c

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