Skip to content

Commit 5d28acf

Browse files
committed
update api
1 parent d29e083 commit 5d28acf

File tree

4 files changed

+22
-30
lines changed

4 files changed

+22
-30
lines changed

wechatrobot/Api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Api:
77
port : int = 18888
88

99
def IsLoginIn(self , **params) -> bool:
10-
return self.post(WECHAT_IS_LOGIN , IsLoginInBody(**params))
10+
return self.post(WECHAT_IS_LOGIN , IsLoginBody(**params))
1111

1212
def GetSelfInfo(self , **params):
1313
return self.post(WECHAT_GET_SELF_INFO , GetSelfInfoBody(**params))
@@ -133,7 +133,7 @@ def GetQrcodeImage(self , **params):
133133
return self.post(WECHAT_GET_QRCODE_IMAGE , GetQrcodeImageBody(**params))
134134

135135
def post(self , type : int, params : Body):
136-
return json.loads(requests.post( f"http://127.0.0.1:{self.port}/api/?type={type}", data = params.json()).text)
136+
return requests.post( f"http://127.0.0.1:{self.port}/api/?type={type}", data = params.json()).text
137137

138138
def exec_command(self , item: str) -> Callable:
139139
return eval(f"self.{item}")

wechatrobot/Bus.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Callable, List, Any
22
from collections import defaultdict
3-
from .Utils import run_funcs
43

54
class EventBus:
65
def __init__(self):
@@ -10,4 +9,7 @@ def subscribe(self, event: str, func: Callable) -> None:
109
self._subscribers[event].add(func)
1110

1211
def emit(self, event: str, *args, **kwargs) -> List[Any]:
13-
return run_funcs(self._subscribers[event], *args,**kwargs)
12+
results = []
13+
for f in self._subscribers[event]:
14+
results.append(f(*args, **kwargs))
15+
return results

wechatrobot/Utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
from typing import Any, Callable, Awaitable, Iterable, List
21

3-
def run_funcs(funcs: Iterable[Callable[..., Awaitable[Any]]], *args,
4-
**kwargs) -> List[Any]:
5-
results = []
6-
for f in funcs:
7-
results.append(f(*args, **kwargs))
8-
return results

wechatrobot/WeChatRobot.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ def __init__(self , ip : str = "0.0.0.0" , port : int = 23456):
1515
self.ip = ip
1616
self.port = port
1717
self.api = Api()
18-
#StartHook
19-
print(self.StartMsgHook(port = port))
20-
print(self.StartImageHook(save_path = ""))
21-
print(self.StartVoiceHook(save_path = ""))
2218

2319
self.url = "http://{}:{}/".format(ip , port)
2420

@@ -30,13 +26,25 @@ def deco(func: Callable) -> Callable:
3026
return deco
3127

3228
def run(self , main_thread : bool = True):
33-
def receive_callback(data):
34-
return self.post(data)
35-
29+
#StartHook
30+
self.StartMsgHook(port = self.port)
31+
self.StartImageHook(save_path = "")
32+
self.StartVoiceHook(save_path = "")
33+
3634
class ReceiveMsgSocketServer(socketserver.BaseRequestHandler):
3735
def __init__(self, *args, **kwargs):
3836
super().__init__(*args, **kwargs)
3937

38+
def receive_callback(msg):
39+
if 1 == msg["isSendMsg"]:
40+
Bus.emit("self_msg", msg)
41+
elif "chatroom" in msg["sender"]:
42+
Bus.emit("group_msg", msg)
43+
elif "gh" in msg["sender"]:
44+
Bus.emit("public_msg", msg)
45+
else:
46+
Bus.emit("friend_msg", msg)
47+
4048
def handle(self):
4149
conn = self.request
4250
while True:
@@ -49,14 +57,7 @@ def handle(self):
4957
conn.sendall("200 OK".encode())
5058
break
5159
msg = json.loads(ptr_data.decode('utf-8'))
52-
53-
if 1 == msg["isSendMsg"]:
54-
Bus.emit("self_msg", msg)
55-
elif "chatroom" in msg["sender"]:
56-
Bus.emit("group_msg", msg)
57-
else:
58-
Bus.emit("friend_msg", msg)
59-
60+
receive_callback(msg)
6061
except OSError:
6162
break
6263
except json.JSONDecodeError:
@@ -79,10 +80,6 @@ def handle(self):
7980
print(e)
8081
return None
8182

82-
# on_friend_msg = on("friend_msg")
83-
# on_group_msg = on("group_msg")
84-
# on_self_msg = on("self_msg")
85-
8683
def __getattr__(self , item : str):
8784
return self.api.exec_command(item)
8885

0 commit comments

Comments
 (0)