mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-25 03:04:29 -04:00
refactor(telegram): dedupe reply delivery option assembly
This commit is contained in:
@@ -249,6 +249,25 @@ export const dispatchTelegramMessage = async ({
|
|||||||
skippedNonSilent: 0,
|
skippedNonSilent: 0,
|
||||||
};
|
};
|
||||||
let finalizedViaPreviewMessage = false;
|
let finalizedViaPreviewMessage = false;
|
||||||
|
const clearGroupHistory = () => {
|
||||||
|
if (isGroup && historyKey) {
|
||||||
|
clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const deliveryBaseOptions = {
|
||||||
|
chatId: String(chatId),
|
||||||
|
token: opts.token,
|
||||||
|
runtime,
|
||||||
|
bot,
|
||||||
|
mediaLocalRoots,
|
||||||
|
replyToMode,
|
||||||
|
textLimit,
|
||||||
|
thread: threadSpec,
|
||||||
|
tableMode,
|
||||||
|
chunkMode,
|
||||||
|
linkPreview: telegramCfg.linkPreview,
|
||||||
|
replyQuoteText,
|
||||||
|
};
|
||||||
|
|
||||||
let queuedFinal = false;
|
let queuedFinal = false;
|
||||||
try {
|
try {
|
||||||
@@ -300,20 +319,9 @@ export const dispatchTelegramMessage = async ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const result = await deliverReplies({
|
const result = await deliverReplies({
|
||||||
|
...deliveryBaseOptions,
|
||||||
replies: [payload],
|
replies: [payload],
|
||||||
chatId: String(chatId),
|
|
||||||
token: opts.token,
|
|
||||||
runtime,
|
|
||||||
bot,
|
|
||||||
mediaLocalRoots,
|
|
||||||
replyToMode,
|
|
||||||
textLimit,
|
|
||||||
thread: threadSpec,
|
|
||||||
tableMode,
|
|
||||||
chunkMode,
|
|
||||||
onVoiceRecording: sendRecordVoice,
|
onVoiceRecording: sendRecordVoice,
|
||||||
linkPreview: telegramCfg.linkPreview,
|
|
||||||
replyQuoteText,
|
|
||||||
});
|
});
|
||||||
if (result.delivered) {
|
if (result.delivered) {
|
||||||
deliveryState.delivered = true;
|
deliveryState.delivered = true;
|
||||||
@@ -356,27 +364,14 @@ export const dispatchTelegramMessage = async ({
|
|||||||
if (!deliveryState.delivered && deliveryState.skippedNonSilent > 0) {
|
if (!deliveryState.delivered && deliveryState.skippedNonSilent > 0) {
|
||||||
const result = await deliverReplies({
|
const result = await deliverReplies({
|
||||||
replies: [{ text: EMPTY_RESPONSE_FALLBACK }],
|
replies: [{ text: EMPTY_RESPONSE_FALLBACK }],
|
||||||
chatId: String(chatId),
|
...deliveryBaseOptions,
|
||||||
token: opts.token,
|
|
||||||
runtime,
|
|
||||||
bot,
|
|
||||||
mediaLocalRoots,
|
|
||||||
replyToMode,
|
|
||||||
textLimit,
|
|
||||||
thread: threadSpec,
|
|
||||||
tableMode,
|
|
||||||
chunkMode,
|
|
||||||
linkPreview: telegramCfg.linkPreview,
|
|
||||||
replyQuoteText,
|
|
||||||
});
|
});
|
||||||
sentFallback = result.delivered;
|
sentFallback = result.delivered;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasFinalResponse = queuedFinal || sentFallback;
|
const hasFinalResponse = queuedFinal || sentFallback;
|
||||||
if (!hasFinalResponse) {
|
if (!hasFinalResponse) {
|
||||||
if (isGroup && historyKey) {
|
clearGroupHistory();
|
||||||
clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit });
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
removeAckReactionAfterReply({
|
removeAckReactionAfterReply({
|
||||||
@@ -396,7 +391,5 @@ export const dispatchTelegramMessage = async ({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (isGroup && historyKey) {
|
clearGroupHistory();
|
||||||
clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit });
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -179,13 +179,16 @@ async function resolveTelegramCommandAuth(params: {
|
|||||||
const senderId = msg.from?.id ? String(msg.from.id) : "";
|
const senderId = msg.from?.id ? String(msg.from.id) : "";
|
||||||
const senderUsername = msg.from?.username ?? "";
|
const senderUsername = msg.from?.username ?? "";
|
||||||
|
|
||||||
const rejectNotAuthorized = async () => {
|
const sendAuthMessage = async (text: string) => {
|
||||||
await withTelegramApiErrorLogging({
|
await withTelegramApiErrorLogging({
|
||||||
operation: "sendMessage",
|
operation: "sendMessage",
|
||||||
fn: () => bot.api.sendMessage(chatId, "You are not authorized to use this command."),
|
fn: () => bot.api.sendMessage(chatId, text),
|
||||||
});
|
});
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
const rejectNotAuthorized = async () => {
|
||||||
|
return await sendAuthMessage("You are not authorized to use this command.");
|
||||||
|
};
|
||||||
|
|
||||||
const baseAccess = evaluateTelegramGroupBaseAccess({
|
const baseAccess = evaluateTelegramGroupBaseAccess({
|
||||||
isGroup,
|
isGroup,
|
||||||
@@ -200,18 +203,10 @@ async function resolveTelegramCommandAuth(params: {
|
|||||||
});
|
});
|
||||||
if (!baseAccess.allowed) {
|
if (!baseAccess.allowed) {
|
||||||
if (baseAccess.reason === "group-disabled") {
|
if (baseAccess.reason === "group-disabled") {
|
||||||
await withTelegramApiErrorLogging({
|
return await sendAuthMessage("This group is disabled.");
|
||||||
operation: "sendMessage",
|
|
||||||
fn: () => bot.api.sendMessage(chatId, "This group is disabled."),
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
if (baseAccess.reason === "topic-disabled") {
|
if (baseAccess.reason === "topic-disabled") {
|
||||||
await withTelegramApiErrorLogging({
|
return await sendAuthMessage("This topic is disabled.");
|
||||||
operation: "sendMessage",
|
|
||||||
fn: () => bot.api.sendMessage(chatId, "This topic is disabled."),
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
return await rejectNotAuthorized();
|
return await rejectNotAuthorized();
|
||||||
}
|
}
|
||||||
@@ -236,11 +231,7 @@ async function resolveTelegramCommandAuth(params: {
|
|||||||
});
|
});
|
||||||
if (!policyAccess.allowed) {
|
if (!policyAccess.allowed) {
|
||||||
if (policyAccess.reason === "group-policy-disabled") {
|
if (policyAccess.reason === "group-policy-disabled") {
|
||||||
await withTelegramApiErrorLogging({
|
return await sendAuthMessage("Telegram group commands are disabled.");
|
||||||
operation: "sendMessage",
|
|
||||||
fn: () => bot.api.sendMessage(chatId, "Telegram group commands are disabled."),
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
policyAccess.reason === "group-policy-allowlist-no-sender" ||
|
policyAccess.reason === "group-policy-allowlist-no-sender" ||
|
||||||
@@ -249,11 +240,7 @@ async function resolveTelegramCommandAuth(params: {
|
|||||||
return await rejectNotAuthorized();
|
return await rejectNotAuthorized();
|
||||||
}
|
}
|
||||||
if (policyAccess.reason === "group-chat-not-allowed") {
|
if (policyAccess.reason === "group-chat-not-allowed") {
|
||||||
await withTelegramApiErrorLogging({
|
return await sendAuthMessage("This group is not allowed.");
|
||||||
operation: "sendMessage",
|
|
||||||
fn: () => bot.api.sendMessage(chatId, "This group is not allowed."),
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,11 +259,7 @@ async function resolveTelegramCommandAuth(params: {
|
|||||||
modeWhenAccessGroupsOff: "configured",
|
modeWhenAccessGroupsOff: "configured",
|
||||||
});
|
});
|
||||||
if (requireAuth && !commandAuthorized) {
|
if (requireAuth && !commandAuthorized) {
|
||||||
await withTelegramApiErrorLogging({
|
return await rejectNotAuthorized();
|
||||||
operation: "sendMessage",
|
|
||||||
fn: () => bot.api.sendMessage(chatId, "You are not authorized to use this command."),
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -411,6 +394,25 @@ export const registerTelegramNativeCommands = ({
|
|||||||
const chunkMode = resolveChunkMode(cfg, "telegram", route.accountId);
|
const chunkMode = resolveChunkMode(cfg, "telegram", route.accountId);
|
||||||
return { chatId, threadSpec, route, mediaLocalRoots, tableMode, chunkMode };
|
return { chatId, threadSpec, route, mediaLocalRoots, tableMode, chunkMode };
|
||||||
};
|
};
|
||||||
|
const buildCommandDeliveryBaseOptions = (params: {
|
||||||
|
chatId: string | number;
|
||||||
|
mediaLocalRoots?: readonly string[];
|
||||||
|
threadSpec: ReturnType<typeof resolveTelegramThreadSpec>;
|
||||||
|
tableMode: ReturnType<typeof resolveMarkdownTableMode>;
|
||||||
|
chunkMode: ReturnType<typeof resolveChunkMode>;
|
||||||
|
}) => ({
|
||||||
|
chatId: String(params.chatId),
|
||||||
|
token: opts.token,
|
||||||
|
runtime,
|
||||||
|
bot,
|
||||||
|
mediaLocalRoots: params.mediaLocalRoots,
|
||||||
|
replyToMode,
|
||||||
|
textLimit,
|
||||||
|
thread: params.threadSpec,
|
||||||
|
tableMode: params.tableMode,
|
||||||
|
chunkMode: params.chunkMode,
|
||||||
|
linkPreview: telegramCfg.linkPreview,
|
||||||
|
});
|
||||||
|
|
||||||
if (commandsToRegister.length > 0 || pluginCatalog.commands.length > 0) {
|
if (commandsToRegister.length > 0 || pluginCatalog.commands.length > 0) {
|
||||||
if (typeof (bot as unknown as { command?: unknown }).command !== "function") {
|
if (typeof (bot as unknown as { command?: unknown }).command !== "function") {
|
||||||
@@ -459,6 +461,13 @@ export const registerTelegramNativeCommands = ({
|
|||||||
isForum,
|
isForum,
|
||||||
resolvedThreadId,
|
resolvedThreadId,
|
||||||
});
|
});
|
||||||
|
const deliveryBaseOptions = buildCommandDeliveryBaseOptions({
|
||||||
|
chatId,
|
||||||
|
mediaLocalRoots,
|
||||||
|
threadSpec,
|
||||||
|
tableMode,
|
||||||
|
chunkMode,
|
||||||
|
});
|
||||||
const threadParams = buildTelegramThreadParams(threadSpec) ?? {};
|
const threadParams = buildTelegramThreadParams(threadSpec) ?? {};
|
||||||
|
|
||||||
const commandDefinition = findCommandByNativeName(command.name, "telegram");
|
const commandDefinition = findCommandByNativeName(command.name, "telegram");
|
||||||
@@ -590,17 +599,7 @@ export const registerTelegramNativeCommands = ({
|
|||||||
deliver: async (payload, _info) => {
|
deliver: async (payload, _info) => {
|
||||||
const result = await deliverReplies({
|
const result = await deliverReplies({
|
||||||
replies: [payload],
|
replies: [payload],
|
||||||
chatId: String(chatId),
|
...deliveryBaseOptions,
|
||||||
token: opts.token,
|
|
||||||
runtime,
|
|
||||||
bot,
|
|
||||||
mediaLocalRoots,
|
|
||||||
replyToMode,
|
|
||||||
textLimit,
|
|
||||||
thread: threadSpec,
|
|
||||||
tableMode,
|
|
||||||
chunkMode,
|
|
||||||
linkPreview: telegramCfg.linkPreview,
|
|
||||||
});
|
});
|
||||||
if (result.delivered) {
|
if (result.delivered) {
|
||||||
deliveryState.delivered = true;
|
deliveryState.delivered = true;
|
||||||
@@ -624,17 +623,7 @@ export const registerTelegramNativeCommands = ({
|
|||||||
if (!deliveryState.delivered && deliveryState.skippedNonSilent > 0) {
|
if (!deliveryState.delivered && deliveryState.skippedNonSilent > 0) {
|
||||||
await deliverReplies({
|
await deliverReplies({
|
||||||
replies: [{ text: EMPTY_RESPONSE_FALLBACK }],
|
replies: [{ text: EMPTY_RESPONSE_FALLBACK }],
|
||||||
chatId: String(chatId),
|
...deliveryBaseOptions,
|
||||||
token: opts.token,
|
|
||||||
runtime,
|
|
||||||
bot,
|
|
||||||
mediaLocalRoots,
|
|
||||||
replyToMode,
|
|
||||||
textLimit,
|
|
||||||
thread: threadSpec,
|
|
||||||
tableMode,
|
|
||||||
chunkMode,
|
|
||||||
linkPreview: telegramCfg.linkPreview,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -685,6 +674,13 @@ export const registerTelegramNativeCommands = ({
|
|||||||
isForum,
|
isForum,
|
||||||
resolvedThreadId,
|
resolvedThreadId,
|
||||||
});
|
});
|
||||||
|
const deliveryBaseOptions = buildCommandDeliveryBaseOptions({
|
||||||
|
chatId,
|
||||||
|
mediaLocalRoots,
|
||||||
|
threadSpec,
|
||||||
|
tableMode,
|
||||||
|
chunkMode,
|
||||||
|
});
|
||||||
const from = isGroup
|
const from = isGroup
|
||||||
? buildTelegramGroupFrom(chatId, threadSpec.id)
|
? buildTelegramGroupFrom(chatId, threadSpec.id)
|
||||||
: `telegram:${chatId}`;
|
: `telegram:${chatId}`;
|
||||||
@@ -706,17 +702,7 @@ export const registerTelegramNativeCommands = ({
|
|||||||
|
|
||||||
await deliverReplies({
|
await deliverReplies({
|
||||||
replies: [result],
|
replies: [result],
|
||||||
chatId: String(chatId),
|
...deliveryBaseOptions,
|
||||||
token: opts.token,
|
|
||||||
runtime,
|
|
||||||
bot,
|
|
||||||
mediaLocalRoots,
|
|
||||||
replyToMode,
|
|
||||||
textLimit,
|
|
||||||
thread: threadSpec,
|
|
||||||
tableMode,
|
|
||||||
chunkMode,
|
|
||||||
linkPreview: telegramCfg.linkPreview,
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user