socket-bot/main.py

200 lines
5.5 KiB
Python
Raw Normal View History

2024-01-07 18:48:44 +08:00
import time
2024-01-25 22:08:02 +08:00
import json
2024-01-07 18:48:44 +08:00
import random
import asyncio
import argparse
import traceback
2024-01-25 22:08:02 +08:00
2024-01-07 18:48:44 +08:00
from typing import Dict, List, Tuple, Any
2024-01-25 22:08:02 +08:00
import aiohttp
2024-01-07 18:48:44 +08:00
import socketio
from colorama import Fore
from nacl.signing import SigningKey
2024-01-25 22:08:02 +08:00
2024-01-07 18:48:44 +08:00
# from lib_not_dr.types import Options
from lib_not_dr.loggers import config
2024-01-07 18:55:22 +08:00
from data_struct import Message, ReplyMessage, get_config, BotConfig, BotStatus
2024-01-07 18:48:44 +08:00
2024-01-25 22:08:02 +08:00
_version_ = "0.2.3"
2024-01-07 18:48:44 +08:00
2024-01-25 22:08:02 +08:00
logger = config.get_logger("icalingua")
2024-01-07 18:48:44 +08:00
BOTCONFIG: BotConfig = get_config()
2024-01-25 22:08:02 +08:00
if __name__ == "__main__":
2024-01-07 18:48:44 +08:00
# --debug
# --config=config.toml
# -n --no-notice
parser = argparse.ArgumentParser()
2024-01-25 22:08:02 +08:00
parser.add_argument("-d", "--debug", action="store_true")
parser.add_argument("-n", "--no-notice", action="store_true")
parser.add_argument("-c", "--config", type=str)
2024-01-07 18:48:44 +08:00
args = parser.parse_args()
if args.debug:
logger.global_level = 0
if args.config:
# global BOTCONFIG
BOTCONFIG: BotConfig = get_config(args.config)
if args.no_notice:
BOTCONFIG.notice_start = False
BotStatus = BotStatus()
sio: socketio.AsyncClient = socketio.AsyncClient()
2024-01-25 22:08:02 +08:00
@sio.on("connect") # type: ignore
2024-01-07 18:48:44 +08:00
def connect():
2024-01-25 22:08:02 +08:00
logger.info(f"{Fore.GREEN}icalingua 已连接")
2024-01-07 18:48:44 +08:00
2024-01-25 22:08:02 +08:00
@sio.on("requireAuth") # type: ignore
2024-01-07 18:48:44 +08:00
async def require_auth(salt: str, versions: Dict[str, str]):
logger.info(f"{Fore.BLUE}versions: {versions}\n{type(salt)}|{salt=}")
# 准备数据
sign = SigningKey(bytes.fromhex(BOTCONFIG.private_key))
signature = sign.sign(bytes.fromhex(salt))
2024-01-25 22:08:02 +08:00
2024-01-07 18:48:44 +08:00
# 发送数据
2024-01-25 22:08:02 +08:00
await sio.emit("auth", signature.signature)
2024-01-07 18:48:44 +08:00
logger.info(f"{Fore.BLUE}send auth emit")
2024-01-25 22:08:02 +08:00
2024-01-07 18:48:44 +08:00
# @sio.on('requireAuth')
# def require_auth(*data: Dict[str, Any]):
# logger.info(f"{Fore.BLUE}requireAuth: {data}")
2024-01-25 22:08:02 +08:00
@sio.on("auth") # type: ignore
2024-01-07 18:48:44 +08:00
def auth(data: Dict[str, Any]):
logger.info(f"auth: {data}")
2024-01-25 22:08:02 +08:00
@sio.on("authFailed") # type: ignore
2024-01-07 18:48:44 +08:00
async def auth_failed():
logger.info(f"{Fore.RED}authFailed")
await sio.disconnect()
2024-01-25 22:08:02 +08:00
@sio.on("authSucceed") # type: ignore
2024-01-07 18:48:44 +08:00
def auth_succeed():
logger.info(f"{Fore.GREEN}authSucceed")
2024-01-25 22:08:02 +08:00
@sio.on("connect_error") # type: ignore
2024-01-07 18:48:44 +08:00
def connect_error(*args, **kwargs):
logger.info(f"连接错误 {args}, {kwargs}")
2024-01-25 22:08:02 +08:00
@sio.on("updateRoom") # type: ignore
2024-01-07 18:48:44 +08:00
def update_room(data: Dict[str, Any]):
logger.info(f"{Fore.CYAN}update_room: {data}")
2024-01-25 22:08:02 +08:00
@sio.on("addMessage") # type: ignore
2024-01-07 18:48:44 +08:00
async def add_message(data: Dict[str, Any]):
logger.info(f"{Fore.MAGENTA}add_message: {data}")
2024-01-25 22:08:02 +08:00
is_self = data["message"]["senderId"] == BOTCONFIG.self_id
sender_name = data["message"]["username"]
sender_id = data["message"]["senderId"]
content = data["message"]["content"]
room_id = data["roomId"]
msg_id = data["message"]["_id"]
2024-01-07 18:48:44 +08:00
2024-01-25 22:08:02 +08:00
reply_msg = Message(content="", room_id=room_id, reply_to=ReplyMessage(id=msg_id))
2024-01-07 18:48:44 +08:00
if not is_self:
route(content, sio)
2024-01-25 22:08:02 +08:00
@sio.on("deleteMessage") # type: ignore
2024-01-07 18:48:44 +08:00
def delete_message(message_id: str):
logger.info(f"{Fore.MAGENTA}delete_message: {message_id}")
2024-01-25 22:08:02 +08:00
@sio.on("setMessages") # type: ignore
2024-01-07 18:48:44 +08:00
def set_messages(data: Dict[str, Any]):
2024-01-25 22:08:02 +08:00
logger.info(
f"{Fore.YELLOW}set_messages: {data}\nmessage_len: {len(data['messages'])}"
)
2024-01-07 18:48:44 +08:00
async def notice_startup(room_list: List[int]):
for notice_room in BOTCONFIG.notice_room:
if notice_room in room_list:
2024-01-25 22:08:02 +08:00
notice_message = Message(
content=f"ica bot v{_version_}", room_id=notice_room
)
await sio.emit("sendMessage", notice_message.to_json())
2024-01-07 18:48:44 +08:00
BotStatus.inited = True
2024-01-25 22:08:02 +08:00
logger.info("inited", tag="notice room")
2024-01-07 18:48:44 +08:00
else:
2024-01-25 22:08:02 +08:00
logger.warn(f"未找到通知房间: {notice_room}", tag="notice room")
2024-01-07 18:48:44 +08:00
await asyncio.sleep(random.randint(2, 5))
2024-01-25 22:08:02 +08:00
@sio.on("setAllRooms") # type: ignore
2024-01-07 18:48:44 +08:00
async def set_all_rooms(rooms: List[Dict[str, Any]]):
BotStatus.running = True
2024-01-25 22:08:02 +08:00
room_list: List[int] = [room.get("roomId") for room in rooms] # type: ignore
2024-01-07 18:48:44 +08:00
if not BotStatus.inited:
2024-01-25 22:08:02 +08:00
logger.info("initing...", tag="setAllRooms")
logger.debug(f"room_list: {room_list}", tag="setAllRooms")
2024-01-07 18:48:44 +08:00
if BOTCONFIG.notice_start:
await notice_startup(room_list)
if room_list != BotStatus.rooms:
logger.info(f"{Fore.YELLOW}set_all_rooms: {rooms}\nlen: {len(rooms)}\n")
BotStatus.rooms = room_list
2024-01-25 22:08:02 +08:00
logger.info(f"更新房间: {room_list}", tag="setAllRooms")
2024-01-07 18:48:44 +08:00
2024-01-25 22:08:02 +08:00
@sio.on("setAllChatGroups") # type: ignore
2024-01-07 18:48:44 +08:00
def set_all_chat_groups(groups: List[Dict[str, Any]]):
logger.info(f"{Fore.YELLOW}set_all_chat_groups: {groups}\nlen: {len(groups)}\n")
2024-01-25 22:08:02 +08:00
@sio.on("notify") # type: ignore
2024-01-07 18:48:44 +08:00
def notify(data: List[Tuple[str, Any]]):
logger.info(f"notify: {data}")
2024-01-25 22:08:02 +08:00
@sio.on("closeLoading") # type: ignore
2024-01-07 18:48:44 +08:00
def close_loading(_):
logger.info(f"{Fore.GREEN}close_loading")
2024-01-25 22:08:02 +08:00
@sio.on("onlineData") # type: ignore
2024-01-07 18:48:44 +08:00
def online_data(data: Dict[str, Any]):
logger.info(f"{Fore.GREEN}online_data: {data}")
2024-01-25 22:08:02 +08:00
@sio.on("*") # type: ignore
2024-01-07 18:48:44 +08:00
def catch_all(event, data):
logger.info(f"{Fore.RED}catch_all: {event}|{data}")
async def main():
"""
while True:
await self.eio.wait()
await self.sleep(1) # give the reconnect task time to start up
if not self._reconnect_task:
break
await self._reconnect_task
if self.eio.state != 'connected':
break
"""
await sio.connect(BOTCONFIG.host)
await sio.wait()
2024-01-25 22:08:02 +08:00
if __name__ == "__main__":
2024-01-07 18:48:44 +08:00
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("KeyboardInterrupt")
except Exception:
logger.error(traceback.format_exc())