有定制化需求可私信联系

文章目录

  • 简介
  • 基本概念
    • corpid
    • agentid和secret
    • touser
  • 安装
  • 初试
  • 获取access_token
  • 发送应用消息
  • Python高并发服务部署——Nginx+Gunicorn+gevent+Flask+Supervisor
  • 接收消息和事件
  • 参考文献

简介

目标是开发一个简易机器人,能接收消息并作出回复。

开发条件如下:

  • 企业微信超级管理员权限
  • 服务器

基本概念

corpid

我的企业 → 企业ID

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

access_token 是调用企业微信 API 的凭证


发送应用消息

Python高并发服务部署——Nginx+Gunicorn+gevent+Flask+Supervisor

服务部署具体流程点标题

Linux 系统分两种:

  1. RedHat 系列:Redhat、Centos、Fedora 等,包管理工具是 yum
  2. Debian 系列:Debian、Ubuntu 等,包管理工具是 apt-get

如腾讯云的 Ubuntu 服务器

设置安全组

接收消息和事件

应用管理 → 点击要接收消息的应用 → 接收消息的设置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

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

Nginx 配置

sudo vim /etc/nginx/sites-available/default

关键内容如下

server {location ^~ /robot/ {proxy_pass http://127.0.0.1:5000/robot/;}
}

重载 Nginx

sudo nginx -s reload

用接口调试工具进行调试:建立连接 → 测试回调模式

出现成功字样后,上面的“接收消息服务器配置”点保存

效果

参考文献

  1. 开发前必读 - 企业微信开发者中心
  2. 获取会话内容 - 企业微信API
  3. python3-企业微信-会话内容存档-对接Linux_C_SDK-libWeWorkFinanceSdk_C.so
  4. python对接企业微信_Python对接企业微信会话内容存档功能的实践
  5. weworkapi_python GitHub
  6. wework GitHub
  7. 如何在CentOS中安装apt-get?
  8. 腾讯云Linux连接ssh失败问题
  9. Linux安装conda并创建虚拟环境
  10. uwsgi/uWSGI/WSGI简介
  11. gunicorn vs uwsgi 性能对比,以flask run为基准
  12. Flask Web开发教程(十二)生产环境部署,基于gunicorn+nginx+supervisor的方案
  13. Nginx、Gunicorn在服务器中分别起什么作用?
  14. Flask教程(十二)项目部署
  15. 配置默认主页、目录浏览 | Nginx 入门教程
  16. 基于nginx上传一个简单的Html网页到服务器,并进行访问
  17. Nginx设置子域名解析
  18. gevent Documentation
  19. Url decode UTF-8 in Python
  20. Python构建企业微信自动消息转发服务端
  21. 在线 XML 格式化 | 菜鸟工具
  22. 关于腾讯云服务器上面的Nginx域名配置

Python构建企业微信智能应答机器人(含服务器部署)相关推荐

  1. python 在企业微信通过群机器人发送消息

    1.在企业微信新建一个群,最开始最好只加入自己,方便测试,以免影响他人 在企业微信群昵称处右键鼠标,选择添加群机器人-添加群机器人-新创建一个机器人,如下图所示: 2.添加完群机器人之后,在群的联系人 ...

  2. 使用 Python 编写的微信智能聊天机器人

    编程语言:Python2.7,基于图灵API 首先在图灵机器人官网(http://www.tuling123.com) 注册账号,创建机器人,使用图灵的API接口,实现智能聊天等功能丰富的机器人,图灵 ...

  3. python web微信应用(三) 微信智能聊天机器人

    文章目录 前言 一.webwx 模块介绍 二.微信智能聊天 前言 本篇文章作为系列第三篇文章,将实现一个微信智能聊天机器人: 系列其它文章请参考: python web微信应用(一) 微信协议分析 p ...

  4. Zabbix配置企业微信群聊机器人告警

    转载来源 : Zabbix配置企业微信群聊机器人告警 : https://www.jianshu.com/p/b5b1f92b1f15 最近在给内部使用的zabbix配置告警发送,要求是使用企业微信群 ...

  5. delphi 企业微信消息机器人_企业微信—群聊机器人

    在企业微信群聊机器人接口对接天气API使用过程中,遇到 过一个问题,就是对于嵌套json数据如何进行嵌套的| 一:"msgtype": "text", curl ...

  6. linux 脚本调用企业微信_shell或python调用企业微信发送消息(实现报警功能)

    shell或python调用企业微信发送消息(实现报警功能) 官方文档 注册登陆企业微信 登录企业微信管理端 -> 应用与小程序 -> 应用 -> 自建,点击"创建应用&q ...

  7. python调用企业微信接口

    python调用企业微信接口实现关联添加用户 # --*-- coding: utf-8 --*--import json import urllib2coreID = secret = apisec ...

  8. Python实现企业微信发送图片

    # -*-coding:utf-8 -*- __author__ = 'yangxin_ryan' import requests, json import urllib3 urllib3.disab ...

  9. 通过python实现企业微信公众号链接+图文推送

    背景:通过python实现企业微信公众号链接+图文推送 目的:实现点击即看到内容,用更符合用户查看公众号消息的习惯推送消息 步骤: 1.创建企业微信公众号(应用) 2.确定推送内容(BI报表链接)+标 ...

最新文章

  1. linux中lvs命令详解,LVS之三:ipvsadm常用管理命令介绍 | 旺旺知识库
  2. 多IDC GSLB的部署
  3. 虚拟时代将至:环绕计算才是未来
  4. C++STL笔记(七):forward list详解
  5. 设计模式—适配器模式(思维导图)
  6. HDU 1230解题报告
  7. LFM回波信号仿真,加汉明窗,可用的matlab代码
  8. 斐讯 N1 降级、刷机及 Armbian 安装 [2019.7.23]
  9. Android SDK开发包 国内下载
  10. java String字符串去除()里的内容
  11. Windows 时间同步出错
  12. python生成简单二维码_使用Python生成个性二维码
  13. Laravel学习笔记汇总——Eloquent Model模型和数据库操作
  14. 百度地图加载海量标注性能优化策略
  15. 设置和获取中断向量,很清楚嗷
  16. Tarena - 基础查询
  17. Ansys workbench结构线性静力学分析-简支梁分析
  18. 考研数据结构——(线性表_双链表)
  19. php实战 --电商网站后台开发 1.1 需求分析
  20. deepin中安装teams

热门文章

  1. 【附源码】计算机毕业设计java养老院管理信息系统设计与实现
  2. 第五章 数字滤波器的基本结构之三
  3. Bilibili Helper - 哔哩哔哩弹幕网辅助扩展插件
  4. 阿里云ECS搭建frp服务器实现黑群晖 Nas 内网穿透
  5. pta 循环单链表的删除(java)
  6. 前端生成txt文件并下载
  7. 南理工计算机学院吉祥物,我们想要一个吉祥物,就差一个设计者了!
  8. RSA算法加解密的C语言实现
  9. android 按钮3d效果图,android.graphics.Camera 实现简单的3D效果
  10. 用cocos2d-x模拟单摆运动的程序