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,大致的流程就是这样子的了,
如果遇到什么别的问题可以在下面回复哦,
谢谢观看,
相关文章推荐
- python内存管理机制
- python学习笔记(3)-进程和线程(一)-多进程
- 用 ElementTree 在 Python 中解析 XML
- python之控制台(console)颜色显示
- Python之路:进程、线程
- python 多线程
- python twisted 建立服务器
- python实现简单爬虫抓取图片
- [转]Python Dict 用法
- Python基础语法(一)
- Python设计模式(9)-外观模式
- Python设计模式(9)-外观模式
- Python设计模式(8)-抽象工厂
- python利用or在列表解析中调用多个函数.py
- [python] LDA处理文档主题分布及分词、词频、tfidf计算
- Python回顾与整理3:数字
- Python回顾与整理3:数字
- Python爬虫_获取贴吧内容
- Python Paste 学习笔记
- Python3.4下安装pip和MySQLdb