feat: 支持NormalMessageResponded事件

This commit is contained in:
Rock Chin 2023-01-15 13:57:05 +08:00
parent b4033b2902
commit df9e89deb7
4 changed files with 51 additions and 7 deletions

View File

@ -42,6 +42,9 @@ PersonCommandSent = "person_command_sent"
params: list[str] 参数列表
text_message: str 完整指令文本
is_admin: bool 是否为管理员
returns (optional):
reply: list 回复消息组件列表
"""
GroupNormalMessageReceived = "group_normal_message_received"
@ -65,6 +68,20 @@ GroupCommandSent = "group_command_sent"
is_admin: bool 是否为管理员
"""
NormalMessageResponded = "normal_message_responded"
"""获取到对普通消息的文字响应时触发
kwargs:
launcher_type: str 发起对象类型(group/person)
launcher_id: int 发起对象ID(群号/QQ号)
sender_id: int 发送者ID(QQ号)
session: pkg.openai.session.Session 会话对象
prefix: str 回复文字消息的前缀
response_text: str 响应文本
returns (optional):
reply: list 替换回复消息组件列表
"""
SessionFirstMessageReceived = "session_first_message_received"
"""会话被第一次交互时触发
kwargs:

View File

@ -80,7 +80,8 @@ def config_operation(cmd, params):
return reply
def process_command(session_name: str, text_message: str, mgr, config, launcher_type: str, launcher_id: int) -> list:
def process_command(session_name: str, text_message: str, mgr, config,
launcher_type: str, launcher_id: int, sender_id: int) -> list:
reply = []
try:
logging.info(
@ -159,7 +160,8 @@ def process_command(session_name: str, text_message: str, mgr, config, launcher_
session = pkg.openai.session.get_session(session_name)
to_send = session.undo()
reply = pkg.qqbot.message.process_normal_message(to_send, mgr, config, launcher_type, launcher_id)
reply = pkg.qqbot.message.process_normal_message(to_send, mgr, config,
launcher_type, launcher_id, sender_id)
elif cmd == 'usage':
reply_str = "[bot]各api-key使用情况:\n\n"

View File

@ -7,17 +7,38 @@ import pkg.openai.session
import pkg.plugin.host as plugin_host
import pkg.plugin.models as plugin_models
def process_normal_message(text_message: str, mgr, config, launcher_type: str, launcher_id: int) -> list:
def process_normal_message(text_message: str, mgr, config, launcher_type: str,
launcher_id: int, sender_id: int) -> list:
session_name = f"{launcher_type}_{launcher_id}"
logging.info("[{}]发送消息:{}".format(session_name, text_message[:min(20, len(text_message))] + (
"..." if len(text_message) > 20 else "")))
session = pkg.openai.session.get_session(session_name)
reply = []
while True:
try:
prefix = "[GPT]" if hasattr(config, "show_prefix") and config.show_prefix else ""
reply = [prefix + session.append(text_message)]
text = session.append(text_message)
# 触发插件事件
args = {
"launcher_type": launcher_type,
"launcher_id": launcher_id,
"sender_id": sender_id,
"session": session,
"prefix": prefix,
"response_text": text
}
event = pkg.plugin.host.emit(plugin_models.NormalMessageResponded, **args)
if event.get_return_value("reply") is not None:
reply = event.get_return("reply")
if not event.is_prevented_default():
reply = [prefix + text]
except openai.error.APIConnectionError as e:
mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e))
reply = ["[bot]err:调用API失败请重试或联系作者或等待修复"]

View File

@ -81,9 +81,13 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
if launcher_type == 'person'
else plugin_models.GroupCommandSent, **args)
# 取出插件提交的返回值赋值给reply
if event.get_return_value("reply") is not None:
reply.append(event.get_return("reply"))
if not event.is_prevented_default():
reply = pkg.qqbot.command.process_command(session_name, text_message,
mgr, config, launcher_type, launcher_id)
mgr, config, launcher_type, launcher_id, sender_id)
else: # 消息
# 触发插件事件
@ -99,7 +103,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
if not event.is_prevented_default():
reply = pkg.qqbot.message.process_normal_message(text_message,
mgr, config, launcher_type, launcher_id)
mgr, config, launcher_type, launcher_id, sender_id)
if reply is not None and type(reply[0]) == str:
logging.info(