本文使用了28行代码实现了一个会自动聊天的机器人.其中主要的类库是基于ichart,API接口基于api.qingyunke
因为读者的系统环境不同,在这里关于如何安装python环境与配置python pip环境请读者自行google,如果你当前的系统是windows,可以参考这里, 点我
$ pip install itchat $ pip install requests $ pip install time $ pip install re $ pip install sys $ pip install urllib2 $ pip install urllib $ pip install json
#!/usr/bin/env python # -- coding: utf-8 -- import itchat, time, re, sys, urllib2, json from urllib import quote,unquote,urlencode from itchat.content import * @itchat.msg_register([TEXT]) def text_reply(msg): m = quote(msg['Text'].encode('utf8')) url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg='+m print url raw = urllib2.urlopen(url) try: data = json.loads(raw.read().decode('utf-8')) #print data['response'] print data['content'] itchat.send((data['content']), msg['FromUserName']) except: pass @itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING]) def pic_reply(msg): itchat.send((unicode('图片不错','gb2312')),msg['FromUserName']) #itchat.send('@img@1.jpg', msg['FromUserName']) if __name__ == '__main__': default_encoding = 'utf-8' index = 0 if sys.getdefaultencoding() != default_encoding: itchat.auto_login(enableCmdQR=False,hotReload=True) itchat.run()
python auto-reply-wechat.py
本文使用了28行代码实现了一个会自动聊天的机器人.源码地址: 点我 ,
最后感谢IVAN的友情支持。
本文0%为翻译组合, 100%为原创