mirror of
https://github.com/wassname/pi-telegram.git
synced 2026-06-27 17:31:23 +08:00
Translate dashes to underscores for Telegram bot commands
Pi command names like compact-now were silently dropped from
setMyCommands because Telegram requires names to match
^[a-z0-9_]{1,32}$ - no dashes. They showed up in /help (which
iterates pi.getCommands() directly) but were untypable via
autocomplete and, even when typed manually, fell through to the
agent as plain text rather than dispatching.
Now: register the underscore-translated form with Telegram, show the
underscore form in /help so it matches what the user sees in the
menu, and on dispatch rewrite the message text from /foo_bar back to
/foo-bar before enqueueing so the harness's slash-command parser
finds the registered command.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+19
-1
@@ -73,8 +73,26 @@ export interface TelegramBotCommand {
|
||||
|
||||
export const TELEGRAM_BOT_COMMAND_LIMIT = 100;
|
||||
|
||||
// Telegram requires command names to match ^[a-z0-9_]{1,32}$ (no dashes).
|
||||
// Pi commands frequently use dashes (e.g. "compact-now"), so we transform
|
||||
// dash -> underscore for the Telegram-facing name and reverse it on the way
|
||||
// back so the harness still receives its original name.
|
||||
const telegramCommandNamePattern = /^[a-z0-9_]{1,32}$/;
|
||||
|
||||
export function toTelegramCommandName(name: string): string {
|
||||
return name.replace(/-/g, "_");
|
||||
}
|
||||
|
||||
export function fromTelegramCommandName(
|
||||
telegramName: string,
|
||||
piCommandNames: ReadonlySet<string>,
|
||||
): string {
|
||||
if (piCommandNames.has(telegramName)) return telegramName;
|
||||
const dashed = telegramName.replace(/_/g, "-");
|
||||
if (piCommandNames.has(dashed)) return dashed;
|
||||
return telegramName;
|
||||
}
|
||||
|
||||
const bridgeLocalBotCommands: TelegramBotCommand[] = [
|
||||
{ command: "start", description: "Show help and refresh this menu" },
|
||||
{ command: "help", description: "Show help" },
|
||||
@@ -104,7 +122,7 @@ export function buildTelegramBotCommands(
|
||||
}
|
||||
for (const command of piCommands) {
|
||||
addCommand({
|
||||
command: command.name,
|
||||
command: toTelegramCommandName(command.name),
|
||||
description: command.description ?? command.name,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user