Skip to content

Commit

Permalink
update: 支持 md 模版和按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien8261 committed Jan 4, 2024
1 parent bbe43e5 commit 9465ce4
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 16 deletions.
2 changes: 1 addition & 1 deletion amiyabot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from amiyabot.builtin.lib.browserService import BrowserLaunchConfig, basic_browser_service

# message
from amiyabot.builtin.messageChain import Chain, ChainBuilder
from amiyabot.builtin.messageChain import Chain, ChainBuilder, InlineKeyboard
from amiyabot.builtin.message import (
Event,
EventList,
Expand Down
8 changes: 8 additions & 0 deletions amiyabot/adapters/tencent/qqGuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ async def build_message_send(chain: Chain, custom_chain: Optional[CHAIN_LIST] =
if isinstance(item, Ark):
messages.add_data(item.get())

# Markdown
if isinstance(item, Markdown):
messages.add_data(item.get())

# Keyboard
if isinstance(item, Keyboard):
messages.add_data(item.get())

messages.done()

return messages
36 changes: 22 additions & 14 deletions amiyabot/builtin/messageChain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,6 @@ def video(self, file: str):
self.chain.append(Video(file, builder=self.builder))
return self

def markdown(
self,
content: str,
render_time: int = DEFAULT_RENDER_TIME,
is_dark: bool = False,
):
return self.html(
ChainConfig.md_template_dark if is_dark else ChainConfig.md_template,
width=50,
height=50,
data={'content': content},
render_time=render_time,
)

def html(
self,
path: str,
Expand All @@ -193,6 +179,28 @@ def html(
)
return self

def markdown(
self,
content: str,
render_time: int = DEFAULT_RENDER_TIME,
is_dark: bool = False,
):
return self.html(
ChainConfig.md_template_dark if is_dark else ChainConfig.md_template,
width=50,
height=50,
data={'content': content},
render_time=render_time,
)

def markdown_template(self, template_id: str, params: List[dict]):
self.chain.append(Markdown(template_id, params))
return self

def keyboard(self, keyboard: Optional[InlineKeyboard] = None, template_id: Optional[str] = ''):
self.chain.append(Keyboard(template_id, keyboard))
return self

def embed(self, title: str, prompt: str, thumbnail: str, fields: List[str]):
self.chain.append(Embed(title, prompt, thumbnail, fields))
return self
Expand Down
53 changes: 52 additions & 1 deletion amiyabot/builtin/messageChain/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from amiyabot.adapters.common import CQCode
from amiyabot import log

from .keyboard import InlineKeyboard


class ChainBuilder:
@classmethod
Expand Down Expand Up @@ -184,6 +186,40 @@ def get(self):
}


@dataclass
class Markdown:
template_id: str
params: List[dict]

def get(self):
return {
'markdown': {
'custom_template_id': self.template_id,
'params': self.params,
}
}


@dataclass
class Keyboard:
template_id: Optional[str] = ''
keyboard: Optional[InlineKeyboard] = None

def get(self):
if self.keyboard:
return {
'keyboard': {
'content': self.keyboard.dict(),
}
}
else:
return {
'keyboard': {
'id': self.template_id,
}
}


@dataclass
class Extend:
data: Any
Expand All @@ -194,5 +230,20 @@ def get(self):
return self.data


CHAIN_ITEM = Union[At, AtAll, Tag, Face, Text, Image, Voice, Video, Html, Embed, Ark, Extend]
CHAIN_ITEM = Union[
At,
AtAll,
Tag,
Face,
Text,
Image,
Voice,
Video,
Html,
Embed,
Ark,
Markdown,
Keyboard,
Extend,
]
CHAIN_LIST = List[CHAIN_ITEM]
82 changes: 82 additions & 0 deletions amiyabot/builtin/messageChain/keyboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from typing import List, Union, Optional
from dataclasses import dataclass, field, asdict


@dataclass
class Permission:
type: int = 1
specify_role_ids: List[str] = field(default_factory=lambda: ['1', '2', '3'])
specify_user_ids: List[str] = field(default_factory=lambda: [])


@dataclass
class RenderData:
label: str = ''
visited_label: str = ''


@dataclass
class Action:
type: int = 1
data: str = ''
unsupport_tips: str = ''

reply: bool = False
enter: bool = False
anchor: bool = False
at_bot_show_channel_list: bool = False

permission: Permission = field(default_factory=Permission)


@dataclass
class Button:
id: str
render_data: RenderData = field(default_factory=RenderData)
action: Action = field(default_factory=Action)


@dataclass
class Row:
buttons: List[Button] = field(default_factory=list)

def add_button(
self,
button: Union[str, Button],
label: str,
visited_label: Optional[str] = None,
action_type: int = 1,
action_data: str = '',
):
if len(self.buttons) >= 5:
raise OverflowError('Create up to 5 buttons per row')

if isinstance(button, str):
button = Button(button)

button.render_data.label = label
button.render_data.visited_label = visited_label or label
button.action.type = action_type
button.action.data = action_data or button.id

self.buttons.append(button)

return button


@dataclass
class InlineKeyboard:
bot_appid: int
rows: List[Row] = field(default_factory=list)

def add_row(self):
if len(self.rows) >= 5:
raise OverflowError('Create up to 5 rows')

row = Row()
self.rows.append(row)

return row

def dict(self):
return asdict(self)

0 comments on commit 9465ce4

Please sign in to comment.