From 2b8d77593d766ef5702c102085afa604097865b2 Mon Sep 17 00:00:00 2001 From: "andrea.difonzo" Date: Thu, 12 Nov 2026 11:12:01 +0100 Subject: [PATCH] Telegram: un topic per sessione, streaming, pulsanti e impostazioni MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Menu comandi (setMyCommands, chat private): /sessioni /nuova /stato /stop /impostazioni /aiuto; /usa rimosso. - Un topic per sessione (createForumTopic in chat privata), nome " · ", rinominato al cambio di modello e "(chiusa)" alla chiusura; mappa salvata in AKODE_HOME/telegram-topics.json. Senza topic: prefisso [cartella · modello] e scelta della sessione con pulsanti. - /sessioni con Apri (link al topic), Stato e Stop; il messaggio si aggiorna modificandolo. - Streaming con sendMessageDraft (can_stop, al massimo 1/s, stop = interrompe il turno), messaggio finale con piede "ctx · t/s · durata". - Approvazioni con pulsanti colorati, poi un pulsante disattivato con l'esito. - Piano come elenco modificato; reazioni ricevuto/fatto configurabili. - /impostazioni con interruttori Sì/No salvati in [telegram] (streaming, reactions nuovi). - Messaggi modificati: un comando modificato riparte, il testo no. - Pulsanti con token opachi lato server; allowlist anche sui pulsanti. - Nessuna emoji nei testi in uscita (test dedicato). Co-Authored-By: Claude Opus 5.5 --- akode-rs/config/src/types.rs | 4 + akode-rs/core/config.schema.json | 8 + akode-rs/telegram/src/api.rs | 209 ++- akode-rs/telegram/src/bridge.rs | 1535 +++++++++++++---- akode-rs/telegram/src/lib.rs | 2 + akode-rs/telegram/src/outbox.rs | 379 ++++ akode-rs/telegram/src/pairing.rs | 4 +- akode-rs/telegram/src/settings.rs | 121 +- akode-rs/telegram/src/text.rs | 135 +- akode-rs/telegram/src/topics.rs | 165 ++ akode-rs/telegram/tests/bridge.rs | 485 +++++- ...nel__tests__telegram_panel_status_120.snap | 5 +- ...anel__tests__telegram_panel_status_80.snap | 6 +- ...ests__telegram_panel_unconfigured_120.snap | 5 +- ...tests__telegram_panel_unconfigured_80.snap | 6 +- akode-rs/tui/src/telegram_panel.rs | 22 + akode-rs/tui/src/telegram_panel_tests.rs | 1 + 17 files changed, 2643 insertions(+), 449 deletions(-) create mode 100644 akode-rs/telegram/src/outbox.rs create mode 100644 akode-rs/telegram/src/topics.rs diff --git a/akode-rs/config/src/types.rs b/akode-rs/config/src/types.rs index 3b5531f5..0a308b62 100644 --- a/akode-rs/config/src/types.rs +++ b/akode-rs/config/src/types.rs @@ -263,6 +263,10 @@ pub struct TelegramConfigToml { pub allowed_chat_ids: Option>, /// Which events are sent to Telegram. Every level defaults to `true`. pub notify: Option, + /// Live output of running turns (message drafts) and the plan. Defaults to `true`. + pub streaming: Option, + /// Reactions on the user's messages (received, done). Defaults to `true`. + pub reactions: Option, } /// `[telegram.notify]`: which events reach Telegram. diff --git a/akode-rs/core/config.schema.json b/akode-rs/core/config.schema.json index d5771090..9458f2ad 100644 --- a/akode-rs/core/config.schema.json +++ b/akode-rs/core/config.schema.json @@ -4759,6 +4759,14 @@ } ], "description": "Which events are sent to Telegram. Every level defaults to `true`." + }, + "reactions": { + "description": "Reactions on the user's messages (received, done). Defaults to `true`.", + "type": "boolean" + }, + "streaming": { + "description": "Live output of running turns (message drafts) and the plan. Defaults to `true`.", + "type": "boolean" } }, "type": "object" diff --git a/akode-rs/telegram/src/api.rs b/akode-rs/telegram/src/api.rs index b8b67ddb..bf3d2fe6 100644 --- a/akode-rs/telegram/src/api.rs +++ b/akode-rs/telegram/src/api.rs @@ -63,6 +63,9 @@ pub struct User { pub username: Option, #[serde(default)] pub first_name: String, + /// Topics in private chats are enabled for the bot (`getMe` only). + #[serde(default)] + pub has_topics_enabled: Option, } #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] @@ -75,27 +78,61 @@ pub struct Message { pub message_id: i64, pub chat: Chat, #[serde(default)] + pub message_thread_id: Option, + #[serde(default)] + pub is_topic_message: Option, + #[serde(default)] pub text: Option, #[serde(default)] pub reply_to_message: Option>, } +impl Message { + /// The topic the message belongs to, if any. + pub fn topic(&self) -> Option { + self.message_thread_id + .filter(|_| self.is_topic_message.unwrap_or(true)) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] pub struct CallbackQuery { pub id: String, #[serde(default)] + pub from: Option, + #[serde(default)] pub message: Option, #[serde(default)] pub data: Option, } +/// The user pressed the stop button of a streamed draft. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +pub struct MessageGenerationStopped { + pub chat: Chat, + #[serde(default)] + pub message_thread_id: Option, + pub draft_id: i64, +} + +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +pub struct ForumTopic { + pub message_thread_id: i64, + #[serde(default)] + pub name: String, +} + #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] pub struct Update { pub update_id: i64, #[serde(default)] pub message: Option, #[serde(default)] + pub edited_message: Option, + #[serde(default)] pub callback_query: Option, + #[serde(default)] + pub stopped_message_generation: Option, } impl Update { @@ -103,6 +140,7 @@ impl Update { pub fn chat_id(&self) -> Option { self.message .as_ref() + .or(self.edited_message.as_ref()) .map(|message| message.chat.id) .or_else(|| { self.callback_query @@ -110,14 +148,78 @@ impl Update { .and_then(|query| query.message.as_ref()) .map(|message| message.chat.id) }) + .or_else(|| { + self.stopped_message_generation + .as_ref() + .map(|stopped| stopped.chat.id) + }) } } +/// `DisabledButton`: currently an empty object. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct DisabledButton {} + /// One inline keyboard button. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Button { pub text: String, - pub callback_data: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub callback_data: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub url: Option, + /// `success` (green), `danger` (red) or `primary` (blue). + #[serde(skip_serializing_if = "Option::is_none")] + pub style: Option<&'static str>, + #[serde(skip_serializing_if = "Option::is_none")] + pub disabled: Option, +} + +impl Button { + pub fn callback(text: impl Into, data: impl Into) -> Self { + Self { + text: text.into(), + callback_data: Some(data.into()), + url: None, + style: None, + disabled: None, + } + } + + pub fn link(text: impl Into, url: impl Into) -> Self { + Self { + text: text.into(), + callback_data: None, + url: Some(url.into()), + style: None, + disabled: None, + } + } + + /// A button that does nothing, used to show an outcome in place of choices. + pub fn disabled(text: impl Into) -> Self { + Self { + text: text.into(), + callback_data: None, + url: None, + style: None, + disabled: Some(DisabledButton {}), + } + } + + pub fn styled(mut self, style: &'static str) -> Self { + self.style = Some(style); + self + } +} + +/// A message entity; offsets and lengths are in UTF-16 code units. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Entity { + #[serde(rename = "type")] + pub kind: &'static str, + pub offset: usize, + pub length: usize, } #[derive(Deserialize)] @@ -204,7 +306,12 @@ impl TelegramApi { ) -> Result, ApiError> { let mut body = json!({ "timeout": timeout_secs, - "allowed_updates": ["message", "callback_query"], + "allowed_updates": [ + "message", + "edited_message", + "callback_query", + "stopped_message_generation", + ], }); if let Some(offset) = offset { body["offset"] = json!(offset); @@ -217,17 +324,26 @@ impl TelegramApi { .await } + async fn call_unit(&self, method: &str, body: Value) -> Result<(), ApiError> { + self.call::(method, body, REQUEST_TIMEOUT) + .await + .map(|_| ()) + } + pub async fn send_message( &self, chat_id: i64, + thread: Option, text: &str, - reply_to: Option, + entities: &[Entity], buttons: Option<&[Vec