# -*- coding:UTF-8 -*-
#!/usr/bin/python3
import requests
import itchat
import time
def get_response(msg): # 圖靈機器人微信好友自動回覆
KEY = 'c5bc10cce8eb4757aa04d0045627de81'
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': 'c5bc10cce8eb4757aa04d0045627de81', # 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()