mirror of
https://github.com/smallevilbeast/ntchat.git
synced 2025-05-23 02:26:11 +08:00
33 lines
919 B
Python
33 lines
919 B
Python
# -*- coding: utf-8 -*-
|
||
import sys
|
||
import time
|
||
import ntchat
|
||
|
||
wechat = ntchat.WeChat()
|
||
|
||
# 打开pc微信, smart: 是否管理已经登录的微信
|
||
wechat.open(smart=True)
|
||
|
||
|
||
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
|
||
data = message["data"]
|
||
from_wxid = data["from_wxid"]
|
||
self_wxid = wechat_instance.get_login_info()["wxid"]
|
||
room_wxid = data["room_wxid"]
|
||
|
||
# 判断消息不是自己发的并且不是群消息时,回复对方
|
||
if from_wxid != self_wxid and not room_wxid:
|
||
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")
|
||
|
||
|
||
# 监听接收文本消息
|
||
wechat.on(ntchat.MT_RECV_TEXT_MSG, on_recv_text_msg)
|
||
|
||
# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
|
||
try:
|
||
while True:
|
||
time.sleep(0.5)
|
||
except KeyboardInterrupt:
|
||
ntchat.exit_()
|
||
sys.exit()
|