通过python的itchat库调用图灵机器人的接口,可以实现人机自动对话.我是在ubuntu16.04下进行开发的.
首先 进入终端,输入pip install itchat安装库
在图灵机器人注册帐号获得 key 值 http://www.tuling123.com/
在sublime-text 里面编写程序 运行.扫描二维码登录. 简短代码如下,实现自动回复功能.
# coding:utf-8
import itchat
from itchat.content import TEXT
from itchat.content import *
import sys
import time
import re
import requests
reload(sys)
sys.setdefaultencoding('utf8')
import os
key = '***********************'
msg_information = {}
face_bug=None #针对表情包的内容
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api'
data={
'key' : key,
'info' : msg,
'userid':'杨方健的智能小助手'
}
try:
r = requests.post(apiUrl,data=data).json()
return r.get('text')
except:
return
@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO],isFriendChat=True, isGroupChat=True, isMpChat=True)
def tuling_reply(msg):
defaultReply = 'i received'+msg['Text']
reply = get_response(msg['Text'])
return reply or defaultReply
itchat.auto_login(hotReload=True)
itchat.run()