Skip to content

Commit

Permalink
feat: support reply decoration
Browse files Browse the repository at this point in the history
  • Loading branch information
iuiaoin committed Aug 2, 2023
1 parent 5999edc commit becef81
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Then fill in the configuration in `config.json`, the following is the descriptio
"clear_all_sessions_command": "#clear all sessions", # Clear all sessions
"chat_group_session_independent": false, # Whether sessions of users are independent in chat group
"single_chat_prefix": ["bot", "@bot"], # Start conversation with "bot" or "@bot" in single chat to trigger the bot, leave it empty if you wanna make the bot active all the time
"group_chat_reply_prefix": "", # Reply prefix in group chat
"group_chat_reply_suffix": "", # Reply suffix in group chat
"single_chat_reply_prefix": "", # Reply prefix in single chat
"single_chat_reply_suffix": "", # Reply suffix in single chat
"query_key_command": "#query key" # Querying the usage of the api key
"recent_days": 5 # The usage in <recent_days> days
"plugins": [{ "name": <plugin name>, other configs }]# Add the your favorite plugins
Expand Down
4 changes: 4 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ pip install -r requirements.txt
"clear_all_sessions_command": "#clear all sessions", # 清楚所有对话记忆
"chat_group_session_independent": false, # 群聊中的用户会话上下文是否是各自独立的
"single_chat_prefix": ["bot", "@bot"], # 在私聊中以“bot”或“@bot”开始对话以触发机器人,如果你想让bot一直处于激活状态,请将其留空
"group_chat_reply_prefix": "", # 群聊中的回复前缀, 可用来区分机器人/真人
"group_chat_reply_suffix": "", # 群聊中的回复后缀, \n 可换行
"single_chat_reply_prefix": "", # 私聊中的回复前缀, 可用来区分机器人/真人
"single_chat_reply_suffix": "", # 私聊中的回复后缀, \n 可换行
"query_key_command": "#query key", # 查询 api key 使用情况
"recent_days": 5 # 查询最近的<recent_days>天
"plugins": [{ "name": <plugin name>, other configs }]# 添加你喜爱的插件
Expand Down
40 changes: 37 additions & 3 deletions channel/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ def handle_single(self, msg: Message):
context.type = ContextType.CREATE_IMAGE
self.handle_reply(msg, context)

def decorate_reply(self, reply: Reply, msg: Message) -> Reply:
if reply.type == ReplyType.TEXT:
group_chat_reply_prefix = conf().get("group_chat_reply_prefix", "")
group_chat_reply_suffix = conf().get("group_chat_reply_suffix", "")
single_chat_reply_prefix = conf().get("single_chat_reply_prefix", "")
single_chat_reply_suffix = conf().get("single_chat_reply_suffix", "")
reply_text = reply.content
if msg.is_group:
reply_text = (
group_chat_reply_prefix + reply_text + group_chat_reply_suffix
)
else:
reply_text = (
single_chat_reply_prefix + reply_text + single_chat_reply_suffix
)
reply.content = reply_text
return reply

def handle_reply(self, msg: Message, context: Context):
e1 = PluginManager().emit(
Event(
Expand All @@ -144,20 +162,36 @@ def handle_reply(self, msg: Message, context: Context):
if e1.is_bypass:
return self.send(e1.reply, e1.message)

reply = Bot().reply(e1.context)
rawReply = Bot().reply(e1.context)

e2 = PluginManager().emit(
Event(
EventType.WILL_SEND_REPLY,
EventType.WILL_DECORATE_REPLY,
{
"channel": self,
"message": e1.message,
"context": e1.context,
"reply": rawReply,
},
)
)
if e2.is_bypass:
return self.send(e2.reply, e2.message)

reply = self.decorate_reply(rawReply, msg)

e3 = PluginManager().emit(
Event(
EventType.WILL_SEND_REPLY,
{
"channel": self,
"message": e2.message,
"context": e2.context,
"reply": reply,
},
)
)
self.send(e2.reply, e2.message)
self.send(e3.reply, e3.message)

def send(self, reply: Reply, msg: Message):
if reply is None:
Expand Down
6 changes: 5 additions & 1 deletion plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ There're four abstract methods in `Plugin` class need to be implemented, and the

- `did_receive_message`: will be called as soon as received the message, its Event contains `channel` and `message`
- `will_generate_reply`: will be called before the reply is generated, its Event contains `channel`, `message` and `context`
- `will_send_reply`: will be called before sending the reply, its Event contains `channel`, `message`, `context` and `reply`
- `will_decorate_reply`: will be called before decorate the reply, its Event contains `channel`, `message`, `context` and `reply`
- `will_send_reply`: will be called before sending the reply, its Event contains `channel`, `message`, `context` and decorated `reply`
- `help`: will be used to show help docs to users by `#help <plugin name>` command

You can modify the `context` and `reply` to change the default behavior, and call `event action method` to decide whether to continue the plugin chain or whether to execute the default logic.
Expand All @@ -73,6 +74,9 @@ def will_generate_reply(self, event: Event):
event.reply = self.reply() # modify the reply
event.bypass() # bypass the plugin chain and default logic

def will_decorate_reply(self, event: Event):
pass

def will_send_reply(self, event: Event):
pass

Expand Down
5 changes: 3 additions & 2 deletions plugins/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

class EventType(Enum):
DID_RECEIVE_MESSAGE = 1 # receive message
WILL_GENERATE_REPLY = 2 # decorate reply
WILL_SEND_REPLY = 3 # send message
WILL_GENERATE_REPLY = 2 # generate reply
WILL_DECORATE_REPLY = 3 # decorate reply
WILL_SEND_REPLY = 4 # send reply

def __str__(self):
return self.name
Expand Down
1 change: 1 addition & 0 deletions plugins/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def activate_plugins(self, plugins: list) -> None:
if instance is not None:
self.on(EventType.DID_RECEIVE_MESSAGE, instance.did_receive_message)
self.on(EventType.WILL_GENERATE_REPLY, instance.will_generate_reply)
self.on(EventType.WILL_DECORATE_REPLY, instance.will_decorate_reply)
self.on(EventType.WILL_SEND_REPLY, instance.will_send_reply)

def emit(self, event: Event) -> Event:
Expand Down
4 changes: 4 additions & 0 deletions plugins/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def did_receive_message(self, event: Event):
def will_generate_reply(self, event: Event):
pass

@abstractmethod
def will_decorate_reply(self, event: Event):
pass

@abstractmethod
def will_send_reply(self, event: Event):
pass
Expand Down

0 comments on commit becef81

Please sign in to comment.