编程语言
首页 > 编程语言> > 使用 Python 来自动回微信

使用 Python 来自动回微信

作者:互联网

准备

引入库

代码:

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()

其中:

全部代码

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()

程序效果


数据库里:

(此为部分数据)

标签:itchat,Python,db,re,自动,msg,import,pymongo,回微信
来源: https://www.cnblogs.com/WindowsRegedit/p/16209529.html