512369f93e 2026-08-01 1: //! Telegram API integration for sending messages and attachments.
512369f93e 2026-08-01 2:
c996f5c871 2026-01-12 3: use crate::utils::{
c996f5c871 2026-01-12 4: Attachment,
c996f5c871 2026-01-12 5: validate,
c996f5c871 2026-01-12 6: };
f5ed284f8c 2025-06-21 7:
f5ed284f8c 2025-06-21 8: use std::{
f5ed284f8c 2025-06-21 9: collections::HashMap,
f5ed284f8c 2025-06-21 10: fmt::Debug,
f5ed284f8c 2025-06-21 11: };
f5ed284f8c 2025-06-21 12:
a044f68fa7 2025-08-23 13: use stacked_errors::{
1723b63d69 2026-08-01 14: bail,
f5ed284f8c 2025-06-21 15: Result,
a044f68fa7 2025-08-23 16: StackableErr,
f5ed284f8c 2025-06-21 17: };
f5ed284f8c 2025-06-21 18: use tgbot::{
f5ed284f8c 2025-06-21 19: api::Client,
f5ed284f8c 2025-06-21 20: types::{
c4f7a906af 2026-09-13 21: ChatPeerId,
c4f7a906af 2026-09-13 22: InputFile,
c4f7a906af 2026-09-13 23: InputFileReader,
c4f7a906af 2026-09-13 24: InputMediaDocument,
c4f7a906af 2026-09-13 25: InputText,
c4f7a906af 2026-09-13 26: MediaGroup,
c4f7a906af 2026-09-13 27: MediaGroupItem,
c4f7a906af 2026-09-13 28: Message,
c4f7a906af 2026-09-13 29: ParseMode::Html,
c4f7a906af 2026-09-13 30: SendDocument,
c4f7a906af 2026-09-13 31: SendMediaGroup,
c4f7a906af 2026-09-13 32: SendMessage,
f5ed284f8c 2025-06-21 33: },
f5ed284f8c 2025-06-21 34: };
f5ed284f8c 2025-06-21 35:
f5ed284f8c 2025-06-21 36: #[derive(Debug)]
f5ed284f8c 2025-06-21 37: pub struct TelegramTransport {
f5ed284f8c 2025-06-21 38: tg: Client,
f5ed284f8c 2025-06-21 39: recipients: HashMap<String, ChatPeerId>,
f5ed284f8c 2025-06-21 40: pub default: ChatPeerId,
f5ed284f8c 2025-06-21 41: }
f5ed284f8c 2025-06-21 42:
f5ed284f8c 2025-06-21 43: impl TelegramTransport {
512369f93e 2026-08-01 44: /// Creates a new `TelegramTransport` instance.
512369f93e 2026-08-01 45: ///
512369f93e 2026-08-01 46: /// # Arguments
512369f93e 2026-08-01 47: /// * `api_key` - Telegram Bot API token.
512369f93e 2026-08-01 48: /// * `recipients` - Mapping of email addresses to Telegram chat IDs.
512369f93e 2026-08-01 49: /// * `settings` - Additional configuration (API gateway, default chat).
512369f93e 2026-08-01 50: ///
512369f93e 2026-08-01 51: /// # Errors
1723b63d69 2026-08-01 52: /// Returns an error if configuration values cannot be read or if Telegram
1723b63d69 2026-08-01 53: /// API client creation fails.
14ef340959 2026-01-01 54: pub fn new (api_key: String, recipients: HashMap<String, i64>, settings: &config::Config) -> Result<TelegramTransport> {
14ef340959 2026-01-01 55: let default = settings.get_int("default")
14ef340959 2026-01-01 56: .context("[smtp2tg.toml] missing \"default\" recipient.\n")?;
14ef340959 2026-01-01 57: let api_gateway = settings.get_string("api_gateway")
14ef340959 2026-01-01 58: .context("[smtp2tg.toml] missing \"api_gateway\" destination.\n")?;
0f47e23e21 2026-01-12 59:
f5ed284f8c 2025-06-21 60: let tg = Client::new(api_key)
14ef340959 2026-01-01 61: .context("Failed to create API.\n")?
14ef340959 2026-01-01 62: .with_host(api_gateway);
f5ed284f8c 2025-06-21 63: let recipients = recipients.into_iter()
f5ed284f8c 2025-06-21 64: .map(|(a, b)| (a, ChatPeerId::from(b))).collect();
f5ed284f8c 2025-06-21 65: let default = ChatPeerId::from(default);
f5ed284f8c 2025-06-21 66:
f5ed284f8c 2025-06-21 67: Ok(TelegramTransport {
f5ed284f8c 2025-06-21 68: tg,
f5ed284f8c 2025-06-21 69: recipients,
f5ed284f8c 2025-06-21 70: default,
f5ed284f8c 2025-06-21 71: })
f5ed284f8c 2025-06-21 72: }
f5ed284f8c 2025-06-21 73:
512369f93e 2026-08-01 74: /// Sends a debug message to the default chat.
512369f93e 2026-08-01 75: ///
512369f93e 2026-08-01 76: /// # Arguments
512369f93e 2026-08-01 77: /// * `msg` - Message text to send.
512369f93e 2026-08-01 78: ///
512369f93e 2026-08-01 79: /// # Returns
512369f93e 2026-08-01 80: /// * `Result<Message>` - Telegram API response.
1723b63d69 2026-08-01 81: ///
1723b63d69 2026-08-01 82: /// # Errors
1723b63d69 2026-08-01 83: /// Returns an error if `msg` contains a closing Telegram tag or sending fails.
512369f93e 2026-08-01 84: pub async fn debug (&self, msg: &str) -> Result<Message> {
d711370ce3 2026-09-11 85: self.send(&self.default, &format!("<pre>{}</pre>", validate(msg).stack()?)).await
512369f93e 2026-08-01 86: }
512369f93e 2026-08-01 87:
512369f93e 2026-08-01 88: /// Retrieves a chat ID by name.
512369f93e 2026-08-01 89: ///
512369f93e 2026-08-01 90: /// # Arguments
512369f93e 2026-08-01 91: /// * `name` - Name or email to look up.
512369f93e 2026-08-01 92: ///
512369f93e 2026-08-01 93: /// # Returns
512369f93e 2026-08-01 94: /// * `Result<&ChatPeerId>` - Chat ID if found.
1723b63d69 2026-08-01 95: ///
1723b63d69 2026-08-01 96: /// # Errors
1723b63d69 2026-08-01 97: /// Returns an error if `name` is not configured.
512369f93e 2026-08-01 98: pub fn get (&self, name: &str) -> Result<&ChatPeerId> {
aaa78fed23 2026-08-01 99: self.recipients.get(&name.to_lowercase())
0acb6536ae 2026-09-10 100: .with_context(|| format!("Recipient {name:?} not found in configuration"))
512369f93e 2026-08-01 101: }
512369f93e 2026-08-01 102:
512369f93e 2026-08-01 103: /// Sends a text message to a specified chat.
512369f93e 2026-08-01 104: ///
512369f93e 2026-08-01 105: /// # Arguments
512369f93e 2026-08-01 106: /// * `to` - Target chat ID.
512369f93e 2026-08-01 107: /// * `msg` - Message text (supports HTML formatting).
512369f93e 2026-08-01 108: ///
512369f93e 2026-08-01 109: /// # Returns
512369f93e 2026-08-01 110: /// * `Result<Message>` - Telegram API response.
d711370ce3 2026-09-11 111: pub async fn send (&self, to: &ChatPeerId, msg: &str) -> Result<Message> {
512369f93e 2026-08-01 112: self.tg.execute(
d711370ce3 2026-09-11 113: SendMessage::new(*to, InputText::from(msg).with_format(Html))
a044f68fa7 2025-08-23 114: ).await.stack()
a044f68fa7 2025-08-23 115: }
a044f68fa7 2025-08-23 116:
512369f93e 2026-08-01 117: /// Sends a message with attachments to a specified chat.
512369f93e 2026-08-01 118: ///
512369f93e 2026-08-01 119: /// # Arguments
512369f93e 2026-08-01 120: /// * `to` - Target chat ID.
1723b63d69 2026-08-01 121: /// * `media` - List of attachments, non-empty.
512369f93e 2026-08-01 122: /// * `msg` - Message text (supports HTML formatting).
512369f93e 2026-08-01 123: ///
512369f93e 2026-08-01 124: /// # Returns
e678fcdc39 2026-09-12 125: /// * `Result<()>` - fails if `media` is empty, problems forming media
b3ca5f23ee 2026-09-12 126: /// group, request fails.
f5ed284f8c 2025-06-21 127: pub async fn sendgroup (&self, to: &ChatPeerId, media: Vec<Attachment>, msg: &str) -> Result<()> {
f5ed284f8c 2025-06-21 128: if media.len() > 1 {
f5ed284f8c 2025-06-21 129: let mut attach = vec![];
f5ed284f8c 2025-06-21 130: let mut pos = media.len();
f5ed284f8c 2025-06-21 131: for file in media {
d711370ce3 2026-09-11 132: let mut doc = InputMediaDocument::from(
d711370ce3 2026-09-11 133: InputFile::from(
d711370ce3 2026-09-11 134: InputFileReader::from(file.data)
d711370ce3 2026-09-11 135: .with_file_name(file.name)
d711370ce3 2026-09-11 136: )
d711370ce3 2026-09-11 137: );
f5ed284f8c 2025-06-21 138: if pos == 1 {
d711370ce3 2026-09-11 139: doc = doc.with_caption(InputText::from(msg)
d711370ce3 2026-09-11 140: .with_format(Html));
f5ed284f8c 2025-06-21 141: }
f5ed284f8c 2025-06-21 142: pos -= 1;
d711370ce3 2026-09-11 143: attach.push(MediaGroupItem::from(doc));
a044f68fa7 2025-08-23 144: }
a044f68fa7 2025-08-23 145: self.tg.execute(SendMediaGroup::new(*to, MediaGroup::new(attach).stack()?)).await.stack()?;
a044f68fa7 2025-08-23 146: } else {
1723b63d69 2026-08-01 147: if media.is_empty() {
1723b63d69 2026-08-01 148: bail!("At least one attachment is required.");
1723b63d69 2026-08-01 149: }
f5ed284f8c 2025-06-21 150: self.tg.execute(
f5ed284f8c 2025-06-21 151: SendDocument::new(
f5ed284f8c 2025-06-21 152: *to,
f5ed284f8c 2025-06-21 153: InputFileReader::from(media[0].data.clone())
d711370ce3 2026-09-11 154: .with_file_name(media[0].name.clone())
d711370ce3 2026-09-11 155: ).with_caption(InputText::from(msg)
d711370ce3 2026-09-11 156: .with_format(Html))
a044f68fa7 2025-08-23 157: ).await.stack()?;
f5ed284f8c 2025-06-21 158: }
f5ed284f8c 2025-06-21 159: Ok(())
f5ed284f8c 2025-06-21 160: }
f5ed284f8c 2025-06-21 161: }