Python中itchat模块对于操作微信来说是个福音,今天,我们就用Python实现微信自动回复,同时调用图灵机器人,实现智能回复。
具体代码如下:
进群进群:700341555可以获取Python各类入门学习资料!
这是我的微信公众号【Python编程之家】各位大佬用空可以关注下,每天更新Python学习方法,感谢!

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#导入库
import itchat
import requests
import re
抓取网页
def tulingrobot(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
自动回复
@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing', 'Picture'])
def text_reply(msg):
判断消息是不是自己回复
if not msg['FromUserName'] == Name["***"]:#改成自己的微信名字
回复给好友
url = "http://www.tuling123.com/openapi/api?key=d92fd1e8671f4d2487b47f85b118eab6&info="
url = url + msg['Text']
html = tulingrobot(url)
message = re.findall(r'"text":".*?"', html)
reply = eval(message[0].split(':')[1])
return reply
if name == 'main':
itchat.auto_login()
获取自己的UserName
friends = itchat.get_friends(update=True)[0:]
Name = {}
Nic = []
User = []
for i in range(len(friends)):
Nic.append(friends[i]["NickName"])
User.append(friends[i]["UserName"])
for i in range(len(friends)):
Name[Nic[i]] = User[i]
itchat.run()
</pre>
测试运行效果如下:
好了,See you!
网友评论