Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 28, 2025. It is now read-only.
/wxautoPublic archive

实时推送新消息至bark#319

hubumulity started this conversation inShow and tell
Discussion options

-- coding: utf-8 --

导入 WeChat 和需要用到的消息类型

from wxauto import WeChat
from wxauto.msgs import FriendMessage
import time
import requests
import json

初始化 WeChat 实例

wx = WeChat()

发送通知的函数

def send_notification(title, content):
data = {
"body": content,
"title": title,
"device_key": "", # 注意:这里是你的设备密钥,请妥善保管
"url": "",
"group": "绿泡泡",
"icon": "https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_appwx_logo.png"
}
headers = {'Content-Type': 'application/json'}
try:
response = requests.post('https://api.day.app/push', data=json.dumps(data), headers=headers)
response.raise_for_status() # 检查请求是否成功
print(f"通知已发送: {title} - {content}")
time.sleep(1)
except requests.exceptions.RequestException as e:
print(f"发送通知时出错: {e}")

设置检查新消息的时间间隔

wait_time = 1

无限循环检查新消息

while True:
try:
# 获取下一条新消息,结果可能是一个字典,也可能是None
messages = wx.GetNextNewMessage(filter_mute=True)

    # 检查是否真的收到了新消息    if messages:        print(f"收到新消息包: {messages}")        # 【修改点1】使用 messages['msg'] 来访问字典中的消息列表        for msg in messages['msg']:            # print(f"正在处理消息: {msg}") # 可以取消注释这行来调试                        # 【修改点2】判断单个消息 msg 的类型,而不是整个 messages 字典            if isinstance(msg, FriendMessage):                print(f"识别到好友消息: 来自 {msg.sender} 的内容是 '{msg.content}'")                send_notification(f"{msg.sender}", f"{msg.content}")except Exception as e:    print(f"主循环出现错误: {e}")time.sleep(wait_time)
You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
1 participant
@hubumulity

[8]ページ先頭

©2009-2025 Movatter.jp