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,15 +951,11 @@ 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) => {
@@ -966,8 +967,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
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(
() => [], () => [],
); );
@@ -983,8 +983,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
...(groupAllowOverride ?? groupAllowFrom ?? []), ...(groupAllowOverride ?? groupAllowFrom ?? []),
...storeAllowFrom, ...storeAllowFrom,
]); ]);
const hasGroupAllowOverride = const hasGroupAllowOverride = typeof groupAllowOverride !== "undefined";
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) => {