python itchat+机器人web api实现个人微信机器人

模块

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,用手机微信扫描生成的二维码,确认登陆即可
原文地址:https://www.cnblogs.com/guigujun/p/8719782.html