# -*- coding:UTF-8 -*-
#!/usr/bin/python3
import requests
import itchat
import time
def get_response(msg): # 图灵机器人微信好友自动回复
KEY = 'c5bc10cce8eb4757aa04d0045627de81123123123'
apiUrl = 'http://www.tuling123.com/openapi/api'
data = {
'key': KEY,
'info': msg,
'userid': 'wechat-robot',
}
try:
r = requests.post(apiUrl, data=data).json()
return r.get('text')
except:
return
@itchat.msg_register(itchat.content.TEXT)
# 文本信息回复
def tuling_reply(msg):
reply = get_response(msg['Text'])
time.sleep(3)
return reply
@itchat.msg_register([itchat.content.PICTURE, itchat.content.RECORDING, itchat.content.VIDEO])
# 回复图片/表情,语音,视频
def other_replay(msg):
return "你要和我斗图吗?"
'''
不想每次运行程序都扫码,可以设置参数hotReload=True
如果想在登陆的时候使用命令行显示二维码,可以设置参数enableCmdQR=True
'''
itchat.auto_login()
itchat.run()
群聊
# -*- coding:UTF-8 -*-
#!/usr/bin/python2.6
import requests
import itchat
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api' #改成你自己的图灵机器人的api
data={
'key': 'c5bc10cce8eb4757aa04d0045627de81123123123', # Tuling Key
'info': msg, # 这是我们发出去的消息
'userid': 'wechat-robot', # 这里可随意修改
}
# 通过如下命令发送一个post请求
r = requests.post(apiUrl, data=data).json()
return r.get('text')
@itchat.msg_register(itchat.content.TEXT)
# 用于接收来自朋友间的对话消息 #如果不用这个,朋友发的消息便不会自动回复
def print_content(msg):
print('单人对话-'+msg['User']['NickName']+':'+msg['Text'])
return get_response(msg['Text'])
# 用于接收群里面的对话消息
@itchat.msg_register([itchat.content.TEXT], isGroupChat=True)
def print_content(msg):
print('多人对话-'+msg['User']['NickName']+':'+msg['Text'])
return get_response(msg['Text'])
itchat.auto_login(True)
itchat.run()