mirror of
https://github.com/smallevilbeast/ntchat.git
synced 2025-05-23 05:29:43 +08:00
添加使用例子
This commit is contained in:
parent
ab8995fc79
commit
be10cf4548
0
examples/__init__.py
Normal file
0
examples/__init__.py
Normal file
32
examples/auto_accept_friend_request.py
Normal file
32
examples/auto_accept_friend_request.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import ntchat
|
||||||
|
import xml.dom.minidom
|
||||||
|
|
||||||
|
wechat = ntchat.WeChat()
|
||||||
|
|
||||||
|
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||||
|
wechat.open(smart=False)
|
||||||
|
|
||||||
|
|
||||||
|
# 注册消息回调
|
||||||
|
@wechat.msg_register(ntchat.MT_RECV_FRIEND_MSG)
|
||||||
|
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
|
||||||
|
xml_content = message["data"]["raw_msg"]
|
||||||
|
dom = xml.dom.minidom.parseString(xml_content)
|
||||||
|
|
||||||
|
# 从xml取相关参数
|
||||||
|
encryptusername = dom.documentElement.getAttribute("encryptusername")
|
||||||
|
ticket = dom.documentElement.getAttribute("ticket")
|
||||||
|
scene = dom.documentElement.getAttribute("scene")
|
||||||
|
|
||||||
|
# 自动同意好友申请
|
||||||
|
wechat_instance.accept_friend_request(encryptusername, ticket, int(scene))
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
ntchat.exit_()
|
||||||
|
sys.exit()
|
28
examples/echo_bot_msg_register.py
Normal file
28
examples/echo_bot_msg_register.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import ntchat
|
||||||
|
|
||||||
|
wechat = ntchat.WeChat()
|
||||||
|
|
||||||
|
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||||
|
wechat.open(smart=False)
|
||||||
|
|
||||||
|
|
||||||
|
# 注册消息回调
|
||||||
|
@wechat.msg_register(ntchat.MT_RECV_TEXT_MSG)
|
||||||
|
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"]
|
||||||
|
|
||||||
|
# 判断消息不是自己发的,并回复对方
|
||||||
|
if from_wxid != self_wxid:
|
||||||
|
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
ntchat.exit_()
|
||||||
|
sys.exit()
|
29
examples/echo_bot_on.py
Normal file
29
examples/echo_bot_on.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import ntchat
|
||||||
|
|
||||||
|
wechat = ntchat.WeChat()
|
||||||
|
|
||||||
|
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||||
|
wechat.open(smart=False)
|
||||||
|
|
||||||
|
|
||||||
|
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"]
|
||||||
|
|
||||||
|
# 判断消息不是自己发的,并回复对方
|
||||||
|
if from_wxid != self_wxid:
|
||||||
|
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")
|
||||||
|
|
||||||
|
|
||||||
|
# 监听接收文本消息
|
||||||
|
wechat.on(ntchat.MT_RECV_TEXT_MSG, on_recv_text_msg)
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
ntchat.exit_()
|
||||||
|
sys.exit()
|
25
examples/get_contacts.py
Normal file
25
examples/get_contacts.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import ntchat
|
||||||
|
|
||||||
|
wechat = ntchat.WeChat()
|
||||||
|
|
||||||
|
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||||
|
wechat.open(smart=False)
|
||||||
|
|
||||||
|
# 等待登录
|
||||||
|
wechat.wait_login()
|
||||||
|
|
||||||
|
# 获取联系人列表并输出
|
||||||
|
contacts = wechat.get_contacts()
|
||||||
|
|
||||||
|
print("联系人列表: ")
|
||||||
|
print(contacts)
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
ntchat.exit_()
|
||||||
|
sys.exit()
|
25
examples/get_rooms.py
Normal file
25
examples/get_rooms.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import ntchat
|
||||||
|
|
||||||
|
wechat = ntchat.WeChat()
|
||||||
|
|
||||||
|
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||||
|
wechat.open(smart=False)
|
||||||
|
|
||||||
|
# 等待登录
|
||||||
|
wechat.wait_login()
|
||||||
|
|
||||||
|
# 获取联系人列表并输出
|
||||||
|
rooms = wechat.get_rooms()
|
||||||
|
|
||||||
|
print("群列表: ")
|
||||||
|
print(rooms)
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
ntchat.exit_()
|
||||||
|
sys.exit()
|
9
examples/multi_open.py
Normal file
9
examples/multi_open.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import ntchat
|
||||||
|
|
||||||
|
# 多开3个微信
|
||||||
|
for i in range(3):
|
||||||
|
wechat = ntchat.WeChat()
|
||||||
|
wechat.open()
|
||||||
|
|
16
examples/resources/send_text_ui.xml
Normal file
16
examples/resources/send_text_ui.xml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--炫彩界面库-窗口布局文件-->
|
||||||
|
<head>
|
||||||
|
<bindJsFile value="" />
|
||||||
|
</head>
|
||||||
|
<windowUI center="true" content="ntchat界面演示" enableLayout="true" layout.horizon="true" padding="5,5,5,5" rect="20,20,761,571" windowStyle="2031" showT="true">
|
||||||
|
<editUI drawBorder="true" layout.height="fill" layout.width=":1" multiLine="true" name="edit_log" rect="246,187,100,20" scrollBarShowH="false" scrollBarShowV="true" showT="true" expandT="true" />
|
||||||
|
<elementUI layout.float="true" layout.height="fill" rect="219,85,200,100" transparent="true" showT="true" expandT="true">
|
||||||
|
<editUI content="filehelper" name="edit_wxid" rect="65,106,116,27" showT="true" expandT="true" />
|
||||||
|
<editUI content="消息来自ntchat" name="edit_content" rect="65,151,116,27" showT="true" expandT="true" />
|
||||||
|
<shapeText content="wxid" layout.height="20" layout.width="auto" rect="18,110,61,20" showT="true" expandT="true" />
|
||||||
|
<shapeText content="消息" layout.height="20" layout.width="auto" rect="20,156,61,20" showT="true" expandT="true" />
|
||||||
|
<buttonUI content="发送消息" name="btn_send" rect="93,192,60,25" showT="true" expandT="true" />
|
||||||
|
<buttonUI content="打开微信" name="btn_open" rect="47,31,120,25" showT="true" expandT="true" />
|
||||||
|
</elementUI>
|
||||||
|
</windowUI>
|
16
examples/resources/warn.svg
Normal file
16
examples/resources/warn.svg
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||||
|
<svg t="1639215066678" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5900"
|
||||||
|
xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style>
|
||||||
|
</defs>
|
||||||
|
|
||||||
|
<path d="M512 512m-403.2 0a403.2 403.2 0 1 0 806.4 0 403.2 403.2 0 1 0-806.4 0Z" fill="#F9D65D" p-id="5901">
|
||||||
|
</path>
|
||||||
|
|
||||||
|
<path d="M512 608c-19.2 0-38.4-19.2-38.4-38.4V288c0-19.2 19.2-38.4 38.4-38.4s38.4 19.2 38.4 38.4
|
||||||
|
V576c0 12.8-19.2 32-38.4 32z" fill="#FFFFFF" p-id="5902">
|
||||||
|
</path>
|
||||||
|
|
||||||
|
<path d="M512 736m-44.8 0a44.8 44.8 0 1 0 89.6 0 44.8 44.8 0 1 0-89.6 0Z" fill="#FFFFFF" p-id="5903">
|
||||||
|
</path>
|
||||||
|
|
||||||
|
</svg>
|
After Width: | Height: | Size: 772 B |
24
examples/send_text.py
Normal file
24
examples/send_text.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import ntchat
|
||||||
|
|
||||||
|
wechat = ntchat.WeChat()
|
||||||
|
|
||||||
|
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||||
|
wechat.open(smart=False)
|
||||||
|
|
||||||
|
# 等待登录
|
||||||
|
wechat.wait_login()
|
||||||
|
|
||||||
|
# 向文件助手发送一条消息
|
||||||
|
wechat.send_text(to_wxid="filehelper", content="hello, filehelper")
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
ntchat.exit_()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
53
examples/send_text_ui.py
Normal file
53
examples/send_text_ui.py
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
import xcgui
|
||||||
|
import ntchat
|
||||||
|
from xcgui import XApp, XWindow
|
||||||
|
|
||||||
|
|
||||||
|
class NtChatWindow(XWindow):
|
||||||
|
def __init__(self):
|
||||||
|
super(NtChatWindow, self).__init__()
|
||||||
|
self.loadLayout("resources\\send_text_ui.xml")
|
||||||
|
self.setMinimumSize(600, 500)
|
||||||
|
|
||||||
|
btn: xcgui.XButton = self.findObjectByName("btn_open")
|
||||||
|
btn.regEvent(xcgui.XE_BNCLICK, self.on_btn_open_clicked)
|
||||||
|
|
||||||
|
btn: xcgui.XButton = self.findObjectByName("btn_send")
|
||||||
|
btn.regEvent(xcgui.XE_BNCLICK, self.on_btn_send_clicked)
|
||||||
|
|
||||||
|
self.edit_wxid: xcgui.XEdit = self.findObjectByName("edit_wxid")
|
||||||
|
self.edit_content: xcgui.XEdit = self.findObjectByName("edit_content")
|
||||||
|
self.edit_log: xcgui.XEdit = self.findObjectByName("edit_log")
|
||||||
|
self.edit_log.enableAutoWrap(True)
|
||||||
|
|
||||||
|
self.wechat_instance: ntchat.WeChat = None
|
||||||
|
|
||||||
|
def on_btn_open_clicked(self, sender, _):
|
||||||
|
self.wechat_instance = ntchat.WeChat()
|
||||||
|
self.wechat_instance.open()
|
||||||
|
self.wechat_instance.on(ntchat.MT_ALL, self.on_recv_message)
|
||||||
|
|
||||||
|
def on_btn_send_clicked(self, sender, _):
|
||||||
|
if not self.wechat_instance.login_status:
|
||||||
|
svg = xcgui.XSvg.loadFile("resources\\warn.svg")
|
||||||
|
svg.setSize(16, 16)
|
||||||
|
self.notifyMsgWindowPopup(xcgui.position_flag_top, "警告", "请先打开并登录微信",
|
||||||
|
xcgui.XImage.loadSvg(svg), xcgui.notifyMsg_skin_warning)
|
||||||
|
else:
|
||||||
|
self.wechat_instance.send_text(self.edit_wxid.getText(), self.edit_content.getText())
|
||||||
|
|
||||||
|
def on_recv_message(self, wechat, message):
|
||||||
|
text = self.edit_log.getText()
|
||||||
|
text += "\n"
|
||||||
|
text += str(message)
|
||||||
|
self.edit_log.setText(text)
|
||||||
|
self.redraw()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app = XApp()
|
||||||
|
window = NtChatWindow()
|
||||||
|
window.showWindow()
|
||||||
|
app.run()
|
||||||
|
ntchat.exit_()
|
||||||
|
app.exit()
|
8
examples/test.py
Normal file
8
examples/test.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import xml.dom.minidom
|
||||||
|
|
||||||
|
content = "<msg fromusername=\"houshao55\" encryptusername=\"v3_020b3826fd03010000000000e1b4234fe9f149000000501ea9a3dba12f95f6b60a0536a1adb6b8611b62b0df6381d864972ef3e95c0a10c1a9d455c8adf40eb8b2ba8bc7f156d6253929391104861ffa18287b@stranger\" fromnickname=\"pony\" content=\"我是小邪\" fullpy=\"pony\" shortpy=\"PONY\" imagestatus=\"3\" scene=\"6\" country=\"CN\" province=\"Chongqing\" city=\"Yongchuan\" sign=\"紧急情况,暂不联系\" percard=\"1\" sex=\"1\" alias=\"\" weibo=\"\" albumflag=\"0\" albumstyle=\"0\" albumbgimgid=\"\" snsflag=\"273\" snsbgimgid=\"http://szmmsns.qpic.cn/mmsns/2HibCL9x8HGxjfnzSK4zEiblwegh91AYYxBhnjALCHHctG3M5FrWyiaMVjLs40nopBThPpwtb0roCY/0\" snsbgobjectid=\"13330254115216625842\" mhash=\"0f3d85bd036c653100b5daf3cb1ab030\" mfullhash=\"0f3d85bd036c653100b5daf3cb1ab030\" bigheadimgurl=\"http://wx.qlogo.cn/mmhead/ver_1/RV52ICEKwkLzDs6KndnljtBfYSxnTT9nxD68GAvdOGXbjLxS0XUBYjNfk4VpDVazAKRuvAB3w9z4oMQT6QgPqw/0\" smallheadimgurl=\"http://wx.qlogo.cn/mmhead/ver_1/RV52ICEKwkLzDs6KndnljtBfYSxnTT9nxD68GAvdOGXbjLxS0XUBYjNfk4VpDVazAKRuvAB3w9z4oMQT6QgPqw/96\" ticket=\"v4_000b708f0b040000010000000000d9712677ef696796e9ae327e04631000000050ded0b020927e3c97896a09d47e6e9effc42d6c4f20c96254081b9aa8fe92a45be60783e3a024d97fdd10ac346452a96a122ca0005ff86a0f796a18811964682d051fed83fe1bbc85e0853b5ed10c4a9c333eba81bcd1fe932bb7e6b273471c810815c8e16422b1510453d423e7d0c4d0aea84f018283561a@stranger\" opcode=\"2\" googlecontact=\"\" qrticket=\"\" chatroomusername=\"\" sourceusername=\"\" sourcenickname=\"\" sharecardusername=\"\" sharecardnickname=\"\" cardversion=\"\" extflag=\"0\"><brandlist count=\"0\" ver=\"774271408\"></brandlist></msg>"
|
||||||
|
dom = xml.dom.minidom.parseString(content)
|
||||||
|
encryptusername = dom.documentElement.getAttribute("encryptusername")
|
||||||
|
ticket = dom.documentElement.getAttribute("ticket")
|
||||||
|
scene = dom.documentElement.getAttribute("scene")
|
||||||
|
print(dom.documentElement.getAttribute("encryptusername"))
|
|
@ -1,4 +1,4 @@
|
||||||
|
MT_ALL = 11000
|
||||||
MT_READY_MSG = 11024
|
MT_READY_MSG = 11024
|
||||||
MT_USER_LOGIN_MSG = 11025
|
MT_USER_LOGIN_MSG = 11025
|
||||||
MT_USER_LOGOUT_MSG = 11026
|
MT_USER_LOGOUT_MSG = 11026
|
||||||
|
|
|
@ -69,14 +69,17 @@ class WeChat:
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
def on_recv(self, message):
|
def on_recv(self, message):
|
||||||
|
log.debug("on recv message: %s", message)
|
||||||
msg_type = message["type"]
|
msg_type = message["type"]
|
||||||
extend = message.get("extend", None)
|
extend = message.get("extend", None)
|
||||||
if msg_type == wx_type.MT_USER_LOGIN_MSG:
|
if msg_type == wx_type.MT_USER_LOGIN_MSG:
|
||||||
self.login_status = False
|
self.login_status = True
|
||||||
self.__wait_login_event.set()
|
self.__wait_login_event.set()
|
||||||
self.__login_info = message.get("data", {})
|
self.__login_info = message.get("data", {})
|
||||||
|
log.info("login success, wxid: %s, nickname: %s", self.__login_info["wxid"], self.__login_info["nickname"])
|
||||||
elif msg_type == wx_type.MT_USER_LOGOUT_MSG:
|
elif msg_type == wx_type.MT_USER_LOGOUT_MSG:
|
||||||
self.login_status = False
|
self.login_status = False
|
||||||
|
log.info("logout, pid: %d", self.pid)
|
||||||
|
|
||||||
if extend is not None and extend in self.__req_data_cache:
|
if extend is not None and extend in self.__req_data_cache:
|
||||||
req_data = self.__req_data_cache[extend]
|
req_data = self.__req_data_cache[extend]
|
||||||
|
@ -84,6 +87,7 @@ class WeChat:
|
||||||
del self.__req_data_cache[extend]
|
del self.__req_data_cache[extend]
|
||||||
else:
|
else:
|
||||||
self.__msg_event_emitter.emit(str(msg_type), self, message)
|
self.__msg_event_emitter.emit(str(msg_type), self, message)
|
||||||
|
self.__msg_event_emitter.emit(str(wx_type.MT_ALL), self, message)
|
||||||
|
|
||||||
def wait_login(self, timeout=None):
|
def wait_login(self, timeout=None):
|
||||||
log.info("wait login...")
|
log.info("wait login...")
|
||||||
|
@ -114,7 +118,7 @@ class WeChat:
|
||||||
if extend is not None:
|
if extend is not None:
|
||||||
message["extend"] = extend
|
message["extend"] = extend
|
||||||
message_json = json.dumps(message)
|
message_json = json.dumps(message)
|
||||||
log.debug("communicate wechat pid:%d, data: %s", self.pid, message)
|
log.debug("communicate wechat pid: %d, data: %s", self.pid, message)
|
||||||
return wcprobe.send(self.client_id, message_json)
|
return wcprobe.send(self.client_id, message_json)
|
||||||
|
|
||||||
def __send_sync(self, msg_type, data=None, timeout=10):
|
def __send_sync(self, msg_type, data=None, timeout=10):
|
||||||
|
|
|
@ -47,7 +47,7 @@ def get_logger(name: str) -> logging.Logger:
|
||||||
|
|
||||||
filepath = f'{base_dir}/log-{time_now.strftime(time_format)}.txt'
|
filepath = f'{base_dir}/log-{time_now.strftime(time_format)}.txt'
|
||||||
|
|
||||||
file_handler = logging.FileHandler(filepath, 'a')
|
file_handler = logging.FileHandler(filepath, 'a', encoding='utf-8')
|
||||||
file_handler.setLevel(NTCHAT_LOG)
|
file_handler.setLevel(NTCHAT_LOG)
|
||||||
file_handler.setFormatter(log_formatter)
|
file_handler.setFormatter(log_formatter)
|
||||||
logger.addHandler(file_handler)
|
logger.addHandler(file_handler)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user