姓名:赵茄宇 学号:21181214351 学院:广州研究院
转自:https://nbodyfun.blog.csdn.net/article/details/108528026
【嵌牛导读】30行python代码实现微信自动聊天
【嵌牛鼻子】Python,图灵机器人
【嵌牛提问】Python有趣应用
【嵌牛正文】
1.依赖库:
requests(人性化的http请求库)
itchat(网页微信接口库)
2.直接贴代码:
# -*- encoding: utf-8 -*-
"""
@File : chatbot.py
@Author : Joshua
@Contact : froginwe11@163.com
"""
import itchat
import requests
from retry import retry
@retry(tries=3)
def robot_chat(text):
url = "http://www.tuling123.com/robot-chat/robot/chat/759052/CUZW?geetest_challenge=&geetest_validate=&geetest_seccode="
return requests.post(url, json={
"perception": {"inputText": {"text": text}},
"userInfo": {"userId": "demo"}
}).json()['data']['results'][0]['values']['text']
@itchat.msg_register(itchat.content.TEXT, isFriendChat=True)
def auto_reply(msg):
reply = "execuse me?"
try:
reply = robot_chat(msg.text)
except:
pass
finally:
print(f'[In] {msg.text} \t [Out] {reply}')
return reply
itchat.auto_login(hotReload=True)
itchat.run()
3.使用方法:
粘贴代码至chatbot.py文件内
控制台执行pip install requests和pip install itchat
然后继续执行python chatbot.py
————————————————
版权声明:本文为CSDN博主「nbody1996」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44087733/article/details/108528026