diff --git a/amiyabot/adapters/tencent/builder.py b/amiyabot/adapters/tencent/builder.py index 66fe34a..609dfe4 100644 --- a/amiyabot/adapters/tencent/builder.py +++ b/amiyabot/adapters/tencent/builder.py @@ -38,10 +38,11 @@ def __init__(self, user_id: str, message_id: str, reference: bool, direct: bool) self.reference: bool = reference self.direct: bool = direct - def __insert_req(self, content: str = '', image: Optional[Union[str, bytes]] = None): + def __insert_req(self, content: str = '', image: Optional[Union[str, bytes]] = None, data: Optional[dict] = None): req = MessageSendRequest( data={ 'msg_id': self.message_id, + **(data or {}), }, direct=self.direct, user_id=self.user_id, @@ -67,13 +68,14 @@ def __insert_req(self, content: str = '', image: Optional[Union[str, bytes]] = N def add_text(self, text: str): if self.req_list: - req = self.req_list[-1] + req_data = self.req_list[-1].data - if 'content' not in req.data: - req.data['content'] = '' + if 'embed' not in req_data and 'ark' not in req_data: + if 'content' not in req_data: + req_data['content'] = '' - req.data['content'] += text - return None + req_data['content'] += text + return None self.text += text @@ -81,9 +83,14 @@ def add_image(self, image: Union[str, bytes]): self.__insert_req(content=self.text, image=image) self.text = '' + def add_data(self, data: Optional[dict]): + self.done() + self.__insert_req(data=data) + def done(self): if self.text: self.__insert_req(content=self.text) + self.text = '' async def build_message_send(chain: Chain, custom_chain: Optional[CHAIN_LIST] = None): @@ -133,6 +140,14 @@ async def build_message_send(chain: Chain, custom_chain: Optional[CHAIN_LIST] = else: log.warning('html convert fail.') + # Embed + if isinstance(item, Embed): + messages.add_data(item.get()) + + # Ark + if isinstance(item, Ark): + messages.add_data(item.get()) + messages.done() return messages diff --git a/amiyabot/builtin/messageChain/__init__.py b/amiyabot/builtin/messageChain/__init__.py index 97e4815..9074ddd 100644 --- a/amiyabot/builtin/messageChain/__init__.py +++ b/amiyabot/builtin/messageChain/__init__.py @@ -188,6 +188,14 @@ def html( ) return self + def embed(self, title: str, prompt: str, thumbnail: str, fields: List[str]): + self.chain.append(Embed(title, prompt, thumbnail, fields)) + return self + + def ark(self, template_id: int, kv: List[dict]): + self.chain.append(Ark(template_id, kv)) + return self + def extend(self, data: Any): self.chain.append(Extend(data)) return self diff --git a/amiyabot/builtin/messageChain/element.py b/amiyabot/builtin/messageChain/element.py index 450886c..8a17c39 100644 --- a/amiyabot/builtin/messageChain/element.py +++ b/amiyabot/builtin/messageChain/element.py @@ -124,6 +124,38 @@ async def create_html_image(self): return result +@dataclass +class Embed: + title: str + prompt: str + thumbnail: str + fields: List[str] + + def get(self): + return { + 'embed': { + 'title': self.title, + 'prompt': self.prompt, + 'thumbnail': {'url': self.thumbnail}, + 'fields': [{'name': item} for item in self.fields], + } + } + + +@dataclass +class Ark: + template_id: int + kv: List[dict] + + def get(self): + return { + 'ark': { + 'template_id': self.template_id, + 'kv': self.kv, + } + } + + @dataclass class Extend: data: Any @@ -134,5 +166,5 @@ def get(self): return self.data -CHAIN_ITEM = Union[At, AtAll, Tag, Face, Text, Image, Voice, Html, Extend] +CHAIN_ITEM = Union[At, AtAll, Tag, Face, Text, Image, Voice, Html, Embed, Ark, Extend] CHAIN_LIST = List[CHAIN_ITEM]