炸裂!接入图灵机器人后,python实现微信智能回复,告别尬聊
作者:互联网
已经接入图灵机器人
每天有500次的自动回复机会
如果不够,自己注册,写在代码里面
如果对方发的是表情,视频,可以自动下载
执行方法:
建议在命令行:python 文件名
截图如下:
会在运行的脚本的统计目录下生成日志:
from itchat.content import *
import random
import requests
import itchat
import time
apiUrl = 'http://www.tuling123.com/openapi/api'
KEY1 = '988cc14521094fbca1f9c0115dbb6b60'
KEY2 = 'dc4312db600b4c41b4edaf381c23559a'
KEY3 = 'ae652fb5fc7d4ce9b78e8edb32ec6b5a'
KEY4 = '221cb1ac22f04de985a72c1cfbe7e73a'
KEY5 = '14d50167b71547349cd6e4456dab064e'
def get_response(msg):
# 这里实现与图灵机器人的交互
# 构造了要发送给服务器的数据
data = {
'key': KEY1,
'info': msg,
'userid': 'wechat-robot'
}
r1 = requests.post(apiUrl, data=data).json()
if r1["code"] == 100000:
res = r1["text"]
else:
data = {
'key': KEY2,
'info': msg,
'userid': 'wechat-robot'
}
r2 = requests.post(apiUrl, data=data).json()
if r2["code"] == 100000:
res = r2.get('text')
else:
data = {
'key': KEY3,
'info': msg,
'userid': 'wechat-robot'
}
r3 = requests.post(apiUrl, data=data).json()
if r3["code"] == 100000:
res = r3.get('text')
else:
data = {
'key': KEY4,
'info': msg,
'userid': 'wechat-robot'
}
r4 = requests.post(apiUrl, data=data).json()
if r4["code"] == 100000:
res = r4.get('text')
else:
data = {
'key': KEY5,
'info': msg,
'userid': 'wechat-robot'
}
r5 = requests.post(apiUrl, data=data).json()
if r5["code"] == 100000:
res = r5.get('text')
return res
def rand():
# 生成随机数,浮点类型
a = random.uniform(3, 6)
# 控制随机数的精度round(数值,精度)
b = round(a, 1)
return b
@itchat.msg_register(TEXT) # 如果收到的信息是文本类型
def text_reply(msg):
friend = itchat.search_friends(userName=msg['FromUserName'])["RemarkName"]
received_message = "{} : {}".format(friend, msg["Text"])
print(received_message)
with open("log_.txt", "a+", encoding="utf8") as f:
f.write(str(received_message) + "\n")
res = get_response(msg["Text"]) # 图灵机器人回复的内容
interval = rand()
reply = "我的回复: {};回复间隔:{}s".format(res, interval)
print(reply)
with open("log_.txt", "a+", encoding="utf8") as f:
f.write(str(reply) + "\n")
time.sleep(interval)
return res
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) # 如果收到的信息是其他消息类容,就下载下来
def download_files(msg):
msg['Text'](msg['FileName']) # 下载文件
if __name__ == '__main__':
itchat.auto_login(enableCmdQR=True, hotReload=True)
itchat.run()
如果在pycharm中运行,生成的二维码不规整。不太好扫。命令行运行的话,二维码看的清晰一点。
友情提示:
不管机器人多智能,都要定时看一下聊天内容
以免,是否对你承诺了已太多,还是我原本给的就不够~~~
标签:itchat,python,微信,get,图灵,text,res,msg,data 来源: https://blog.51cto.com/u_14303514/2951705