4
头图

need

Mac WeChat has a message notification, which is good. Sometimes you can see the message content without opening it at all. The Windows computer version does not have this message notification, only the icon in the lower right corner flashes.

image.png
(Mac WeChat notification)

So I thought of a lot of ways to achieve such a function, and I have found another way to achieve this function. In fact, it has certain limitations, but it can meet the small needs during the office.

The effect is as follows:

image.png
(Windows WeChat notification)

Implementation

receive notifications

The first is that the implementation of Windows notification pop-up is the notification bar that comes with Windows. We can use Python to call Windows API to send Windows notifications and let the message pop up.

send notification

Compared with receiving notifications, it is more difficult to send notifications, because the window of the Windows WeChat PC version belongs to the non-Windows window standard, we cannot read the content of each control in a conventional way, let alone monitor the notification content, so I Here is another way to monitor WeChat messages through Android phones.

So here is the open source software SmsForwarder, installed on your Android phone, then set the forwarding rules, monitor the notifications of WeChat APP, and then send the channel to select WebHook, POST the monitored message to the HTTP server opened by Python, and trigger Windows Just a notification!

image.png

code above

module

The following modules are involved, flask is used to implement http requests, winotify is used to send Windows notifications, urllib.parse is used to decode the URL encoding from POST, and win32gui and win32con are used to create task icons on the taskbar and run in the background .

 from flask import Flask, request
from winotify import Notification
import urllib.parse
import win32gui
import win32con

Code description:
The key code snippet for sending the notification, among which icon=r"D:\Python\demo\wechat.png" this is a small icon displayed in the notification, you can find a picture you like online and put it in the same level directory of toasts.py.

 toast = Notification(app_id="通知中心",title=nickname,msg=weixinMsg,icon=r"D:\Python\demo\wechat.png")
    toast.show()

192.168.1.100 is the intranet ip of my computer, and 8080 is the port number. You need to obtain the intranet ip of your computer and replace it, otherwise the subsequent configuration of the monitoring forwarder on the Android side will not be able to forward notifications normally.

 app.run(debug=False,host='192.168.1.100', port=8080)

image.png

Enter ipconfig in cmd to get the ip address.

Finally execute the code.

image.png

After executing the code, an HTTP service will be opened on your computer. At this time, you can test locally in the browser, and paste it in the browser according to the following format:

 http://内网ip:端口号/?通知内容

Then you can visit.

image.png

toasts.py

 from flask import Flask, request
from winotify import Notification
import urllib.parse
import win32gui
import win32con

app = Flask(__name__)

@app.route('/')  # 获取url信息
def getUrlInfo():
    # 完整url
    url = request.url
    # 主机部分
    hostUrl = request.host_url
    # 访问路径
    fullPath = request.full_path
    # 输出
    print('收到推送任务,推送内容是:'+str(urllib.parse.unquote(fullPath.split("/?")[1])).replace('+', ' ', 1))

    # 接收到的内容
    content = str(urllib.parse.unquote(fullPath.split("/?")[1])).replace('+', ' ', 1);

    # 错误处理
    # 因为监听软件那边监听到的首条消息是没有带上微信用户昵称的
    # 所以需要判断当前接收到的消息是不是首条消息
    # 如果不做这一步就会出错

    pdmh = ":" in content
    if pdmh == True:
        # 截取:前面的内容
        qianmian = content.split(":")[0]
        weixinMsg = content.split(":")[1]
        # 还要将[]这一块也去掉,这就提取到了微信昵称
        nickname = qianmian.split("]")[1]
    else:
        nickname = '微信消息通知'
        weixinMsg = content

    # 开发Push通知
    # toaster = ToastNotifier()
    # toaster.show_toast(title=nickname, msg=weixinMsg,icon_path="logo.ico", duration=5)
    toast = Notification(app_id="通知中心",title=nickname,msg=weixinMsg,icon=r"D:\Python\demo\wechat.png")
    toast.show()
    return "ok"


def notify(hwnd, msg, wparam, lparam):
    print("notify", msg)
    if lparam == win32con.WM_LBUTTONDBLCLK:  # 双击左键
        print("双击左键", msg)
        pass
    elif lparam == win32con.WM_RBUTTONUP:  # 右键弹起
        print("右键弹起", msg)
        pass
    elif lparam == win32con.WM_LBUTTONUP:  # 左键弹起
        print("左键弹起", msg)
        pass
    return True

wc = win32gui.WNDCLASS()
wc.hInstance = win32gui.GetModuleHandle(None)
wc.lpszClassName = "Windows通知中心"
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
wc.lpfnWndProc = notify
classAtom = win32gui.RegisterClass(wc) 
hwnd =win32gui.CreateWindow(classAtom,"tst2",win32con.WS_OVERLAPPEDWINDOW,win32con.CW_USEDEFAULT,win32con.CW_USEDEFAULT,win32con.CW_USEDEFAULT,win32con.CW_USEDEFAULT,None,None,None,None)
notify_id = (hwnd,0,win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,win32con.WM_USER + 20,win32gui.LoadIcon(0, win32con.IDI_APPLICATION),"Windows通知中心")
win32gui.Shell_NotifyIcon(0,notify_id)

# 在指定IP和端口开启HTTP服务
if __name__ == '__main__':
    app.run(debug=False,host='192.168.1.100', port=8080)

Android phone configuration

Install the software SmsForwarder.

Open source address: https://gitee.com/pp/SmsForwarder
Download address: https://gitee.com/pp/SmsForwarder/releases/tag/v3.0.8

Set the sending channel, select Webhook , and fill in the following format for Webserver:

 http://内网ip:端口号/

WebParams directly fill in [msg] , the request method is GET

image.png

Set the forwarding rule, add a rule, select the matching “APP包名” , select the matching mode “是” , the matching value is com.tencent.mm , select the sending channel just configured, customize the template directly Click on the notification content.

After simple configuration, SmsForwarder can be used normally after making some necessary settings.

Where to optimize

At present, we are still trying to find a way to monitor WeChat messages locally on the computer. After all, monitoring messages through an Android phone is not an option. On the one hand, we rely on the local server to send notifications to the computer through the local area network. On the other hand, Android monitoring messages are not stable, and not everyone uses Android. It is difficult for users of Apple mobile phones to achieve this.

Points to pay attention to

  1. The intranet ip may change. If you find that the notification cannot be received normally, you can check the intranet ip, change the ip and restart the service.
  2. Because this is an HTTP service implemented using an intranet ip, your Android phone needs to be connected to the same wifi as your computer. If your computer is a wired network, your phone needs to be in the same network environment as the computer's wired network, that is, the same ip.
  3. If you do not want to implement it through a local area network, you can configure the intranet penetration service yourself to implement the external network request service.

A scheme for monitoring WeChat messages

  1. Hook WeChat, read memory to monitor WeChat messages
  2. Using the off-the-shelf solution, there are also some Hook WeChat frameworks on the market, such as Cute Cat . This framework provides a very convenient SDK to call the API to monitor WeChat chat messages, and even send messages.

author

TANKING


TANKING
4.8k 声望493 粉丝

热爱分享,热爱创作,热爱研究。