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