Fork me on GitHub

微信机器人

个人微信接入图灵机器人(基于Python)

开发环境:阿里云 Ubuntu 16.04
编程语言:Python2.7

  1. 图灵机器人官网注册账号,创建机器人,如果只是学习的话,可以使用免费版,一个账号最多可以创建5个机器人,一个机器人日调用接口数5000次
  2. 开发环境搭建

    1
    2
    3
    4
    5
    6
    7
    8
    //安装python2.7
    $ sudo apt-get install python2.7
    //安装pip:
    $ sudo apt-get install python-pip
    //有时候安装pillow会出现依赖包缺失错误,需要先安装pillow依赖库
    $ sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk
    //借助python的itchat和pillow,可以使用pip安装。itchat 是 A complete and graceful API for Wechat
    $ sudo pip install itchat pillow
  3. python代码编写,创建demo.py脚本文件

    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
    import itchat, time, re
    from itchat, content import *
    import urllib2,urllib
    import json
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    @itchat.msg_register([TEXT])
    def text_reply(msg):
    info = msg['Text'].encode('UTF-8')
    url = 'http://www.tuling123.com/openapi/api'
    data = {u"key": "9ab235c4ec4b4c2887d4e57627d41ab6","info":info,u"loc": "","userid": ""}
    data = urllib.urlencode(data)
    url2 = urllib2.Request(url, data)
    response = urllib2.urlopen(url2)
    apicontent = response.read()
    s = json.loads(apicontent, encoding='utf-8')
    print 's==',s
    if s['code']== 100000:
    itchat.send(s['text'], msg['FromUserName'])
    itchat.auto_login(enableCmdQR=2,hotReload=True)
    itchat.run(debug=True)
「真诚赞赏,手留余香」