# -*- coding: utf-8 -*-
import itchat
import requests
import sys
#登录微信并记录登录信息
itchat.auto_login(hotReload=True)
nameIn = input('请输入微信好友名:')
apiUrl = 'http://www.tuling123.com/openapi/api'
def get_info(message):
data = {
'key':图灵apikey',
'info':message,
'userid':'robot'
}
try:
r = requests.post(apiUrl,data=data).json()
answer = r['text']
print('robot reply:%s'%answer)
return answer
except:
return
#回复给微信好友
# 装饰器,给函数新增功能
@itchat.msg_register(itchat.content.TEXT)
def anto_reply(msg):
readFriend = itchat.search_friends(name=nameIn)
realFriendsName = readFriend[0]['UserName']
#打印好友回复的消息
print('message:%s'%msg['Text'])
#调用图灵接口
reply = get_info(msg['Text'])
if msg['FromUserName'] == realFriendsName:
itchat.send(reply,toUserName=realFriendsName)
itchat.run()