【Python自动化】微信自动化智能回复
参考资料:
感谢晚枫大大提供的工具包,教程链接如下:又一个微信聊天机器人横空出世了,人人可用!,B站视频链接如下:又一个微信聊天机器人发布了,人人可用!不需要网页版微信
微信自动化回复工具包Github链接如下:PyOfficeRobot(目前好像仅支持windows
系统)
中文对话系统项目链接如下:dialogbot,gpt2-dialogbot-base-chinese
安装教程:
#安装微信自动化回复插件(我安装作者提供的python-office工具包时,PyMuPDF报错,其实只安装PyOfficeRobot即可)
pip install PyOfficeRobot -i https://pypi.tuna.tsinghua.edu.cn/simple -U
pip install pywin32 -i https://pypi.tuna.tsinghua.edu.cn/simple
#智能问答
pip install -U dialogbot
参考代码:
以下代码可以实现整点报时和智能回复。
from PyOfficeRobot.api.chat import wx
from dialogbot import GPTBot
from datetime import datetime
import time
#参考 https://huggingface.co/shibing624/gpt2-dialogbot-base-chinese
model = GPTBot("shibing624/gpt2-dialogbot-base-chinese") #加载问答模型
#周期任务 + 三餐消息报时
def pointOut_with_interval(who):
# wx.GetSessionList() # 获取会话列表
# wx.ChatWith(who) # 打开`who`聊天窗口
hours_msg = {
8 : '记得准时吃早饭哦~',
12 : '记得准时吃午饭哦~',
18 : '记得准时吃晚饭哦~'
}
# print('pointOut_with_interval')
cur_hour = datetime.now().hour
cur_minute = datetime.now().minute
cur_second = datetime.now().second
if(cur_minute == 0 and cur_second < 2):
msg = f'现在是北京时间{
cur_hour}点整' #整点报时
wx.SendMsg(msg) # 发送消报时息
if (cur_hour in hours_msg.keys()):
msg = f'{
hours_msg[cur_hour]}'
wx.SendMsg(msg) #发送提醒消息
#智能闲聊
def chat_with_AI(who):
wx.GetSessionList() # 获取会话列表
wx.ChatWith(who) # 打开`who`聊天窗口
temp_msg = ''
while True:
try:
time.sleep(0.5) #释放资源
#定时任务
pointOut_with_interval(who)
friend_name, receive_msg = wx.GetAllMessage[-1][0], wx.GetAllMessage[-1][1] # 获取朋友的名字、发送的信息
if (friend_name == who) & (receive_msg != temp_msg):
"""
条件:
朋友名字正确:(friend_name == who)
不是上次的对话:(receive_msg != temp_msg)
# 对方内容在自己的预设里:(receive_msg in kv.keys())
"""
temp_msg = receive_msg
answer = model.answer(temp_msg)
wx.SendMsg(answer) # 向`who`发送消息
except:
pass
if __name__ == '__main__':
who = "xxx" # 你好友名字
chat_with_AI(who) #智能闲聊
效果展示: