Skip to content

Commit

Permalink
update: Chain 支持构建 embed 和 ark 消息
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien8261 committed Oct 31, 2023
1 parent 23b98dc commit fd1f6c0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
27 changes: 21 additions & 6 deletions amiyabot/adapters/tencent/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -67,23 +68,29 @@ 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

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):
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions amiyabot/builtin/messageChain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 33 additions & 1 deletion amiyabot/builtin/messageChain/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]

0 comments on commit fd1f6c0

Please sign in to comment.