app/liteyuki/session/event.py

88 lines
2.5 KiB
Python
Raw Permalink Normal View History

2024-08-19 23:47:39 +08:00
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/19 下午10:47
@Author : snowykami
@Email : snowykami@outlook.com
@File : event.py
@Software: PyCharm
"""
2024-08-20 20:30:50 +08:00
from typing import Any, Optional
2024-08-29 13:50:12 +08:00
from liteyuki import Channel
2024-08-19 23:47:39 +08:00
2024-08-20 20:30:50 +08:00
class MessageEvent:
def __init__(
self,
bot_id: str,
message: list[dict[str, Any]] | str,
message_type: str,
raw_message: str,
session_id: str,
user_id: str,
2024-08-20 20:30:50 +08:00
session_type: str,
2024-08-29 13:50:12 +08:00
receive_channel: Optional[Channel["MessageEvent"]] = None,
2024-08-20 20:30:50 +08:00
data: Optional[dict[str, Any]] = None,
):
"""
2024-08-20 20:30:50 +08:00
轻雪抽象消息事件
Args:
2024-08-20 20:30:50 +08:00
bot_id: 机器人ID
2024-08-20 20:30:50 +08:00
message: 消息消息段数组[{type: str, data: dict[str, Any]}]
raw_message: 原始消息(通常为纯文本的格式)
message_type: 消息类型(private, group, other)
session_id: 会话ID(私聊通常为用户ID群聊通常为群ID)
session_type: 会话类型(private, group)
receive_channel: 接收频道(用于回复消息)
data: 附加数据
"""
2024-08-20 20:30:50 +08:00
if data is None:
data = {}
self.message_type = message_type
2024-08-19 23:47:39 +08:00
self.data = data
self.bot_id = bot_id
2024-08-20 20:30:50 +08:00
self.message = message
self.raw_message = raw_message
self.session_id = session_id
self.session_type = session_type
self.user_id = user_id
2024-08-20 20:30:50 +08:00
self.receive_channel = receive_channel
def __str__(self):
2024-08-20 20:30:50 +08:00
return (f"Event(message_type={self.message_type}, data={self.data}, bot_id={self.bot_id}, "
f"session_id={self.session_id}, session_type={self.session_type})")
def reply(self, message: str | dict[str, Any]):
"""
回复消息
Args:
message:
Returns:
"""
2024-08-20 20:30:50 +08:00
reply_event = MessageEvent(
message_type=self.session_type,
message=message,
raw_message="",
data={
"message": message
},
bot_id=self.bot_id,
session_id=self.session_id,
2024-08-29 13:50:12 +08:00
user_id=self.user_id,
session_type=self.session_type,
2024-08-29 13:50:12 +08:00
receive_channel=None
)
2024-08-29 13:50:12 +08:00
# shared_memory.publish(self.receive_channel, reply_event)
if self.receive_channel:
self.receive_channel.send(reply_event)