2023-04-06 21:09:03 +08:00

105 lines
3.0 KiB
Python

import asyncio
from typing import List, Union, Literal, Optional
from fastapi import WebSocket
from msgspec import json as msgjson
from gsuid_core.logger import logger
from gsuid_core.gs_logger import GsLogger
from gsuid_core.segment import MessageSegment
from gsuid_core.models import Event, Message, MessageSend
class _Bot:
def __init__(self, _id: str, ws: WebSocket):
self.bot_id = _id
self.bot = ws
self.logger = GsLogger(self.bot_id, ws)
self.queue = asyncio.queues.Queue()
self.bg_tasks = set()
async def target_send(
self,
message: Union[Message, List[Message], str, bytes],
target_type: Literal['group', 'direct', 'channel', 'sub_channel'],
target_id: Optional[str],
bot_id: str,
bot_self_id: str,
msg_id: str = '',
at_sender: bool = False,
sender_id: str = '',
):
if isinstance(message, Message):
message = [message]
elif isinstance(message, str):
if message.startswith('base64://'):
message = [MessageSegment.image(message)]
else:
message = [MessageSegment.text(message)]
elif isinstance(message, bytes):
message = [MessageSegment.image(message)]
if at_sender and sender_id:
message.append(MessageSegment.at(sender_id))
send = MessageSend(
content=message,
bot_id=bot_id,
bot_self_id=bot_self_id,
target_type=target_type,
target_id=target_id,
msg_id=msg_id,
)
logger.info(f'[发送消息to] {bot_id} - {target_type} - {target_id}')
await self.bot.send_bytes(msgjson.encode(send))
async def _process(self):
while True:
data = await self.queue.get()
asyncio.create_task(data)
self.queue.task_done()
class Bot:
def __init__(self, bot: _Bot, ev: Event):
self.bot = bot
self.ev = ev
self.logger = self.bot.logger
self.bot_id = ev.bot_id
self.bot_self_id = ev.bot_self_id
async def send(
self,
message: Union[Message, List[Message], str, bytes],
at_sender: bool = False,
):
return await self.bot.target_send(
message,
self.ev.user_type,
self.ev.group_id if self.ev.group_id else self.ev.user_id,
self.ev.bot_id,
self.bot_self_id,
self.ev.msg_id,
at_sender,
self.ev.user_id,
)
async def target_send(
self,
message: Union[Message, List[Message], str, bytes],
target_type: Literal['group', 'direct', 'channel', 'sub_channel'],
target_id: Optional[str],
at_sender: bool = False,
sender_id: str = '',
):
return await self.bot.target_send(
message,
target_type,
target_id,
self.ev.bot_id,
self.ev.bot_self_id,
self.ev.msg_id,
at_sender,
sender_id,
)