Skip to content

Commit

Permalink
优化异常输出
Browse files Browse the repository at this point in the history
  • Loading branch information
smallevilbeast committed Oct 9, 2022
1 parent 38506a0 commit 330f072
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.16-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>

Expand Down
11 changes: 11 additions & 0 deletions fastapi_example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,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()
Expand Down
4 changes: 4 additions & 0 deletions fastapi_example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class GetRoomMembersReqModel(ClientReqModel):
room_wxid: str


class GetRoomNameReqModel(ClientReqModel):
room_wxid: str


class CreateRoomReqModel(ClientReqModel):
member_list: List[str]

Expand Down
2 changes: 1 addition & 1 deletion ntchat/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = '0.1.15'
VERSION = '0.1.16'

LOG_LEVEL = "DEBUG"
LOG_KEY = 'NTCHAT_LOG'
Expand Down
15 changes: 11 additions & 4 deletions ntchat/core/mgr.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,9 +31,16 @@ def set_wechat_exe_path(self, wechat_exe_path=None, wechat_version=None):
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)

Expand Down
10 changes: 10 additions & 0 deletions ntchat/core/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,13 @@ def modify_friend_remark(self, wxid: str, remark: str):
"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 ''
4 changes: 4 additions & 0 deletions ntchat/exception/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class WeChatBindError(Exception):

class WeChatNotLoginError(Exception):
pass


class WeChatRuntimeError(Exception):
pass
13 changes: 9 additions & 4 deletions ntchat/utils/xdg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import os.path
from ntchat.wc import SUPPORT_VERSIONS


def get_exec_dir():
Expand All @@ -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

3 changes: 3 additions & 0 deletions ntchat/wc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUPPORT_VERSIONS = [
'3.6.0.18'
]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def add_prefix(l, prefix):

setup(
name='ntchat',
version='0.1.15',
version='0.1.16',
description='About Conversational RPA SDK for Chatbot Makers',
long_description="",
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 330f072

Please sign in to comment.