Skip to content

Commit 5881f97

Browse files
♻️ refactor sender.send_message to welcome call_api function
1 parent 126cb04 commit 5881f97

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

send_test.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import asyncio
1111
from src.sender import sender
1212

13-
asyncio.run(sender.send_message('test4', 2560359315))
14-
asyncio.run(sender.send_message('test5', 2560359315, 798891715))
15-
asyncio.run(sender.send_message('test6', group_id=798891715))
16-
asyncio.run(sender.send_message('test7', None, None))
13+
# asyncio.run(sender.send_message('test4', 2560359315))
14+
# asyncio.run(sender.send_message('test5', 2560359315, 798891715))
15+
# asyncio.run(sender.send_message('test6', group_id=798891715))
16+
# asyncio.run(sender.send_message('test7', None, None))
17+
asyncio.run(sender.call_api('get_stranger_info', {'user_id': 2560359315}))
18+
asyncio.run(sender.call_api('set_group_card', {'group_id': 798891715, 'user_id': 2560359315, 'card': 'test'}))

src/sender.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,46 @@
99
ws_config = user_config["ws"]
1010

1111

12-
def get_request_body(message, user_id: int = -1, group_id: int = -1):
12+
def _get_params(message, user_id: int = -1, group_id: int = -1) -> dict:
1313
'''
14-
Get request body for sending message.
15-
Return:
16-
request_body: str
17-
echo_message: str
14+
Get params for sending message.
1815
'''
19-
echo_message = f"send_mannually_by_cmd_{time.time()}"
20-
request_body = {
21-
"action": "send_msg",
22-
"params": {
23-
"message": message,
24-
},
25-
"echo": echo_message
26-
}
16+
params = {"message": message}
2717
# group_id is more important than user_id
2818
if group_id != -1:
29-
request_body["params"]["group_id"] = group_id
19+
params["group_id"] = group_id
3020
elif user_id != -1:
31-
request_body["params"]["user_id"] = user_id
32-
return json.dumps(request_body), echo_message
21+
params["user_id"] = user_id
22+
return params
3323

3424
class Sender:
35-
async def send_message(self, message, user_id: int = -1, group_id: int = -1):
36-
# Why to set default value to -1? To keep the type of user_id and group_id are int.
37-
request_body, echo_message = get_request_body(message, user_id, group_id)
25+
async def _call_api(self, action: str, params: dict, echo_message: str = "call_mannually_by_cmd", timeout: int = 30, print_response: bool = True) -> dict:
26+
start_time = time.time()
27+
echo_message += f"_{start_time}"
28+
request_body = {
29+
"action": action,
30+
"params" : params,
31+
"echo": echo_message
32+
}
3833
async with websockets.connect(
3934
f"ws://localhost:{ws_config['port-send']}"
4035
) as websocket:
41-
await websocket.send(request_body)
36+
await websocket.send(json.dumps(request_body))
4237
while True: # wait for response
4338
response = await websocket.recv()
4439
response = json.loads(response)
4540
if response.get("echo") == echo_message:
4641
break
47-
response = json.dumps(response, sort_keys=True, indent=4)
48-
print(f"Response < \n{response}")
42+
if time.time() - start_time > timeout:
43+
raise TimeoutError(f"Timeout when calling {action}")
44+
response_str = json.dumps(response, sort_keys=True, indent=4)
45+
if print_response:
46+
print(f"\033[1;36mResponse <\033[0m \n{response_str}")
47+
return response
48+
49+
async def send_message(self, message, user_id: int = -1, group_id: int = -1) -> None:
50+
# Why to set default value to -1? To keep the type of user_id and group_id are int.
51+
await self._call_api('send_msg', _get_params(message, user_id, group_id), "send_mannuall_by_cmd")
4952

5053

5154
sender = Sender()

0 commit comments

Comments
 (0)