nonebot2/nonebot/adapters/ding/event.py

125 lines
3.0 KiB
Python
Raw Normal View History

2020-12-30 00:36:29 +08:00
from enum import Enum
from typing import List, Optional
from typing_extensions import Literal
2020-12-30 00:36:29 +08:00
from pydantic import BaseModel
2020-12-06 02:30:19 +08:00
from nonebot.utils import escape_tag
2020-12-30 00:36:29 +08:00
from nonebot.typing import overrides
from nonebot.adapters import Event as BaseEvent
2020-12-03 00:59:32 +08:00
from .message import Message
class Event(BaseEvent):
"""
2020-12-30 00:36:29 +08:00
钉钉 协议 Event 适配各事件字段参考 `钉钉文档`_
.. _钉钉文档:
https://ding-doc.dingtalk.com/document#/org-dev-guide/elzz1p
2020-12-03 00:59:32 +08:00
"""
2020-12-30 00:36:29 +08:00
chatbotUserId: str
@overrides(BaseEvent)
def get_type(self) -> Literal["message", "notice", "request", "meta_event"]:
raise ValueError("Event has no type!")
@overrides(BaseEvent)
def get_event_name(self) -> str:
2020-12-30 00:36:29 +08:00
raise ValueError("Event has no type!")
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
@overrides(BaseEvent)
def get_event_description(self) -> str:
2020-12-30 00:36:29 +08:00
raise ValueError("Event has no type!")
@overrides(BaseEvent)
def get_message(self) -> "Message":
raise ValueError("Event has no type!")
@overrides(BaseEvent)
def get_plaintext(self) -> str:
raise ValueError("Event has no type!")
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
@overrides(BaseEvent)
def get_user_id(self) -> str:
2020-12-30 00:36:29 +08:00
raise ValueError("Event has no type!")
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
@overrides(BaseEvent)
def get_session_id(self) -> str:
2020-12-30 00:36:29 +08:00
raise ValueError("Event has no type!")
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
@overrides(BaseEvent)
def is_tome(self) -> bool:
return True
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
class TextMessage(BaseModel):
content: str
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
class AtUsersItem(BaseModel):
dingtalkId: str
staffId: Optional[str]
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
class ConversationType(str, Enum):
private = "1"
group = "2"
class MessageEvent(Event):
msgtype: str
text: TextMessage
msgId: str
createAt: int # ms
conversationType: ConversationType
conversationId: str
senderId: str
senderNick: str
senderCorpId: str
sessionWebhook: str
sessionWebhookExpiredTime: int
isAdmin: bool
@overrides(Event)
def get_type(self) -> Literal["message", "notice", "request", "meta_event"]:
return "message"
@overrides(BaseEvent)
def get_event_name(self) -> str:
return f"{self.get_type()}.{self.conversationType.name}"
@overrides(BaseEvent)
def get_event_description(self) -> str:
return f'Message[{self.msgtype}] {self.msgId} from {self.senderId} "{self.text.content}"'
@overrides(BaseEvent)
def get_plaintext(self) -> str:
return self.text.content
@overrides(BaseEvent)
def get_user_id(self) -> str:
return self.senderId
@overrides(BaseEvent)
def get_session_id(self) -> str:
return self.senderId
class PrivateMessageEvent(MessageEvent):
chatbotCorpId: str
senderStaffId: Optional[str]
conversationType: ConversationType = ConversationType.private
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
class GroupMessageEvent(MessageEvent):
atUsers: List[AtUsersItem]
conversationType: ConversationType = ConversationType.group
conversationTitle: str
isInAtList: bool
2020-12-03 00:59:32 +08:00
2020-12-30 00:36:29 +08:00
@overrides(MessageEvent)
def is_tome(self) -> bool:
return self.isInAtList