feat(telegram): register bot commands with setMyCommands on startup

Register /new, /model, and /models commands with Telegram's Bot API
on startup so they appear in the command menu for users. Registration
is non-fatal — if the API call fails, a warning is logged and the
bot continues listening normally.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
cyberpapi 2026-02-24 15:37:45 -05:00 committed by Chum Yin
parent 9125651775
commit 1177a83e4a

View File

@ -898,6 +898,31 @@ impl TelegramChannel {
}
}
/// Register bot commands with Telegram's `setMyCommands` API so they
/// appear in the command menu for users. Called once on startup.
async fn register_commands(&self) -> anyhow::Result<()> {
let url = self.api_url("setMyCommands");
let body = serde_json::json!({
"commands": [
{ "command": "new", "description": "Start a new conversation" },
{ "command": "model", "description": "Show or switch the current model" },
{ "command": "models", "description": "Show or switch the current provider" },
]
});
let resp = self.http_client().post(&url).json(&body).send().await?;
if !resp.status().is_success() {
let status = resp.status();
let text = resp.text().await.unwrap_or_default();
tracing::warn!("setMyCommands failed: status={status}, body={text}");
} else {
tracing::info!("Telegram bot commands registered successfully");
}
Ok(())
}
fn is_telegram_username_char(ch: char) -> bool {
ch.is_ascii_alphanumeric() || ch == '_'
}
@ -3104,6 +3129,10 @@ impl Channel for TelegramChannel {
let _ = self.get_bot_username().await;
}
if let Err(e) = self.register_commands().await {
tracing::warn!("Failed to register Telegram bot commands: {e}");
}
tracing::info!("Telegram channel listening for messages...");
// Startup probe: claim the getUpdates slot before entering the long-poll loop.