Annotation For src/telegram.rs
Logged in as anonymous

Origin for each line in src/telegram.rs from check-in 14ef340959:

f5ed284f8c 2025-06-21    1: use crate::utils::{
f5ed284f8c 2025-06-21    2: 	Attachment,
f5ed284f8c 2025-06-21    3: 	RE_SPECIAL,
f5ed284f8c 2025-06-21    4: };
f5ed284f8c 2025-06-21    5: 
f5ed284f8c 2025-06-21    6: use std::{
f5ed284f8c 2025-06-21    7: 	borrow::Cow,
f5ed284f8c 2025-06-21    8: 	collections::HashMap,
f5ed284f8c 2025-06-21    9: 	fmt::Debug,
f5ed284f8c 2025-06-21   10: };
f5ed284f8c 2025-06-21   11: 
a044f68fa7 2025-08-23   12: use stacked_errors::{
f5ed284f8c 2025-06-21   13: 	Result,
a044f68fa7 2025-08-23   14: 	StackableErr,
f5ed284f8c 2025-06-21   15: };
f5ed284f8c 2025-06-21   16: use tgbot::{
f5ed284f8c 2025-06-21   17: 	api::Client,
f5ed284f8c 2025-06-21   18: 	types::{
f5ed284f8c 2025-06-21   19: 		ChatPeerId,
f5ed284f8c 2025-06-21   20: 		InputFile,
f5ed284f8c 2025-06-21   21: 		InputFileReader,
f5ed284f8c 2025-06-21   22: 		InputMediaDocument,
f5ed284f8c 2025-06-21   23: 		MediaGroup,
f5ed284f8c 2025-06-21   24: 		MediaGroupItem,
f5ed284f8c 2025-06-21   25: 		Message,
f5ed284f8c 2025-06-21   26: 		ParseMode::MarkdownV2,
f5ed284f8c 2025-06-21   27: 		SendMediaGroup,
f5ed284f8c 2025-06-21   28: 		SendMessage,
f5ed284f8c 2025-06-21   29: 		SendDocument,
f5ed284f8c 2025-06-21   30: 	},
f5ed284f8c 2025-06-21   31: };
f5ed284f8c 2025-06-21   32: 
f5ed284f8c 2025-06-21   33: /// Encodes special HTML entities to prevent them interfering with Telegram HTML
f5ed284f8c 2025-06-21   34: pub fn encode (text: &str) -> Cow<'_, str> {
f5ed284f8c 2025-06-21   35: 	RE_SPECIAL.replace_all(text, "\\$1")
f5ed284f8c 2025-06-21   36: }
f5ed284f8c 2025-06-21   37: 
f5ed284f8c 2025-06-21   38: #[derive(Debug)]
f5ed284f8c 2025-06-21   39: pub struct TelegramTransport {
f5ed284f8c 2025-06-21   40: 	tg: Client,
f5ed284f8c 2025-06-21   41: 	recipients: HashMap<String, ChatPeerId>,
f5ed284f8c 2025-06-21   42: 	pub default: ChatPeerId,
f5ed284f8c 2025-06-21   43: }
f5ed284f8c 2025-06-21   44: 
f5ed284f8c 2025-06-21   45: impl TelegramTransport {
f5ed284f8c 2025-06-21   46: 
14ef340959 2026-01-01   47: 	pub fn new (api_key: String, recipients: HashMap<String, i64>, settings: &config::Config) -> Result<TelegramTransport> {
14ef340959 2026-01-01   48: 		let default = settings.get_int("default")
14ef340959 2026-01-01   49: 			.context("[smtp2tg.toml] missing \"default\" recipient.\n")?;
14ef340959 2026-01-01   50: 		let api_gateway = settings.get_string("api_gateway")
14ef340959 2026-01-01   51: 			.context("[smtp2tg.toml] missing \"api_gateway\" destination.\n")?;
f5ed284f8c 2025-06-21   52: 		let tg = Client::new(api_key)
14ef340959 2026-01-01   53: 			.context("Failed to create API.\n")?
14ef340959 2026-01-01   54: 			.with_host(api_gateway);
f5ed284f8c 2025-06-21   55: 		let recipients = recipients.into_iter()
f5ed284f8c 2025-06-21   56: 			.map(|(a, b)| (a, ChatPeerId::from(b))).collect();
f5ed284f8c 2025-06-21   57: 		let default = ChatPeerId::from(default);
f5ed284f8c 2025-06-21   58: 
f5ed284f8c 2025-06-21   59: 		Ok(TelegramTransport {
f5ed284f8c 2025-06-21   60: 			tg,
f5ed284f8c 2025-06-21   61: 			recipients,
f5ed284f8c 2025-06-21   62: 			default,
f5ed284f8c 2025-06-21   63: 		})
f5ed284f8c 2025-06-21   64: 	}
f5ed284f8c 2025-06-21   65: 
f5ed284f8c 2025-06-21   66: 	/// Send message to default user, used for debug/log/info purposes
f5ed284f8c 2025-06-21   67: 	pub async fn debug (&self, msg: &str) -> Result<Message> {
f5ed284f8c 2025-06-21   68: 		self.send(&self.default, encode(msg)).await
f5ed284f8c 2025-06-21   69: 	}
f5ed284f8c 2025-06-21   70: 
f5ed284f8c 2025-06-21   71: 	/// Get recipient by address
f5ed284f8c 2025-06-21   72: 	pub fn get (&self, name: &str) -> Result<&ChatPeerId> {
f5ed284f8c 2025-06-21   73: 		self.recipients.get(name)
f5ed284f8c 2025-06-21   74: 			.with_context(|| format!("Recipient \"{name}\" not found in configuration"))
f5ed284f8c 2025-06-21   75: 	}
f5ed284f8c 2025-06-21   76: 
f5ed284f8c 2025-06-21   77: 	/// Send message to specified user
f5ed284f8c 2025-06-21   78: 	pub async fn send <S> (&self, to: &ChatPeerId, msg: S) -> Result<Message>
f5ed284f8c 2025-06-21   79: 	where S: Into<String> + Debug{
a044f68fa7 2025-08-23   80: 		self.tg.execute(
f5ed284f8c 2025-06-21   81: 			SendMessage::new(*to, msg)
f5ed284f8c 2025-06-21   82: 			.with_parse_mode(MarkdownV2)
a044f68fa7 2025-08-23   83: 		).await.stack()
f5ed284f8c 2025-06-21   84: 	}
f5ed284f8c 2025-06-21   85: 
f5ed284f8c 2025-06-21   86: 	/// Send media to specified user
f5ed284f8c 2025-06-21   87: 	pub async fn sendgroup (&self, to: &ChatPeerId, media: Vec<Attachment>, msg: &str) -> Result<()> {
f5ed284f8c 2025-06-21   88: 		if media.len() > 1 {
f5ed284f8c 2025-06-21   89: 			let mut attach = vec![];
f5ed284f8c 2025-06-21   90: 			let mut pos = media.len();
f5ed284f8c 2025-06-21   91: 			for file in media {
f5ed284f8c 2025-06-21   92: 				let mut caption = InputMediaDocument::default();
f5ed284f8c 2025-06-21   93: 				if pos == 1 {
f5ed284f8c 2025-06-21   94: 					caption = caption.with_caption(msg)
f5ed284f8c 2025-06-21   95: 						.with_caption_parse_mode(MarkdownV2);
f5ed284f8c 2025-06-21   96: 				}
f5ed284f8c 2025-06-21   97: 				pos -= 1;
f5ed284f8c 2025-06-21   98: 				attach.push(
f5ed284f8c 2025-06-21   99: 					MediaGroupItem::for_document(
f5ed284f8c 2025-06-21  100: 						InputFile::from(
f5ed284f8c 2025-06-21  101: 							InputFileReader::from(file.data)
f5ed284f8c 2025-06-21  102: 								.with_file_name(file.name)
f5ed284f8c 2025-06-21  103: 						),
f5ed284f8c 2025-06-21  104: 						caption
f5ed284f8c 2025-06-21  105: 					)
f5ed284f8c 2025-06-21  106: 				);
f5ed284f8c 2025-06-21  107: 			}
a044f68fa7 2025-08-23  108: 			self.tg.execute(SendMediaGroup::new(*to, MediaGroup::new(attach).stack()?)).await.stack()?;
f5ed284f8c 2025-06-21  109: 		} else {
f5ed284f8c 2025-06-21  110: 			self.tg.execute(
f5ed284f8c 2025-06-21  111: 				SendDocument::new(
f5ed284f8c 2025-06-21  112: 					*to,
f5ed284f8c 2025-06-21  113: 					InputFileReader::from(media[0].data.clone())
f5ed284f8c 2025-06-21  114: 					.with_file_name(media[0].name.clone())
f5ed284f8c 2025-06-21  115: 				).with_caption(msg)
f5ed284f8c 2025-06-21  116: 				.with_caption_parse_mode(MarkdownV2)
a044f68fa7 2025-08-23  117: 			).await.stack()?;
f5ed284f8c 2025-06-21  118: 		}
f5ed284f8c 2025-06-21  119: 		Ok(())
f5ed284f8c 2025-06-21  120: 	}
f5ed284f8c 2025-06-21  121: }