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.1.tar.gz (124.8 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: wecom_bot_svr-0.1.1.tar.gz
  • Upload date:
  • Size: 124.8 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.1.tar.gz
Algorithm Hash digest
SHA256 cd840da81c107a0a3fcb3ffecefed933e0cddf8f8f9a9dc50527ed385ae0cc93
MD5 552bca98537e21f7f760a5768192ba80
BLAKE2b-256 9d7a550cbad4b373d46787d1dd9ba105d0b7ca6c12dfdc140553339f28e87e55

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wecom_bot_svr-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b8b424c8a8f121673a47cf0456c3fdc91204807d726c2cc1b69e9885fd1e7c5
MD5 61af626dd5c8387109d14a048fe104a6
BLAKE2b-256 af086ad23bb106b59b4de159f36ca207fa6b0c01c27d54930721d400a071ee86

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