mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-31 14:47:13 +00:00
fix: In the reply_message_chunk of the adapter, the message is only streamed into the card or edited at the end of the 8th chunk return or streaming
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from re import S
|
||||||
import traceback
|
import traceback
|
||||||
import typing
|
import typing
|
||||||
from libs.dingtalk_api.dingtalkevent import DingTalkEvent
|
from libs.dingtalk_api.dingtalkevent import DingTalkEvent
|
||||||
@@ -99,13 +100,15 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
|
|||||||
message_converter: DingTalkMessageConverter = DingTalkMessageConverter()
|
message_converter: DingTalkMessageConverter = DingTalkMessageConverter()
|
||||||
event_converter: DingTalkEventConverter = DingTalkEventConverter()
|
event_converter: DingTalkEventConverter = DingTalkEventConverter()
|
||||||
config: dict
|
config: dict
|
||||||
card_instance_id_dict: dict
|
card_instance_id_dict: dict # 回复卡片消息字典,key为消息id,value为回复卡片实例id,用于在流式消息时判断是否发送到指定卡片
|
||||||
|
seq: int # 消息顺序,直接以seq作为标识
|
||||||
|
|
||||||
def __init__(self, config: dict, ap: app.Application, logger: EventLogger):
|
def __init__(self, config: dict, ap: app.Application, logger: EventLogger):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.ap = ap
|
self.ap = ap
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.card_instance_id_dict = {}
|
self.card_instance_id_dict = {}
|
||||||
|
self.seq = 1
|
||||||
required_keys = [
|
required_keys = [
|
||||||
'client_id',
|
'client_id',
|
||||||
'client_secret',
|
'client_secret',
|
||||||
@@ -155,14 +158,16 @@ class DingTalkAdapter(adapter.MessagePlatformAdapter):
|
|||||||
# incoming_message = event.incoming_message
|
# incoming_message = event.incoming_message
|
||||||
|
|
||||||
# msg_id = incoming_message.message_id
|
# msg_id = incoming_message.message_id
|
||||||
|
self.seq += 1
|
||||||
|
if (self.seq - 1) % 8 == 0 or is_final:
|
||||||
|
content, at = await DingTalkMessageConverter.yiri2target(message)
|
||||||
|
|
||||||
content, at = await DingTalkMessageConverter.yiri2target(message)
|
card_instance, card_instance_id = self.card_instance_id_dict[message_id]
|
||||||
|
# print(card_instance_id)
|
||||||
card_instance, card_instance_id = self.card_instance_id_dict[message_id]
|
await self.bot.send_card_message(card_instance, card_instance_id, content, is_final)
|
||||||
# print(card_instance_id)
|
if is_final:
|
||||||
await self.bot.send_card_message(card_instance, card_instance_id, content, is_final)
|
self.seq = 1 # 消息回复结束之后重置seq
|
||||||
if is_final:
|
self.card_instance_id_dict.pop(message_id) # 消息回复结束之后删除卡片实例id
|
||||||
self.card_instance_id_dict.pop(message_id)
|
|
||||||
|
|
||||||
async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain):
|
async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain):
|
||||||
content = await DingTalkMessageConverter.yiri2target(message)
|
content = await DingTalkMessageConverter.yiri2target(message)
|
||||||
|
|||||||
@@ -144,7 +144,9 @@ class TelegramAdapter(adapter.MessagePlatformAdapter):
|
|||||||
config: dict
|
config: dict
|
||||||
ap: app.Application
|
ap: app.Application
|
||||||
|
|
||||||
msg_stream_id: dict
|
msg_stream_id: dict # 流式消息id字典,key为流式消息id,value为首次消息源id,用于在流式消息时判断编辑那条消息
|
||||||
|
|
||||||
|
seq: int # 消息中识别消息顺序,直接以seq作为标识
|
||||||
|
|
||||||
listeners: typing.Dict[
|
listeners: typing.Dict[
|
||||||
typing.Type[platform_events.Event],
|
typing.Type[platform_events.Event],
|
||||||
@@ -156,6 +158,7 @@ class TelegramAdapter(adapter.MessagePlatformAdapter):
|
|||||||
self.ap = ap
|
self.ap = ap
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.msg_stream_id = {}
|
self.msg_stream_id = {}
|
||||||
|
self.seq = 1
|
||||||
|
|
||||||
async def telegram_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
async def telegram_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
if update.message.from_user.is_bot:
|
if update.message.from_user.is_bot:
|
||||||
@@ -213,52 +216,56 @@ class TelegramAdapter(adapter.MessagePlatformAdapter):
|
|||||||
quote_origin: bool = False,
|
quote_origin: bool = False,
|
||||||
is_final: bool = False,
|
is_final: bool = False,
|
||||||
):
|
):
|
||||||
assert isinstance(message_source.source_platform_object, Update)
|
self.seq += 1
|
||||||
components = await TelegramMessageConverter.yiri2target(message, self.bot)
|
if (self.seq - 1) % 8 == 0 or is_final:
|
||||||
args = {}
|
|
||||||
message_id = message_source.source_platform_object.message.id
|
|
||||||
if quote_origin:
|
|
||||||
args['reply_to_message_id'] = message_source.source_platform_object.message.id
|
|
||||||
|
|
||||||
component = components[0]
|
assert isinstance(message_source.source_platform_object, Update)
|
||||||
if message_id not in self.msg_stream_id:
|
components = await TelegramMessageConverter.yiri2target(message, self.bot)
|
||||||
# time.sleep(0.6)
|
args = {}
|
||||||
if component['type'] == 'text':
|
message_id = message_source.source_platform_object.message.id
|
||||||
if self.config['markdown_card'] is True:
|
if quote_origin:
|
||||||
content = telegramify_markdown.markdownify(
|
args['reply_to_message_id'] = message_source.source_platform_object.message.id
|
||||||
content=component['text'],
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
content = component['text']
|
|
||||||
args = {
|
|
||||||
'chat_id': message_source.source_platform_object.effective_chat.id,
|
|
||||||
'text': content,
|
|
||||||
}
|
|
||||||
if self.config['markdown_card'] is True:
|
|
||||||
args['parse_mode'] = 'MarkdownV2'
|
|
||||||
|
|
||||||
send_msg = await self.bot.send_message(**args)
|
component = components[0]
|
||||||
send_msg_id = send_msg.message_id
|
if message_id not in self.msg_stream_id: # 当消息回复第一次时,发送新消息
|
||||||
self.msg_stream_id[message_id] = send_msg_id
|
# time.sleep(0.6)
|
||||||
else:
|
if component['type'] == 'text':
|
||||||
if component['type'] == 'text':
|
if self.config['markdown_card'] is True:
|
||||||
if self.config['markdown_card'] is True:
|
content = telegramify_markdown.markdownify(
|
||||||
content = telegramify_markdown.markdownify(
|
content=component['text'],
|
||||||
content=component['text'],
|
)
|
||||||
)
|
else:
|
||||||
else:
|
content = component['text']
|
||||||
content = component['text']
|
args = {
|
||||||
args = {
|
'chat_id': message_source.source_platform_object.effective_chat.id,
|
||||||
'message_id': self.msg_stream_id[message_id],
|
'text': content,
|
||||||
'chat_id': message_source.source_platform_object.effective_chat.id,
|
}
|
||||||
'text': content,
|
if self.config['markdown_card'] is True:
|
||||||
}
|
args['parse_mode'] = 'MarkdownV2'
|
||||||
if self.config['markdown_card'] is True:
|
|
||||||
args['parse_mode'] = 'MarkdownV2'
|
|
||||||
|
|
||||||
await self.bot.edit_message_text(**args)
|
send_msg = await self.bot.send_message(**args)
|
||||||
if is_final:
|
send_msg_id = send_msg.message_id
|
||||||
self.msg_stream_id.pop(message_id)
|
self.msg_stream_id[message_id] = send_msg_id
|
||||||
|
else: # 存在消息的时候直接编辑消息1
|
||||||
|
if component['type'] == 'text':
|
||||||
|
if self.config['markdown_card'] is True:
|
||||||
|
content = telegramify_markdown.markdownify(
|
||||||
|
content=component['text'],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
content = component['text']
|
||||||
|
args = {
|
||||||
|
'message_id': self.msg_stream_id[message_id],
|
||||||
|
'chat_id': message_source.source_platform_object.effective_chat.id,
|
||||||
|
'text': content,
|
||||||
|
}
|
||||||
|
if self.config['markdown_card'] is True:
|
||||||
|
args['parse_mode'] = 'MarkdownV2'
|
||||||
|
|
||||||
|
await self.bot.edit_message_text(**args)
|
||||||
|
if is_final:
|
||||||
|
self.seq = 1 # 消息回复结束之后重置seq
|
||||||
|
self.msg_stream_id.pop(message_id) # 消息回复结束之后删除流式消息id
|
||||||
|
|
||||||
async def is_stream_output_supported(self) -> bool:
|
async def is_stream_output_supported(self) -> bool:
|
||||||
is_stream = False
|
is_stream = False
|
||||||
|
|||||||
Reference in New Issue
Block a user