Python构建企业微信智能应答机器人(含服务器部署)
有定制化需求可私信联系 |
文章目录
- 简介
- 基本概念
- corpid
- agentid和secret
- touser
- 安装
- 初试
- 获取access_token
- 发送应用消息
- Python高并发服务部署——Nginx+Gunicorn+gevent+Flask+Supervisor
- 接收消息和事件
- 参考文献
简介
目标是开发一个简易机器人,能接收消息并作出回复。
开发条件如下:
- 企业微信超级管理员权限
- 服务器
基本概念
corpid
agentid和secret
touser
安装
pip install requests
初试
发送消息
import json
import urllib.parseimport requestscorpid = 'wwxxxxxxxxxxxxxxxx' # 企业ID
agentid = 1000001 # 应用ID
corpsecret = 'pxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # 应用Secret
touser = 'LxxJxxxKxx' # 接收消息的用户base = 'https://qyapi.weixin.qq.com'# 1.请求access_token
access_token_api = urllib.parse.urljoin(base, '/cgi-bin/gettoken')
params = {'corpid': corpid, 'corpsecret': corpsecret}
response = requests.get(url=access_token_api, params=params).json()
access_token = response['access_token']# 2.发送消息
message_send_api = urllib.parse.urljoin(base, f'/cgi-bin/message/send?access_token={access_token}')
data = {'touser': touser, 'msgtype': 'text', 'agentid': agentid, 'text': {'content': 'Hello World!'}}
response = requests.post(url=message_send_api, data=json.dumps(data)).json()if response['errcode'] == 0:print('发送成功')
else:print(response)
获取access_token
发送应用消息
Python高并发服务部署——Nginx+Gunicorn+gevent+Flask+Supervisor
接收消息和事件
应用管理 → 点击要接收消息的应用 → 接收消息的设置API接收
URL 后面再确定,先随机获取 Token 和 EncodingAESKey
pip install flask
pip install pycryptodome
wget https://raw.githubusercontent.com/sbzhu/weworkapi_python/master/callback/WXBizMsgCrypt3.py
wget https://raw.githubusercontent.com/sbzhu/weworkapi_python/master/callback/ierror.py
vim app.py
import time
import xml.etree.cElementTree as ETfrom flask import Flask, requestfrom WXBizMsgCrypt3 import WXBizMsgCryptsToken = 'xxxx' # 对应上图的Token
sEncodingAESKey = 'xxxx' # 对应上图的EncodingAESKey
sReceiveId = 'xxxx' # 对应企业ID,即corpid
wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sReceiveId)app = Flask(__name__)@app.route('/robot/', methods=['GET', 'POST'])
def robot():msg_signature = request.args.get('msg_signature') # 企业微信加密签名timestamp = request.args.get('timestamp') # 时间戳nonce = request.args.get('nonce') # 随机数echostr = request.args.get('echostr') # 加密字符串# 验证URL有效性if request.method == 'GET':ret, sReplyEchoStr = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr)if ret == 0:return sReplyEchoStrelse:return 'ERR: VerifyURL ret:' + str(ret)# 接收消息if request.method == 'POST':ret, xml_content = wxcpt.DecryptMsg(request.data, msg_signature, timestamp, nonce)if ret == 0:root = ET.fromstring(xml_content)print(xml_content)to_user_name = root.find('ToUserName').textfrom_user_name = root.find('FromUserName').textcreate_time = root.find('CreateTime').textmsg_type = root.find('MsgType').textcontent = root.find('Content').textmsg_id = root.find('MsgId').textagent_id = root.find('AgentID').textprint(content)# return content# 被动回复create_time = timestamp = str(int(time.time()))content = content.replace('吗', '').replace('?', '!').replace('?', '!')sReplyMsg = f'<xml><ToUserName>{to_user_name}</ToUserName><FromUserName>{from_user_name}</FromUserName><CreateTime>{create_time}</CreateTime><MsgType>text</MsgType><Content>{content}</Content><MsgId>{msg_id}</MsgId><AgentID>{agent_id}</AgentID></xml>'ret, sEncryptMsg = wxcpt.EncryptMsg(sReplyMsg, nonce, timestamp)if ret == 0:passelse:return 'ERR: EncryptMsg ret: ' + str(ret)return sEncryptMsgelse:return 'ERR: DecryptMsg ret:' + str(ret)if __name__ == '__main__':app.run()
python app.py
sudo vim /etc/nginx/sites-available/default
server {location ^~ /robot/ {proxy_pass http://127.0.0.1:5000/robot/;}
}
sudo nginx -s reload
参考文献
- 开发前必读 - 企业微信开发者中心
- 获取会话内容 - 企业微信API
- python3-企业微信-会话内容存档-对接Linux_C_SDK-libWeWorkFinanceSdk_C.so
- python对接企业微信_Python对接企业微信会话内容存档功能的实践
- weworkapi_python GitHub
- wework GitHub
- 如何在CentOS中安装apt-get?
- 腾讯云Linux连接ssh失败问题
- Linux安装conda并创建虚拟环境
- uwsgi/uWSGI/WSGI简介
- gunicorn vs uwsgi 性能对比,以flask run为基准
- Flask Web开发教程(十二)生产环境部署,基于gunicorn+nginx+supervisor的方案
- Nginx、Gunicorn在服务器中分别起什么作用?
- Flask教程(十二)项目部署
- 配置默认主页、目录浏览 | Nginx 入门教程
- 基于nginx上传一个简单的Html网页到服务器,并进行访问
- Nginx设置子域名解析
- gevent Documentation
- Url decode UTF-8 in Python
- Python构建企业微信自动消息转发服务端
- 在线 XML 格式化 | 菜鸟工具
- 关于腾讯云服务器上面的Nginx域名配置
Python构建企业微信智能应答机器人(含服务器部署)相关推荐
- python 在企业微信通过群机器人发送消息
1.在企业微信新建一个群,最开始最好只加入自己,方便测试,以免影响他人 在企业微信群昵称处右键鼠标,选择添加群机器人-添加群机器人-新创建一个机器人,如下图所示: 2.添加完群机器人之后,在群的联系人 ...
- 使用 Python 编写的微信智能聊天机器人
编程语言:Python2.7,基于图灵API 首先在图灵机器人官网(http://www.tuling123.com) 注册账号,创建机器人,使用图灵的API接口,实现智能聊天等功能丰富的机器人,图灵 ...
- python web微信应用(三) 微信智能聊天机器人
文章目录 前言 一.webwx 模块介绍 二.微信智能聊天 前言 本篇文章作为系列第三篇文章,将实现一个微信智能聊天机器人: 系列其它文章请参考: python web微信应用(一) 微信协议分析 p ...
- Zabbix配置企业微信群聊机器人告警
转载来源 : Zabbix配置企业微信群聊机器人告警 : https://www.jianshu.com/p/b5b1f92b1f15 最近在给内部使用的zabbix配置告警发送,要求是使用企业微信群 ...
- delphi 企业微信消息机器人_企业微信—群聊机器人
在企业微信群聊机器人接口对接天气API使用过程中,遇到 过一个问题,就是对于嵌套json数据如何进行嵌套的| 一:"msgtype": "text", curl ...
- linux 脚本调用企业微信_shell或python调用企业微信发送消息(实现报警功能)
shell或python调用企业微信发送消息(实现报警功能) 官方文档 注册登陆企业微信 登录企业微信管理端 -> 应用与小程序 -> 应用 -> 自建,点击"创建应用&q ...
- python调用企业微信接口
python调用企业微信接口实现关联添加用户 # --*-- coding: utf-8 --*--import json import urllib2coreID = secret = apisec ...
- Python实现企业微信发送图片
# -*-coding:utf-8 -*- __author__ = 'yangxin_ryan' import requests, json import urllib3 urllib3.disab ...
- 通过python实现企业微信公众号链接+图文推送
背景:通过python实现企业微信公众号链接+图文推送 目的:实现点击即看到内容,用更符合用户查看公众号消息的习惯推送消息 步骤: 1.创建企业微信公众号(应用) 2.确定推送内容(BI报表链接)+标 ...
最新文章
- linux中lvs命令详解,LVS之三:ipvsadm常用管理命令介绍 | 旺旺知识库
- 多IDC GSLB的部署
- 虚拟时代将至:环绕计算才是未来
- C++STL笔记(七):forward list详解
- 设计模式—适配器模式(思维导图)
- HDU 1230解题报告
- LFM回波信号仿真,加汉明窗,可用的matlab代码
- 斐讯 N1 降级、刷机及 Armbian 安装 [2019.7.23]
- Android SDK开发包 国内下载
- java String字符串去除()里的内容
- Windows 时间同步出错
- python生成简单二维码_使用Python生成个性二维码
- Laravel学习笔记汇总——Eloquent Model模型和数据库操作
- 百度地图加载海量标注性能优化策略
- 设置和获取中断向量,很清楚嗷
- Tarena - 基础查询
- Ansys workbench结构线性静力学分析-简支梁分析
- 考研数据结构——(线性表_双链表)
- php实战 --电商网站后台开发 1.1 需求分析
- deepin中安装teams