fix(telegram): tolerate missing native command APIs

This commit is contained in:
Peter Steinberger
2026-01-12 22:43:05 +00:00
parent 72100ba3ab
commit 73d9469bf8

View File

@@ -932,8 +932,13 @@ export function createTelegramBot(opts: TelegramBotOptions) {
? listNativeCommandSpecsForConfig(cfg) ? listNativeCommandSpecsForConfig(cfg)
: []; : [];
if (nativeCommands.length > 0) { if (nativeCommands.length > 0) {
if (typeof bot.api.setMyCommands === "function") { const api = bot.api as unknown as {
bot.api setMyCommands?: (
commands: Array<{ command: string; description: string }>,
) => Promise<unknown>;
};
if (typeof api.setMyCommands === "function") {
api
.setMyCommands( .setMyCommands(
nativeCommands.map((command) => ({ nativeCommands.map((command) => ({
command: command.name, command: command.name,
@@ -946,45 +951,39 @@ export function createTelegramBot(opts: TelegramBotOptions) {
); );
}); });
} else { } else {
runtime.info?.( logVerbose("telegram: setMyCommands unavailable; skipping registration");
"telegram: setMyCommands not available on api mock; skipping",
);
} }
if (typeof bot.command !== "function") { if (typeof (bot as unknown as { command?: unknown }).command !== "function") {
runtime.info?.( logVerbose("telegram: bot.command unavailable; skipping native handlers");
"telegram: bot.command not available on api mock; skipping native command handlers",
);
} else { } else {
for (const command of nativeCommands) { for (const command of nativeCommands) {
bot.command(command.name, async (ctx) => { bot.command(command.name, async (ctx) => {
const msg = ctx.message; const msg = ctx.message;
if (!msg) return; if (!msg) return;
if (shouldSkipUpdate(ctx)) return; if (shouldSkipUpdate(ctx)) return;
const chatId = msg.chat.id; const chatId = msg.chat.id;
const isGroup = const isGroup =
msg.chat.type === "group" || msg.chat.type === "supergroup"; msg.chat.type === "group" || msg.chat.type === "supergroup";
const messageThreadId = (msg as { message_thread_id?: number }) const messageThreadId = (msg as { message_thread_id?: number })
.message_thread_id; .message_thread_id;
const isForum = const isForum = (msg.chat as { is_forum?: boolean }).is_forum === true;
(msg.chat as { is_forum?: boolean }).is_forum === true; const storeAllowFrom = await readTelegramAllowFromStore().catch(
const storeAllowFrom = await readTelegramAllowFromStore().catch( () => [],
() => [], );
); const { groupConfig, topicConfig } = resolveTelegramGroupConfig(
const { groupConfig, topicConfig } = resolveTelegramGroupConfig( chatId,
chatId, messageThreadId,
messageThreadId, );
); const groupAllowOverride = firstDefined(
const groupAllowOverride = firstDefined( topicConfig?.allowFrom,
topicConfig?.allowFrom, groupConfig?.allowFrom,
groupConfig?.allowFrom, );
); const effectiveGroupAllow = normalizeAllowFrom([
const effectiveGroupAllow = normalizeAllowFrom([ ...(groupAllowOverride ?? groupAllowFrom ?? []),
...(groupAllowOverride ?? groupAllowFrom ?? []), ...storeAllowFrom,
...storeAllowFrom, ]);
]); const hasGroupAllowOverride = typeof groupAllowOverride !== "undefined";
const hasGroupAllowOverride =
typeof groupAllowOverride !== "undefined";
if (isGroup && groupConfig?.enabled === false) { if (isGroup && groupConfig?.enabled === false) {
await bot.api.sendMessage(chatId, "This group is disabled."); await bot.api.sendMessage(chatId, "This group is disabled.");
@@ -1149,9 +1148,16 @@ export function createTelegramBot(opts: TelegramBotOptions) {
} }
} }
} else if (nativeDisabledExplicit) { } else if (nativeDisabledExplicit) {
bot.api.setMyCommands([]).catch((err) => { const api = bot.api as unknown as { setMyCommands?: (commands: []) => Promise<unknown> };
runtime.error?.(danger(`telegram clear commands failed: ${String(err)}`)); if (typeof api.setMyCommands === "function") {
}); api.setMyCommands([]).catch((err) => {
runtime.error?.(
danger(`telegram clear commands failed: ${String(err)}`),
);
});
} else {
logVerbose("telegram: setMyCommands unavailable; skipping clear");
}
} }
bot.on("callback_query", async (ctx) => { bot.on("callback_query", async (ctx) => {