添加模糊查询联系人和发送xml接口

This commit is contained in:
evilbeast 2022-09-04 20:40:45 +08:00
parent 0c19723833
commit f326669603
7 changed files with 153 additions and 4 deletions

View File

@ -1,6 +1,6 @@
<h1 align="center">NtChat</h1>
<p align="center">
<a href="https://github.com/smallevilbeast/ntchat/releases"><img src="https://img.shields.io/badge/release-0.1.6-blue.svg?" alt="release"></a>
<a href="https://github.com/smallevilbeast/ntchat/releases"><img src="https://img.shields.io/badge/release-0.1.7-blue.svg?" alt="release"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-brightgreen.svg?" alt="License"></a>
</p>

View File

@ -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()

View File

@ -1,4 +1,4 @@
VERSION = '0.1.6'
VERSION = '0.1.7'
LOG_LEVEL = "DEBUG"
LOG_KEY = 'NTCHAT_LOG'

View File

@ -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

View File

@ -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

View File

@ -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):
"""
同意加好友请求

View File

@ -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',