【微信_监听并回复消息(私聊+群聊)】

张开发
2026/5/6 6:03:54 15 分钟阅读
【微信_监听并回复消息(私聊+群聊)】
微信_监听并回复消息私聊群聊import json from wxauto import WeChat from wxauto.msgs import FriendMessage import time import requests #与AP智能体对话 def talk_with_AP_agent(content): API_URL https://power-api.yingdao.com/oapi/assistant/v1/session/run/XXX headers { Authorization:Bearer AP_XXX, Content-Type: application/json } def query(payload): response requests.post(API_URL, headersheaders, jsonpayload, streamTrue) return response output query({thirdId:xiaoman,question:{type:text,value:content},startFlowId:,startNodeId:}) return output.json()[data][0][content][value] #消息处理 def on_message(msg, chat): #获取当下时间 now time.strftime(%Y-%m-%d %H:%M:%S, time.localtime()) message_detailf在{now},收到来自{msg.info[chat_type]}{msg.info[chat_name]}的消息发送者{msg.sender},发送者类型{msg.attr}消息类型{msg.type},消息内容{msg.content} if msg.sender ! self: print(message_detail) #如果是对方发来的消息 if isinstance(msg, FriendMessage): #判断是否需要回复 need_reply False chat_type chat.chat_type contentNone #处理群聊消息 if chat_type group: # print(群聊消息---, msg.content) for my_nickname in nickname_in_group_list: if f{my_nickname} in msg.content: need_reply True #去掉昵称部分 content msg.content.replace(f{my_nickname}, ).strip() break #处理好友消息 elif chat_type friend: need_reply True content msg.content print(好友消息---, content) elif chat_type service: need_reply True content msg.content print(客服消息---, content) if need_reply and content: output_data talk_with_AP_agent(content) # output_data这是测试回复内容 print(f回复内容:\n{output_data}) #引用并回复 msg.quote(output_data) # #直接回复 # msg.reply(output_data) time.sleep(1) #监控微信 wx WeChat() # 请填写在群聊或者私聊中你的昵称,注意是备注名 nickname_in_group_list[wx.nickname,小王] print(nickname_in_group_list) # 请填写需要监听的群聊名称或好友昵称。注意是给对方设置的备注名而不是对方真实的微信名 # listen_list[影刀技术服务小群] listen_list[小曼的个微群,小曼的企微群] for user in listen_list: # 移除监听 wx.RemoveListenChat(nicknameuser) # 添加监听监听到的消息用 on_message 函数进行处理 wx.AddListenChat(nicknameuser, callbackon_message) # 保持程序运行 wx.KeepRunning()

更多文章