1. 程式人生 > >python itchat+機器人web api實現個人微信機器人

python itchat+機器人web api實現個人微信機器人

mark 打印 web 我們 pycharm 通過 inf 回復 代碼

模塊

itchat

功能

實現微信回復機器人(調用圖靈機器人api)

代碼

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Author:Eric
import requests
import itchat



def getResponse(_info):
    #print(_info)
    apiUrl = ‘http://www.tuling123.com/openapi/api‘
    data = {
        ‘key‘    : ‘7c1ccc2786df4e1685dda9f7a98c4ec9‘, # 如果這個Tuling Key不能用,那就換一個
        ‘info‘   : _info, # 這是我們發出去的消息
        ‘userid‘ : ‘wechat-robot‘, # 這裏你想改什麽都可以
    }
    # 我們通過如下命令發送一個post請求
    r = requests.post(apiUrl, data=data).json()

    # 讓我們打印一下返回的值,看一下我們拿到了什麽
    return r

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
        # print(msg)
    return "小小:" + getResponse(msg["Text"])["text"]

    # itchat.auto_login(enableCmdQR=True)
#pycharm下使用如下兩條命令
itchat.login()
# itchat.auto_login(enableCmdQR=True)
itchat.run()
#服務器上使用如下兩條命令
itchat.auto_login(enableCmdQR=2,hotReload=True)
itchat.run(debug=True)

#print(getResponse("早上好"))

使用方法(前提是設備安裝了python):

本地使用:
輸入pip install itchat  pillow,等待安裝完成,輸入python wx.py,用手機微信掃描生成的二維碼,確認登陸即可

python itchat+機器人web api實現個人微信機器人