准备
Python3
Python Itchat库(可以通过来安装)
(可选)Python Pymongo库(可以通过来安装)
(可选)MongoDB(请自行百度)
引入库
代码:
import re
import itchat
import pymongo
import requests
from urllib.parse import quote
其中:
请根据自己的情况导入
re(正则表达式)库用于剔除多余的AI表情字符(如这种)
itchat 库用于发微信
pymongo 库用于将接收的数据保存到数据库
requests 库用于获取AI通话的内容
urllib.parse.quote 用于将聊天的内容转换成URL
主函数
s = requests.Session()
msg_url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg={message}"
client = pymongo.MongoClient('localhost', 27017)
db = client['wechat']
collection = db['wechat']
@itchat.msg_register(itchat.content.TEXT)
def _(msg):
print(msg)
collection.insert_one(msg)
if msg.toUserName == "filehelper":
r = s.get(msg_url.format(message=quote(msg['Text'])))
print(r.json())
msg.user.send(re.sub('\{.*?\}', '', r.json()["content"]))
itchat.auto_login(hotReload=True)
itchat.run()
其中:
定义client, db, collection的请根据自己的情况取舍
其余代码请自行消化
全部代码
import re
import itchat
import pymongo
import requests
from urllib.parse import quote
s = requests.Session()
msg_url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg={message}"
client = pymongo.MongoClient('localhost', 27017)
db = client['wechat']
collection = db['wechat']
@itchat.msg_register(itchat.content.TEXT)
def _(msg):
print(msg)
collection.insert_one(msg)
if msg.toUserName == "filehelper":
r = s.get(msg_url.format(message=quote(msg['Text'])))
print(r.json())
msg.user.send(re.sub('\{.*?\}', '', r.json()["content"]))
itchat.auto_login(hotReload=True)
itchat.run()
程序效果
数据库里:
(此为部分数据)