mirror of
https://github.com/smallevilbeast/ntchat.git
synced 2025-06-04 01:26:07 +08:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
da155c82e3 | ||
![]() |
92364c1e65 | ||
![]() |
90c824d0b0 | ||
![]() |
330f0724f3 | ||
![]() |
38506a0e7b | ||
![]() |
f7fb727257 |
|
@ -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.15-blue.svg?" alt="release"></a>
|
||||
<a href="https://github.com/smallevilbeast/ntchat/releases"><img src="https://img.shields.io/badge/release-0.1.17-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>
|
||||
|
||||
|
@ -19,8 +19,7 @@
|
|||
## 帮助文档
|
||||
- 查看 [常见问题](docs/FAQ.md)
|
||||
- 查看 [常用示例](examples)
|
||||
- 查看 [NtChatHttp接口示例](fastapi_example)
|
||||
- 加入群聊 [PyXCGUI&NtChat交流群](https://jq.qq.com/?_wv=1027&k=oIXzbTbI)
|
||||
- 查看 [NtChatHttp接口示例](fastapi_example)
|
||||
- 查看 [PyXCGUI项目](https://github.com/smallevilbeast/pyxcgui)
|
||||
|
||||
## 安装
|
||||
|
@ -202,7 +201,3 @@ if __name__ == '__main__':
|
|||
app.exit()
|
||||
|
||||
```
|
||||
|
||||
帮助&支持
|
||||
-------------------------
|
||||
点击链接加入群聊 [PyXCGUI&NtChat交流群](https://jq.qq.com/?_wv=1027&k=oIXzbTbI)
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
import time
|
||||
import ntchat
|
||||
|
||||
wechat = ntchat.WeChat()
|
||||
|
||||
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||
wechat.open(smart=True)
|
||||
|
||||
# 等待登录
|
||||
wechat.wait_login()
|
||||
|
||||
# 获取群列表并输出
|
||||
room_wxid = wechat.get_rooms()[0]["wxid"]
|
||||
|
||||
|
||||
def get_room_name(wechat: ntchat.WeChat, room_wxid: str):
|
||||
sql = f"select nickname from contact where username='{room_wxid}'"
|
||||
result = wechat.sql_query(sql, 1)["result"]
|
||||
if result:
|
||||
return result[0][0]
|
||||
return None
|
||||
|
||||
|
||||
print("群名是: ", get_room_name(wechat, room_wxid))
|
||||
|
||||
# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
|
||||
try:
|
||||
while True:
|
||||
time.sleep(0.5)
|
||||
except KeyboardInterrupt:
|
||||
ntchat.exit_()
|
||||
sys.exit()
|
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import uvicorn
|
||||
import threading
|
||||
from functools import wraps
|
||||
from fastapi import FastAPI
|
||||
from mgr import ClientManager
|
||||
|
@ -56,8 +57,14 @@ async def client_create():
|
|||
response_model=models.ResponseModel)
|
||||
@catch_exception()
|
||||
async def client_open(model: models.ClientOpenReqModel):
|
||||
ret = client_mgr.get_client(model.guid).open(model.smart, model.show_login_qrcode)
|
||||
return response_json(1 if ret else 0)
|
||||
client = client_mgr.get_client(model.guid)
|
||||
ret = client.open(model.smart, model.show_login_qrcode)
|
||||
|
||||
# 当show_login_qrcode=True时, 打开微信时会显示二维码界面
|
||||
if model.show_login_qrcode:
|
||||
client.qrcode_event = threading.Event()
|
||||
client.qrcode_event.wait(timeout=10)
|
||||
return response_json(1 if ret else 0, {'qrcode': client.qrcode})
|
||||
|
||||
|
||||
@app.post("/global/set_callback_url", summary="设置接收通知地址", tags=["Global"],
|
||||
|
@ -107,6 +114,17 @@ async def get_rooms(model: models.ClientReqModel):
|
|||
return response_json(1, data)
|
||||
|
||||
|
||||
@app.post("/room/get_name_name", summary="获取群名称", tags=["Room"],
|
||||
response_model=models.ResponseModel)
|
||||
@catch_exception()
|
||||
async def get_rooms(model: models.GetRoomNameReqModel):
|
||||
name = client_mgr.get_client(model.guid).get_room_name(model.room_wxid)
|
||||
data = {
|
||||
"name": name
|
||||
}
|
||||
return response_json(1, data)
|
||||
|
||||
|
||||
@app.post("/room/get_room_members", summary="获取群成员列表", tags=["Room"],
|
||||
response_model=models.ResponseModel)
|
||||
@catch_exception()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import ntchat
|
||||
import threading
|
||||
import requests
|
||||
from typing import Dict, Union
|
||||
from ntchat.utils.singleton import Singleton
|
||||
|
@ -9,6 +10,8 @@ from exception import ClientNotExists
|
|||
|
||||
class ClientWeChat(ntchat.WeChat):
|
||||
guid: str = ""
|
||||
qrcode_event: threading.Event = None
|
||||
qrcode: str = ""
|
||||
|
||||
|
||||
class ClientManager(metaclass=Singleton):
|
||||
|
@ -35,7 +38,7 @@ class ClientManager(metaclass=Singleton):
|
|||
wechat.on(ntchat.MT_RECV_WECHAT_QUIT_MSG, self.__on_quit_callback)
|
||||
return guid
|
||||
|
||||
def get_client(self, guid: str) -> Union[None, ntchat.WeChat]:
|
||||
def get_client(self, guid: str) -> ClientWeChat:
|
||||
client = self.__client_map.get(guid, None)
|
||||
if client is None:
|
||||
raise ClientNotExists(guid)
|
||||
|
@ -45,7 +48,14 @@ class ClientManager(metaclass=Singleton):
|
|||
if guid in self.__client_map:
|
||||
del self.__client_map[guid]
|
||||
|
||||
def __on_callback(self, wechat, message):
|
||||
def __on_callback(self, wechat: ClientWeChat, message: dict):
|
||||
|
||||
# 通知二维码显示
|
||||
msg_type = message['type']
|
||||
if msg_type == ntchat.MT_RECV_LOGIN_QRCODE_MSG and wechat.qrcode_event:
|
||||
wechat.qrcode = message["data"]["code"]
|
||||
wechat.qrcode_event.set()
|
||||
|
||||
if not self.callback_url:
|
||||
return
|
||||
|
||||
|
|
|
@ -87,6 +87,10 @@ class GetRoomMembersReqModel(ClientReqModel):
|
|||
room_wxid: str
|
||||
|
||||
|
||||
class GetRoomNameReqModel(ClientReqModel):
|
||||
room_wxid: str
|
||||
|
||||
|
||||
class CreateRoomReqModel(ClientReqModel):
|
||||
member_list: List[str]
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
VERSION = '0.1.15'
|
||||
VERSION = '0.1.17'
|
||||
|
||||
LOG_LEVEL = "DEBUG"
|
||||
LOG_KEY = 'NTCHAT_LOG'
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import json
|
||||
import os.path
|
||||
from ntchat.wc import wcprobe
|
||||
from ntchat.utils.xdg import get_helper_file
|
||||
from ntchat.exception import WeChatVersionNotMatchError, WeChatBindError
|
||||
from ntchat.wc import wcprobe, SUPPORT_VERSIONS
|
||||
from ntchat.utils.xdg import get_helper_file, is_support_version, has_helper_file
|
||||
from ntchat.exception import WeChatVersionNotMatchError, WeChatBindError, WeChatRuntimeError
|
||||
from ntchat.utils.singleton import Singleton
|
||||
from ntchat.const import notify_type
|
||||
from ntchat.utils.logger import get_logger
|
||||
|
@ -31,9 +31,16 @@ class WeChatMgr(metaclass=Singleton):
|
|||
else:
|
||||
version = wechat_version
|
||||
|
||||
if not is_support_version(version):
|
||||
raise WeChatVersionNotMatchError(f"ntchat support wechat versions: {','.join(SUPPORT_VERSIONS)}")
|
||||
|
||||
if not has_helper_file():
|
||||
raise WeChatRuntimeError('When using pyinstaller to package exe, you need to add the '
|
||||
'`--collect-data=ntchat` parameter')
|
||||
|
||||
helper_file = get_helper_file(version)
|
||||
if not os.path.exists(helper_file):
|
||||
raise WeChatVersionNotMatchError()
|
||||
raise WeChatRuntimeError("missing core files")
|
||||
|
||||
log.info("initialize wechat, version: %s", version)
|
||||
|
||||
|
|
|
@ -62,34 +62,32 @@ class WeChat:
|
|||
WeChatMgr().append_instance(self)
|
||||
self.__wait_login_event = Event()
|
||||
self.__req_data_cache = {}
|
||||
self.__msg_event_emitter = pyee.EventEmitter()
|
||||
self.event_emitter = pyee.EventEmitter()
|
||||
self.__login_info = {}
|
||||
|
||||
def on(self, msg_type, f):
|
||||
return self.__msg_event_emitter.on(str(msg_type), RaiseExceptionFunc(f))
|
||||
|
||||
def msg_register(self, msg_type: Union[int, List[int], Tuple[int]]):
|
||||
if not (isinstance(msg_type, list) or isinstance(msg_type, tuple)):
|
||||
msg_type = [msg_type]
|
||||
for event in msg_type:
|
||||
self.event_emitter.on(str(event), RaiseExceptionFunc(f))
|
||||
|
||||
def msg_register(self, msg_type: Union[int, List[int], Tuple[int]]):
|
||||
def wrapper(f):
|
||||
wraps(f)
|
||||
for event in msg_type:
|
||||
self.on(event, RaiseExceptionFunc(f))
|
||||
self.on(msg_type, f)
|
||||
return f
|
||||
|
||||
return wrapper
|
||||
|
||||
def on_close(self):
|
||||
self.login_status = False
|
||||
self.status = False
|
||||
self.__msg_event_emitter.emit(str(notify_type.MT_RECV_WECHAT_QUIT_MSG), self)
|
||||
self.event_emitter.emit(str(notify_type.MT_RECV_WECHAT_QUIT_MSG), self)
|
||||
|
||||
message = {
|
||||
"type": notify_type.MT_RECV_WECHAT_QUIT_MSG,
|
||||
"data": {}
|
||||
}
|
||||
self.__msg_event_emitter.emit(str(notify_type.MT_ALL), self, message)
|
||||
self.event_emitter.emit(str(notify_type.MT_ALL), self, message)
|
||||
|
||||
def bind_client_id(self, client_id):
|
||||
self.status = True
|
||||
|
@ -113,8 +111,8 @@ class WeChat:
|
|||
req_data.on_response(message)
|
||||
del self.__req_data_cache[extend]
|
||||
else:
|
||||
self.__msg_event_emitter.emit(str(msg_type), self, message)
|
||||
self.__msg_event_emitter.emit(str(notify_type.MT_ALL), self, message)
|
||||
self.event_emitter.emit(str(msg_type), self, message)
|
||||
self.event_emitter.emit(str(notify_type.MT_ALL), self, message)
|
||||
|
||||
def wait_login(self, timeout=None):
|
||||
log.info("wait login...")
|
||||
|
@ -481,3 +479,13 @@ class WeChat:
|
|||
"remark": remark
|
||||
}
|
||||
return self.__send_sync(send_type.MT_MODIFY_FRIEND_REMARK, data)
|
||||
|
||||
def get_room_name(self, room_wxid: str) -> str:
|
||||
"""
|
||||
获取群名
|
||||
"""
|
||||
sql = f"select nickname from contact where username='{room_wxid}'"
|
||||
result = self.sql_query(sql, 1)["result"]
|
||||
if result:
|
||||
return result[0][0]
|
||||
return ''
|
||||
|
|
|
@ -8,3 +8,7 @@ class WeChatBindError(Exception):
|
|||
|
||||
class WeChatNotLoginError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class WeChatRuntimeError(Exception):
|
||||
pass
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import sys
|
||||
import os.path
|
||||
from ntchat.wc import SUPPORT_VERSIONS
|
||||
|
||||
|
||||
def get_exec_dir():
|
||||
|
@ -26,9 +27,13 @@ def get_helper_file(version):
|
|||
return os.path.join(get_wc_dir(), f"helper_{version}.dat")
|
||||
|
||||
|
||||
def get_support_download_url():
|
||||
return 'https://webcdn.m.qq.com/spcmgr/download/WeChat3.6.0.18.exe'
|
||||
def has_helper_file():
|
||||
for name in os.listdir(get_wc_dir()):
|
||||
if name.startswith("helper_"):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(get_helper_file('3.6.0.18'))
|
||||
def is_support_version(version):
|
||||
return version in SUPPORT_VERSIONS
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SUPPORT_VERSIONS = [
|
||||
'3.6.0.18'
|
||||
]
|
|
@ -3,6 +3,7 @@
|
|||
%UserProfile%\.pyenv\pyenv-win\versions\3.8.0-win32\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.9.0-win32\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.10.0-win32\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.11.0-win32\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
|
||||
pushd ..
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.6.0-win32\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
|
@ -10,4 +11,5 @@ pushd ..
|
|||
%UserProfile%\.pyenv\pyenv-win\versions\3.8.0-win32\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.9.0-win32\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.10.0-win32\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.11.0-win32\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
popd
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
%UserProfile%\.pyenv\pyenv-win\versions\3.8.0\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.9.0\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.10.0\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.11.0\python.exe -m pip install --upgrade pip setuptools wheel
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
|
||||
pushd ..
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.6.0\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
|
@ -10,4 +11,5 @@ pushd ..
|
|||
%UserProfile%\.pyenv\pyenv-win\versions\3.8.0\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.9.0\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.10.0\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
%UserProfile%\.pyenv\pyenv-win\versions\3.11.0\python.exe setup.py bdist_wheel -d wheelhouse
|
||||
popd
|
||||
|
|
5
setup.py
5
setup.py
|
@ -194,7 +194,7 @@ extension.extra_compile_cpp_args = extra_compile_cpp_args[target_os]
|
|||
|
||||
setup(
|
||||
name='ntchat',
|
||||
version='0.1.15',
|
||||
version='0.1.17',
|
||||
description='About Conversational RPA SDK for Chatbot Makers',
|
||||
long_description="",
|
||||
long_description_content_type='text/markdown',
|
||||
|
@ -209,7 +209,8 @@ setup(
|
|||
'Programming Language :: Python :: 3.7',
|
||||
'Programming Language :: Python :: 3.8',
|
||||
'Programming Language :: Python :: 3.9',
|
||||
'Programming Language :: Python :: 3.10'
|
||||
'Programming Language :: Python :: 3.10',
|
||||
'Programming Language :: Python :: 3.11'
|
||||
],
|
||||
package_data={"": ["py.typed", "*.pyi", "helper*.dat"]},
|
||||
include_package_data=False,
|
||||
|
|
Loading…
Reference in New Issue
Block a user