您的位置:首页 > 编程语言 > Python开发

python 微信机器人之图灵

2016-03-15 10:20 579 查看
前几天在知乎上看到有人用图灵提供的api实现了微信聊天机器人,本人也跟风之下做了一个,用时1天半,
制作微信聊天机器人,需要如下几个条件,
1:web微信通讯协议,和各路接口
2:机器人接口


微信web版的模拟登陆和各个接口我是没有时间去研究,不过刚好在github上看到了一个别人做好的框架,就拿下来用了

地址:https://github.com/liuwons/wxBot

下载之后,直接放到lib目录下就好了,

这里多说一下,如果你出现%1 这不是标准的win32程序,而且是出在xml.dom下面的话,可以试着换一种方式来完成,在wxbot.pyde login函数里面本人是换成了lxml库的方式来替换那段代码,

self.skey = doc.xpath(u'//skey')[0].text
print self.skey
self.sid = doc.xpath(u'//wxsid')[0].text
print self.sid
self.uin = doc.xpath(u'//wxuin')[0].text
print self.uin
self.pass_ticket = doc.xpath(u'//pass_ticket')[0].text
print self.pass_ticket


这段代码就是从下载下来的xml文件中读取相应的数据。

然后运行wxbot文件下的test.py大概就可以完成了

这个库相应的解析在github上面有,自己去看吧,

接下来就是聊天机器人了,

图灵机器人的官网是:http://www.tuling123.com/login.htm?loginRedirectUrl=%2Fweb%2Frobot_access%21index.action%3Fcur%3Dl_05

我是用qq登陆的,你看着办。

登陆之后在仪表盘-机器人接入-可以看到你的apikey,那个就是在向图灵post的时候所需要的

因为我选择的是python所以只能是api接入了,下载后面的文档,可以看到他教你怎么获取图灵的数据,

下面是一段解析和下载图灵的代码

class TuLin(object):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
self.Url = "http://www.tuling123.com/openapi/api"
self.template = "?key=APIKEY&info=content"
self.ApiKey = self.getKey()
self.postObjecturl = ''
self.returnforTulin = ''
'''

'''
self.textCate = 100000
self.linkCate = 200000
self.newCate = 302000
self.cookbookCate = 308000
def filter(self,text):
return text.replace(u'图灵机器人',u'小超',1)
def analysisreturnforTulininJson(self,textjson):
returninfodict = simplejson.loads(textjson)
returnToWechatText = self.combin(returninfodict)
return self.filter(returnToWechatText)
def combin(self,dictinfor):
if dictinfor['code']==self.textCate:
return self.textinfo(dictinfor)
if dictinfor['code'] == self.linkCate:
return self.linkinfo(dictinfor)
if dictinfor['code'] == self.newCate:
return self.newinfo(dictinfor)
if dictinfor['code'] == self.cookbookCate:
return self.cookinfo(dictinfor)
def textinfo(self,dict):
return dict['text']
def linkinfo(self,dict):
return dict['text']+'\n'+dict['url']
def newinfo(self,dict):
text = ''
text= text + dict['text']+'\n'
for newdict in dict['list']:
text = text + newdict['article']+'\n'
text = text + newdict['source']+'\n'
text = text + newdict['detailurl']+'\n'
text = text+'-----------------------\n'
return text
def cookinfo(self,dict):
text = ''
text= text + dict['text']
cookdict = dict['list']
text = text + cookdict['name']+'\n'
text = text + cookdict['icon']+'\n'
text = text + cookdict['info']+'\n'
text = text + cookdict['detailurl']+'\n'
text = text+'-----------------------\n'
return text
def getReturnText(self):
return self.analysisreturnforTulininJson(self.returnforTulin)

def sendTextToTuLin(self,text):
self.postObjecturl = self.Url + self.template.replace('APIKEY',self.ApiKey,1).replace('content',text,1)
self.returnforTulin = self.getSourceforHtml(self.postObjecturl.decode('utf-8'))

def getKey(self):
fkey = open('APIKEY','r')
apikey = fkey.read()
fkey.close()
return apikey

def getSourceforHtml(self,PostUrl):
return requests.post(PostUrl).content


在外面只需要set一次,然后get可以得到解析好的数据了,

再照着wxbot框架的方法,接收到数据之后set给图灵,之后get数据,就好了

so,大致的流程就是这样子的了,

如果遇到什么别的问题可以在下面回复哦,

谢谢观看,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: