update: 关闭typing
This commit is contained in:
@@ -10,13 +10,16 @@ from collections import deque, defaultdict
|
|||||||
from maubot.handlers import command, event
|
from maubot.handlers import command, event
|
||||||
from maubot import Plugin, MessageEvent
|
from maubot import Plugin, MessageEvent
|
||||||
from mautrix.errors import MNotFound, MatrixRequestError
|
from mautrix.errors import MNotFound, MatrixRequestError
|
||||||
from mautrix.types import Format, TextMessageEventContent, EventType, RoomID, UserID, MessageType, RelationType, EncryptedEvent
|
from mautrix.types import Format, TextMessageEventContent, EventType, RoomID, UserID, MessageType, RelationType, \
|
||||||
|
EncryptedEvent
|
||||||
from mautrix.util import markdown
|
from mautrix.util import markdown
|
||||||
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
||||||
|
|
||||||
"""
|
"""
|
||||||
配置文件加载
|
配置文件加载
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Config(BaseProxyConfig):
|
class Config(BaseProxyConfig):
|
||||||
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
||||||
helper.copy("allowed_users")
|
helper.copy("allowed_users")
|
||||||
@@ -27,12 +30,11 @@ class Config(BaseProxyConfig):
|
|||||||
helper.copy("system_prompt")
|
helper.copy("system_prompt")
|
||||||
helper.copy("platforms")
|
helper.copy("platforms")
|
||||||
|
|
||||||
class AiBotPlugin(Plugin):
|
|
||||||
|
|
||||||
|
class AiBotPlugin(Plugin):
|
||||||
# name of the bot
|
# name of the bot
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|
||||||
async def start(self) -> None:
|
async def start(self) -> None:
|
||||||
await super().start()
|
await super().start()
|
||||||
# 加载并更新配置
|
# 加载并更新配置
|
||||||
@@ -46,6 +48,7 @@ class AiBotPlugin(Plugin):
|
|||||||
判断sender是否是allowed_users中的成员
|
判断sender是否是allowed_users中的成员
|
||||||
如果是, 则可以发送消息给AI
|
如果是, 则可以发送消息给AI
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def is_allow(self, sender: str) -> bool:
|
def is_allow(self, sender: str) -> bool:
|
||||||
# 如果列表中没有元素, 直接返回True
|
# 如果列表中没有元素, 直接返回True
|
||||||
if len(self.config['allowed_users']) <= 0:
|
if len(self.config['allowed_users']) <= 0:
|
||||||
@@ -70,6 +73,7 @@ class AiBotPlugin(Plugin):
|
|||||||
4. 当聊天室中只有两个人, 并且其中一个是机器人时
|
4. 当聊天室中只有两个人, 并且其中一个是机器人时
|
||||||
5. 在thread中
|
5. 在thread中
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def should_respond(self, event: MessageEvent) -> bool:
|
async def should_respond(self, event: MessageEvent) -> bool:
|
||||||
# 发送者是机器人本身, 返回False
|
# 发送者是机器人本身, 返回False
|
||||||
if event.sender == self.client.mxid:
|
if event.sender == self.client.mxid:
|
||||||
@@ -95,7 +99,8 @@ class AiBotPlugin(Plugin):
|
|||||||
|
|
||||||
# 在thread中时
|
# 在thread中时
|
||||||
if self.config['reply_in_thread'] and event.content.relates_to.rel_type == RelationType.THREAD:
|
if self.config['reply_in_thread'] and event.content.relates_to.rel_type == RelationType.THREAD:
|
||||||
parent_event = await self.client.get_event(room_id=event.room_id, event_id=event.content.get_thread_parent())
|
parent_event = await self.client.get_event(room_id=event.room_id,
|
||||||
|
event_id=event.content.get_thread_parent())
|
||||||
return await self.should_respond(parent_event)
|
return await self.should_respond(parent_event)
|
||||||
|
|
||||||
# 如果是回复消息
|
# 如果是回复消息
|
||||||
@@ -117,12 +122,17 @@ class AiBotPlugin(Plugin):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
await event.mark_read()
|
await event.mark_read()
|
||||||
resp_content = "response test"
|
|
||||||
|
|
||||||
await self.client.set_typing(event.room_id, timeout=99999)
|
await self.client.set_typing(event.room_id, timeout=99999)
|
||||||
|
resp_content = "response test"
|
||||||
|
# ai gpt调用
|
||||||
|
# 关闭typing提示
|
||||||
|
await self.client.set_typing(event.room_id, timeout=0)
|
||||||
|
|
||||||
|
# 打开typing提示
|
||||||
response = TextMessageEventContent(msgtype=MessageType.NOTICE, body=resp_content, format=Format.HTML,
|
response = TextMessageEventContent(msgtype=MessageType.NOTICE, body=resp_content, format=Format.HTML,
|
||||||
formatted_body=markdown.render(resp_content))
|
formatted_body=markdown.render(resp_content))
|
||||||
await event.respond(response)
|
await event.respond(response)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -131,5 +141,3 @@ class AiBotPlugin(Plugin):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def get_config_class(cls) -> Type[BaseProxyConfig]:
|
def get_config_class(cls) -> Type[BaseProxyConfig]:
|
||||||
return Config
|
return Config
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user