图灵机器人

利用微信官方提供的开放API itchat库制作的微信聊天机器人

安装itchat,request模块,利用图灵接口

 1 # -*- coding: utf-8 -*-
 2 import itchat,time,re,requests
 3 from threading  import Timer
 4 from itchat.content import *
 5 def get_tl_res(msg):
 6     url = "http://www.tuling123.com/openapi/api"
 7     data = {
 8         "key": "e549********************",# 自己注册图灵,获取KEY
 9         "info": msg,
10         "userid": "pth-robot"
11     }
12     res = requests.post(url, data=data).json()
13     return res.get("text")
14 @itchat.msg_register([TEXT],isGroupChat=True)
15 def text_reply(msg):
16     if msg['isAt']:
17         res = get_tl_res(msg["Text"])
18         itchat.send((res), msg["FromUserName"])
19 @itchat.msg_register([PICTURE,RECORDING,VIDEO,SHARING],isGroupChat=True)
20 def other_reply(msg):
21     if  msg['isAt']:
22         res = get_tl_res(msg[PICTURE,RECORDING,VIDEO,SHARING])
23         itchat.send((res),msg["FromUserName"])
24 
25 itchat.auto_login(hotReload=True)
26 #itchat.auto_login(enableCmdQR=2, hotReload=True) 
27 itchat.run()
 1 # -*- coding: utf-8 -*-
 2 import itchat,time,re,requests
 3 from threading  import Timer
 4 from itchat.content import *
 5 def get_tl_res(msg):
 6     url = "http://www.tuling123.com/openapi/api"
 7     data = {
 8         "key": "e549********************",# 自己注册图灵,获取KEY
 9         "info": msg,
10         "userid": "pth-robot"
11     }
12     res = requests.post(url, data=data).json()
13     return res.get("text")
14 @itchat.msg_register([TEXT],isGroupChat=True)
15 def text_reply(msg):
16     #if msg['isAt']:
17     res = get_tl_res(msg["Text"])
18     itchat.send((res), msg["FromUserName"])
19 @itchat.msg_register([PICTURE,RECORDING,VIDEO,SHARING],isGroupChat=True)
20 def other_reply(msg):
21     #if  msg['isAt']:
22     res = get_tl_res(msg[PICTURE,RECORDING,VIDEO,SHARING])
23     itchat.send((res),msg["FromUserName"])
24 
25 # 注册文本消息,绑定到text_reply处理函数
26 # text_reply msg_files可以处理好友之间的聊天回复
27 @itchat.msg_register([TEXT,MAP,CARD,NOTE,SHARING])
28 def text1_reply(msg):
29     res = get_tl_res(msg["Text"])
30     itchat.send((res), msg["FromUserName"])
31 @itchat.msg_register([PICTURE,RECORDING,VIDEO,SHARING])
32 def other_reply1(msg):
33     #res = get_tl_res(msg[PICTURE,RECORDING,VIDEO,SHARING])
34     res = get_tl_res(msg[PICTURE,RECORDING,VIDEO,SHARING])
35     itchat.send((res),msg["FromUserName"])
36 
37 
38 
39 itchat.auto_login(hotReload=True)
40 #itchat.auto_login(enableCmdQR=2, hotReload=True) 
41 itchat.run()

猜你喜欢

转载自www.cnblogs.com/wanglinjie/p/9276522.html