基于Python实现微信自动回复功能
作者:Python学习与数据挖掘 时间:2021-10-17 14:01:54
大家好,之前我们使用 WechatPCAPI 做了获取微信好友信息以及查看撤回消息,本文我们再使用 WechatPCAPI 来实现微信自动回复的功能。
实现自动回复的功能,我们需要用到图灵机器人,网址为:http://www.turingapi.com,我们在浏览器中输入上述网址打开,之后点击注册/登录按钮,如下图所示:
打开后如下图所示:
我们接着点击立即注册,就跳转到了注册页,如下图所示:
我们先填写必填信息,填写完之后点击注册按钮即可,注册成功之后便跳到了机器人管理页面,如下所示:
我们点击创建机器人按钮跳转到如下页面:
我们填写完相应信息之后点创建按钮,之后会跳转到机器人设置页面,如下图所示:
我们需要记录下 apikey。
有了 apikey,我们就可以实现自动回复功能了,实现代码如下所示:
import time, logging, random, requests
from queue import Queue
from WechatPCAPI import WechatPCAPI
logging.basicConfig(level=logging.INFO)
queue_recved_event = Queue()
def on_message(msg):
queue_recved_event.put(msg)
# 机器人返回消息
def reply_msg(receive_msg):
apikey = '自己的apikey'
apiurl = 'http://www.tuling123.com/openapi/api?key=%s&info=%s' % (apikey, receive_msg)
result = requests.get(apiurl)
result.encoding = 'utf-8'
data = result.json()
return data['text']
def login():
pre_msg = ''
# 初始化微信实例
wx_inst = WechatPCAPI(on_message=on_message, log=logging)
# 启动微信
wx_inst.start_wechat(block=True)
# 等待登陆成功,此时需要人为扫码登录微信
while not wx_inst.get_myself():
time.sleep(5)
print('登陆成功')
while True:
msg = queue_recved_event.get()
if 'msg::single' in msg.get('type'):
data = msg.get('data')
if data.get('is_recv', False):
msgfrominfo = data.get('msgfrominfo')
if msgfrominfo is not None:
wx_id = msgfrominfo.get('wx_id')
if wx_id != 'weixin':
receive_msg =str(data.get('msgcontent'))
reply = reply_msg(receive_msg)
wx_inst.send_text(to_user=wx_id, msg=reply)
看一下实现效果:
来源:https://blog.csdn.net/weixin_38037405/article/details/125233982
标签:Python,微信,自动,回复
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python list.sort()根据多个关键字排序的方法实现
2021-05-22 03:16:09
老生常谈Python进阶之装饰器
2022-05-20 10:51:23
Python元组常见操作示例
2023-06-28 20:48:26
互联网产品设计师自我介绍
2009-04-16 12:45:00
ASP伪静态页简单教程
2007-09-28 14:35:00
再谈“字符串拼接”的效率
2009-04-30 12:48:00
Python入门基础之变量及字符串
2023-08-08 06:10:11
![](https://img.aspxhome.com/file/2023/0/93480_0s.png)
服务器端控件是如何操作的?
2009-11-01 15:22:00
python内置模块OS 实现SHELL端文件处理器
2023-11-24 16:41:14
![](https://img.aspxhome.com/file/2023/2/63882_0s.png)
ASP连接access和mssql数据库的全能代码
2008-10-12 13:17:00
Python的Django框架中的select_related函数对QuerySet 查询的优化
2022-09-02 06:06:46
![](https://img.aspxhome.com/file/2023/6/87506_0s.jpg)
Python中的测试模块unittest和doctest的使用教程
2021-09-07 05:50:54
深入理解Python中的内置常量
2023-01-21 02:57:47
opencv实现图像校正
2023-12-26 02:07:49
![](https://img.aspxhome.com/file/2023/4/130784_0s.jpg)
基于JS实现动态跟随特效的示例代码
2023-06-30 11:15:16
![](https://img.aspxhome.com/file/2023/5/55865_0s.png)
Python自定义简单图轴简单实例
2021-09-22 22:56:32
![](https://img.aspxhome.com/file/2023/9/121849_0s.png)
PyTorch策略梯度算法详情
2022-12-20 14:35:12
![](https://img.aspxhome.com/file/2023/7/86617_0s.png)
FCKeditor新版本发布,并更名为CKeditor
2009-09-08 13:09:00
![](https://img.aspxhome.com/file/UploadPic/20099/8/ckeditor-60s.jpg)
Python pymsql模块的使用
2023-01-08 15:23:00
![](https://img.aspxhome.com/file/2023/0/114770_0s.gif)
OpenCV机器学习MeanShift算法笔记分享
2021-06-04 18:54:54
![](https://img.aspxhome.com/file/2023/1/121671_0s.jpg)