Skip to content

Commit d2825c7

Browse files
authored
refactor(telegram): consolidate bot handlers into named modules (#122174)
The bot-handlers family was 22 files behind a 22-line fan-out: six files holding one callback switch, four holding one inbound pipeline, every interface a derived ReturnType, processMessage taking 8 positional args, and its params type declared inside bot-native-commands.ts. Three named coordinators (inbound pipeline, callback router, event bindings) now sit behind the same registerTelegramHandlers entry with hand-written leaf contracts; the factory graph, 16 slice/barrel files, and the type shim are deleted. Behavior-neutral: bot.test.ts and the ingress e2e byte-identical to main (SHA-256-pinned) and green; +185 production LOC accepted as the written-contract tradeoff. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 5bc7efe commit d2825c7

40 files changed

Lines changed: 2524 additions & 2258 deletions

‎extensions/telegram/src/bot-core.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,26 @@ export function createTelegramBotCore(
431431
resolveGroupRequireMention,
432432
resolveTelegramGroupConfig,
433433
shouldSkipUpdate,
434-
processMessage,
434+
processMessage: async ({
435+
ctx,
436+
allMedia,
437+
storeAllowFrom,
438+
turnContext,
439+
options,
440+
replyMedia,
441+
replyChain,
442+
promptContext,
443+
}) =>
444+
await processMessage(
445+
ctx,
446+
allMedia,
447+
storeAllowFrom,
448+
turnContext,
449+
options,
450+
replyMedia,
451+
replyChain,
452+
promptContext,
453+
),
435454
logger,
436455
telegramDeps,
437456
nativeCommandCallbackDispatcher,

‎extensions/telegram/src/bot-handlers.authorization-groups.runtime.ts‎

Lines changed: 0 additions & 118 deletions
This file was deleted.

extensions/telegram/src/bot-handlers.callback-actions.runtime.ts renamed to extensions/telegram/src/bot-handlers.callback-actions.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Message } from "grammy/types";
2-
import type { RegisterTelegramHandlerParams } from "./bot-native-commands.js";
2+
import { questionGatewayRuntime } from "openclaw/plugin-sdk/question-gateway-runtime";
3+
import type { RegisterTelegramHandlerParams } from "./bot-handlers.types.js";
34
import { buildTelegramThreadParams, resolveTelegramMessageThreadSpec } from "./bot/helpers.js";
5+
import type { TelegramQuestionCallback } from "./question-callback-data.js";
46
import { buildInlineKeyboard } from "./send.js";
57

68
export type TelegramCallbackButton = {
@@ -25,6 +27,11 @@ export interface TelegramCallbackMessageActions {
2527
editCallbackButtons: (
2628
buttons: TelegramCallbackButton[][],
2729
) => ReturnType<RegisterTelegramHandlerParams["bot"]["api"]["editMessageReplyMarkup"]>;
30+
editCallbackMessageWithButtons: (
31+
text: string,
32+
buttons: TelegramCallbackButton[][],
33+
extra?: { parse_mode?: "HTML" | "Markdown" | "MarkdownV2" },
34+
) => Promise<void>;
2835
deleteCallbackMessage: () => ReturnType<
2936
RegisterTelegramHandlerParams["bot"]["api"]["deleteMessage"]
3037
>;
@@ -92,11 +99,66 @@ export function createTelegramCallbackMessageActions(params: {
9299
return await bot.api.sendMessage(callbackMessage.chat.id, text, mergedParams);
93100
};
94101

102+
const editCallbackMessageWithButtons = async (
103+
text: string,
104+
buttons: TelegramCallbackButton[][],
105+
extra?: { parse_mode?: "HTML" | "Markdown" | "MarkdownV2" },
106+
) => {
107+
const keyboard = buildInlineKeyboard(buttons);
108+
const editParams = keyboard ? { reply_markup: keyboard, ...extra } : extra;
109+
try {
110+
await editCallbackMessage(text, editParams);
111+
} catch (editErr) {
112+
const errStr = String(editErr);
113+
if (errStr.includes("no text in the message")) {
114+
try {
115+
await deleteCallbackMessage();
116+
} catch {}
117+
await replyToCallbackChat(text, keyboard ? { reply_markup: keyboard, ...extra } : extra);
118+
} else if (!errStr.includes("message is not modified")) {
119+
throw editErr;
120+
}
121+
}
122+
};
123+
95124
return {
96125
editCallbackMessage,
97126
clearCallbackButtons,
98127
editCallbackButtons,
128+
editCallbackMessageWithButtons,
99129
deleteCallbackMessage,
100130
replyToCallbackChat,
101131
};
102132
}
133+
type ResolveQuestionParams = Parameters<typeof questionGatewayRuntime.resolveOption>[0];
134+
type QuestionResolver = (
135+
params: ResolveQuestionParams,
136+
) => ReturnType<typeof questionGatewayRuntime.resolveOption>;
137+
138+
export async function handleTelegramQuestionCallback(params: {
139+
callback: TelegramQuestionCallback;
140+
cfg: ResolveQuestionParams["cfg"];
141+
senderId: string;
142+
feedback: (text: string, terminal: boolean) => Promise<unknown>;
143+
resolveQuestion?: QuestionResolver;
144+
}): Promise<void> {
145+
let result: Awaited<ReturnType<QuestionResolver>>;
146+
try {
147+
result = await (params.resolveQuestion ?? questionGatewayRuntime.resolveOption)({
148+
cfg: params.cfg,
149+
questionId: params.callback.questionId,
150+
optionIndex: params.callback.optionIndex,
151+
senderId: params.senderId,
152+
clientDisplayName: "Telegram question",
153+
});
154+
} catch (error) {
155+
await params.feedback("Could not submit this answer.", false).catch(() => {});
156+
throw error;
157+
}
158+
await params
159+
.feedback(
160+
result.status === "answered" ? "Answer submitted." : "This question was already answered.",
161+
true,
162+
)
163+
.catch(() => {});
164+
}

0 commit comments

Comments
 (0)