From f326669603a6def999023e2195e4de103d3a75ca Mon Sep 17 00:00:00 2001 From: evilbeast Date: Sun, 4 Sep 2022 20:40:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A8=A1=E7=B3=8A=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E8=81=94=E7=B3=BB=E4=BA=BA=E5=92=8C=E5=8F=91=E9=80=81?= =?UTF-8?q?xml=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- examples/search_contacts.py | 46 ++++++++++++++++++++++ ntchat/conf/__init__.py | 2 +- ntchat/const/notify_type.py | 17 ++++++++ ntchat/const/send_type.py | 11 +++++- ntchat/core/wechat.py | 77 +++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 7 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 examples/search_contacts.py diff --git a/README.md b/README.md index 6005381..8dbcc25 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

NtChat

- release + release License

diff --git a/examples/search_contacts.py b/examples/search_contacts.py new file mode 100644 index 0000000..2987536 --- /dev/null +++ b/examples/search_contacts.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +import sys +import time +import ntchat + + +def version_tuple(v): + return tuple(map(int, (v.split(".")))) + + +if version_tuple(ntchat.__version__) < version_tuple('0.1.7'): + print("error: ntchat version required 0.1.7, use `pip install -U ntchat` to upgrade") + sys.exit() + +wechat = ntchat.WeChat() + +# 打开pc微信, smart: 是否管理已经登录的微信 +wechat.open(smart=True) + +# 等待登录 +wechat.wait_login() + +# 根据wxid模糊查询查询联系人 +contacts = wechat.search_contacts(wxid="wxid_") +print(contacts) + +# 根据微信号模糊查询联系人 +# contacts = wechat.search_contacts(account="") + + +# 根据昵称模糊查询联系人, 如昵称包含`小`的联系人 +contacts = wechat.search_contacts(nickname="小") +print(contacts) + +# 根据备注查询联系人 +contacts = wechat.search_contacts(remark="备注") +print(contacts) + + +# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码 +try: + while True: + time.sleep(0.5) +except KeyboardInterrupt: + ntchat.exit_() + sys.exit() diff --git a/ntchat/conf/__init__.py b/ntchat/conf/__init__.py index d55ea8c..685af60 100644 --- a/ntchat/conf/__init__.py +++ b/ntchat/conf/__init__.py @@ -1,4 +1,4 @@ -VERSION = '0.1.6' +VERSION = '0.1.7' LOG_LEVEL = "DEBUG" LOG_KEY = 'NTCHAT_LOG' diff --git a/ntchat/const/notify_type.py b/ntchat/const/notify_type.py index 7220f38..e1d017f 100644 --- a/ntchat/const/notify_type.py +++ b/ntchat/const/notify_type.py @@ -65,5 +65,22 @@ MT_RECV_OTHER_MSG = 11060 # 未知应用消息通知 MT_RECV_OTHER_APP_MSG = 11061 +# 群成员新增通知 +MT_ROOM_ADD_MEMBER_NOTIFY_MSG = 11098 + +# 群成员删除通知 +MT_ROOM_DEL_MEMBER_NOTIFY_MSG = 11099 + +# 通过接口创建群聊的通知 +MT_ROOM_CREATE_NOTIFY_MSG = 11100 + +# 退群或被踢通知 +MT_ROOM_DEL_NOTIFY_MSG = 11101 + +# 联系人新增通知 +MT_CONTACT_ADD_NOITFY_MSG = 11102 + +# 联系人删除通知 +MT_CONTACT_DEL_NOTIFY_MSG = 11103 diff --git a/ntchat/const/send_type.py b/ntchat/const/send_type.py index 6a9032c..05b8c5a 100644 --- a/ntchat/const/send_type.py +++ b/ntchat/const/send_type.py @@ -11,7 +11,10 @@ MT_GET_ROOMS_MSG = 11031 MT_GET_ROOM_MEMBERS_MSG = 11032 # 获取指定联系人的详细信息 -MT_GET_CONTACT_DETAIL_MSG = 11034 +MT_GET_CONTACT_DETAIL_MSG = 11029 + +# 获取指定群的详细信息 +MT_GET_ROOM_DETAIL_MSG = 11125 # 发送文本消息 MT_SEND_TEXT_MSG = 11036 @@ -37,6 +40,9 @@ MT_SEND_VIDEO_MSG = 11042 # 发送gif消息 MT_SEND_GIF_MSG = 11043 +# 发送xml消息 +MT_SEND_XML_MSG = 11113 + # 接受新好友请求 MT_ACCEPT_FRIEND_MSG = 11065 @@ -63,3 +69,6 @@ MT_QUIT_DEL_ROOM_MSG = 11077 # 添加群成员为好友 MT_ADD_FRIEND_MSG = 11062 + +# 数据库查询 +MT_SQL_QUERY_MSG = 11027 diff --git a/ntchat/core/wechat.py b/ntchat/core/wechat.py index 4181809..ec0cdee 100644 --- a/ntchat/core/wechat.py +++ b/ntchat/core/wechat.py @@ -66,6 +66,7 @@ class WeChat: for event in msg_type: self.on(event, f) return f + return wrapper def on_close(self): @@ -152,6 +153,16 @@ class WeChat: def __repr__(self): return f"WeChatInstance(pid: {self.pid}, client_id: {self.client_id})" + def sql_query(self, sql: str, db: int): + """ + 数据库查询 + """ + data = { + "sql": sql, + "db": db + } + return self.__send_sync(send_type.MT_SQL_QUERY_MSG, data) + def get_login_info(self): """ 获取登录信息 @@ -171,17 +182,72 @@ class WeChat: return self.__send_sync(send_type.MT_GET_CONTACTS_MSG) def get_contact_detail(self, wxid): + """ + 获取联系人详细信息 + """ data = { "wxid": wxid } return self.__send_sync(send_type.MT_GET_CONTACT_DETAIL_MSG, data) + def search_contacts(self, + wxid: Union[None, str] = None, + account: Union[None, str] = None, + nickname: Union[None, str] = None, + remark: Union[None, str] = None): + """ + 根据wxid、微信号、昵称和备注模糊搜索联系人 + """ + conds = {} + if wxid: + conds["username"] = wxid + if account: + conds["alias"] = account + if nickname: + conds["nickname"] = nickname + if remark: + conds["remark"] = remark + if not conds: + return [] + + cond_pairs = [] + for k, v in conds.items(): + cond_pairs.append(f"{k} like '%{v}%'") + + cond_str = " or ".join(cond_pairs) + sql = f"select username from contact where {cond_str}" + message = self.sql_query(sql, 1) + print(message) + if not message: + return [] + + result = message["result"] + if not result: + return [] + + contacts = [] + for wxid_list in result: + if len(wxid_list) > 0: + wxid = wxid_list[0] + contact = self.get_contact_detail(wxid) + contacts.append(contact) + return contacts + def get_rooms(self): """ 获取群列表 """ return self.__send_sync(send_type.MT_GET_ROOMS_MSG) + def get_room_detail(self, room_wxid): + """ + 获取指定群详细信息 + """ + data = { + "room_wxid": room_wxid + } + return self.__send_sync(send_type.MT_GET_ROOM_DETAIL_MSG, data) + def get_room_members(self, room_wxid: str): """ 获取群成员列表 @@ -276,6 +342,17 @@ class WeChat: } return self.__send(send_type.MT_SEND_GIF_MSG, data) + def send_xml(self, to_wxid, xml, app_type=5): + """ + 发送xml消息 + """ + data = { + "to_wxid": to_wxid, + "xml": xml, + "app_type": app_type + } + return self.__send(send_type.MT_SEND_XML_MSG, data) + def accept_friend_request(self, encryptusername: str, ticket: str, scene: int): """ 同意加好友请求 diff --git a/setup.py b/setup.py index 1b61e36..872cdfd 100644 --- a/setup.py +++ b/setup.py @@ -194,7 +194,7 @@ extension.extra_compile_cpp_args = extra_compile_cpp_args[target_os] setup( name='ntchat', - version='0.1.6', + version='0.1.7', description='About Conversational RPA SDK for Chatbot Makers', long_description="", long_description_content_type='text/markdown',