使用itchat和图灵机器人搭建一个聊天微信号

上一篇体会到了itchat微信接口的强大与便捷,便想着做一个自动回复的微信,过程很简单,都是现成的接口,调用一下就可以了

整个调试过程也就半个小时,比较简单,直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import itchat
from itchat.content import *
import requests
import json

@itchat.msg_register([TEXT,PICTURE,MAP,CARD,NOTE,SHARING])
def text_reply(msg):
if msg.type==TEXT:
reply='hello'

text=dict(text=msg.text)
inputText=dict(inputText=text)
userInfo=dict(apiKey='xxxxxxxxxxxd474eaxxxxx' ,userId='4xxxxxx')
postjson=dict(perception=inputText,userInfo=userInfo)
postjson=json.dumps(postjson)
r = requests.post("http://openapi.tuling123.com/openapi/api/v2", data=postjson)
print(r.text)
res=r.json()
rescode=res['intent']['code']

if rescode >= 0:
reply=res['results'][0]['values']['text']
else :
if msg.text.find('吗') > -1:
reply=msg.text.split('吗')[0]+'!'
else :
reply=msg.text+'哟!'

msg.user.send('%s' % (reply))

itchat.auto_login()
itchat.run(True)

具体就是要去图灵机器人注册一个账号,创建一个应用,得到apiKey和userID,然后就可以通过http post接口交换聊天信息了。

itchat注册消息到达函数,把朋友发来的消息交给图灵去处理,然后把图灵返回的消息发送给朋友。

效果如下:

另外,代码中还包含了价值一个亿的AI聊天机器人核心代码

更新 百度智能对话 接入

百度在人工智能方面也是领头羊的地位,他的 UNIT智能对话平台 也是非常理想的聊天机器人

官网UNIT搭载业界领先的需求理解、对话控制及底层的自然语言处理、知识挖掘等核心技术让您的产品快速拥有对话交互能力, SDK文档UNIT机器人对话API文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def baiduAI(yousay):
pass
post_dict = dict(version='2.0', log_id='UNITTEST_10000')
post_dict['service_id'] = 'S19668'
post_dict['skill_id'] = '63755'
post_dict['session_id'] = ''

request_dict = dict(user_id='1111', query=yousay)
post_dict['request'] = request_dict

postjson = json.dumps(post_dict)

r = requests.post(
"https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token={24.4d2xxxxxx.1564144088.282335-16652172}", data=postjson)

res = r.json()
if res['error_code'] == 0:
res_dict = dict(
code=0, string=res['result']['response']['action_list'][0]['say']) # 返回权重最高的一句话
else:
res_dict = dict(code=1, string='')

return res_dict
虽然很不要脸,但是还请您多多打赏 ^_^