-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (95 loc) · 3.37 KB
/
main.py
File metadata and controls
112 lines (95 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import asyncio
import json
import base64
from pathlib import Path
from src.config import Config
from src.connection.luma_client import WebSocketConnection
from src.send_handler.message_send_handler import LumaSendHandler
# from src.api_handler.api import LumaAPI
from src.logger import logger
running_flag = asyncio.Event()
async def dispatch_content(message: str | bytes):
"""分发消息内容到不同的处理器"""
if isinstance(message, bytes):
logger.critical("收到二进制消息,当前版本仅支持文本消息")
return
raw_dict = json.loads(message)
logger.info(f"分发消息内容: {raw_dict}")
async def main(config: Config):
client = WebSocketConnection(
running_flag=running_flag,
token=config.luma_client.token,
server=config.luma_client.server,
port=config.luma_client.port,
)
# send_handler = LumaSendHandler(client)
await client.connect()
client.set_handler(dispatch_content)
await asyncio.sleep(1) # 等待连接稳定
try:
# await test_send_text(send_handler)
# await test_send_image(send_handler)
await client.listen()
except Exception as e:
logger.error(f"连接或监听出现错误: {e}")
await client.disconnect()
async def cleanup():
logger.info("正在执行清理操作...")
running_flag.set()
pending_tasks = [task for task in asyncio.all_tasks() if task is not asyncio.current_task()]
for task in pending_tasks:
task.cancel()
try:
await asyncio.wait_for(asyncio.gather(*pending_tasks, return_exceptions=True), timeout=10)
except asyncio.TimeoutError:
logger.warning("清理任务超时,强制退出")
return
logger.info("清理完成,程序即将退出")
async def test_send_text(send_handler: LumaSendHandler):
await send_handler.send_payload(
"send_msg",
{
"message_type": "group",
"group_id": "1081372778",
"message": [
{"type": "text", "data": {"text": "这是一个测试消息"}},
],
},
)
async def test_send_image(send_handler: LumaSendHandler):
# 读取图片并转换为base64
image_path = Path(__file__).parent / "test" / "111.jpeg"
with open(image_path, "rb") as f:
image_data = f.read()
base64_content = base64.b64encode(image_data).decode("utf-8")
await send_handler.send_payload(
"send_msg",
{
"message_type": "group",
"group_id": "1036092828",
"message": [
{
"type": "image",
"data": {
"summary": "我喜欢你,和我结婚吧!",
"file": f"base64://{base64_content}",
},
}
],
},
)
if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
from src.config import load_config_from_file
config = load_config_from_file(Path(__file__).parent / "config.toml")
try:
loop.run_until_complete(main(config))
except OSError as e:
logger.error(f"网络错误: {e}")
except KeyboardInterrupt:
logger.info("程序已被用户中断")
finally:
loop.run_until_complete(cleanup())
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()