Annotation For src/tg_bot.rs
Logged in as anonymous

Origin for each line in src/tg_bot.rs from check-in fabcca1eaf:

9c4f09193a 2026-01-09    1: use stacked_errors::{
9c4f09193a 2026-01-09    2: 	Result,
9c4f09193a 2026-01-09    3: 	StackableErr,
9c4f09193a 2026-01-09    4: };
9c4f09193a 2026-01-09    5: use tgbot::{
9c4f09193a 2026-01-09    6: 	api::Client,
9c4f09193a 2026-01-09    7: 	types::{
9c4f09193a 2026-01-09    8: 		Bot,
9c4f09193a 2026-01-09    9: 		ChatPeerId,
9c4f09193a 2026-01-09   10: 		GetBot,
9c4f09193a 2026-01-09   11: 		Message,
9c4f09193a 2026-01-09   12: 		ParseMode,
9c4f09193a 2026-01-09   13: 		SendMessage,
9c4f09193a 2026-01-09   14: 	},
9c4f09193a 2026-01-09   15: };
9c4f09193a 2026-01-09   16: 
9c4f09193a 2026-01-09   17: #[derive(Clone)]
9c4f09193a 2026-01-09   18: pub struct Tg {
9c4f09193a 2026-01-09   19: 	pub me: Bot,
9c4f09193a 2026-01-09   20: 	pub owner: ChatPeerId,
9c4f09193a 2026-01-09   21: 	pub client: Client,
9c4f09193a 2026-01-09   22: }
9c4f09193a 2026-01-09   23: 
9c4f09193a 2026-01-09   24: impl Tg {
fabcca1eaf 2026-01-09   25: 	/// Construct a new `Tg` instance from configuration.
fabcca1eaf 2026-01-09   26: 	///
fabcca1eaf 2026-01-09   27: 	/// The `settings` must provide the following keys:
fabcca1eaf 2026-01-09   28: 	///  - `"api_key"` (string),
fabcca1eaf 2026-01-09   29: 	///  - `"owner"` (integer chat id),
fabcca1eaf 2026-01-09   30: 	///  - `"api_gateway"` (string).
fabcca1eaf 2026-01-09   31: 	///
fabcca1eaf 2026-01-09   32: 	/// The function initialises the client, configures the gateway and fetches the bot identity
fabcca1eaf 2026-01-09   33: 	/// before returning the constructed `Tg`.
9c4f09193a 2026-01-09   34: 	pub async fn new (settings: &config::Config) -> Result<Tg> {
9c4f09193a 2026-01-09   35: 		let api_key = settings.get_string("api_key").stack()?;
9c4f09193a 2026-01-09   36: 
9c4f09193a 2026-01-09   37: 		let owner = ChatPeerId::from(settings.get_int("owner").stack()?);
9c4f09193a 2026-01-09   38: 		let client = Client::new(&api_key).stack()?
9c4f09193a 2026-01-09   39: 			.with_host(settings.get_string("api_gateway").stack()?)
9c4f09193a 2026-01-09   40: 			.with_max_retries(0);
9c4f09193a 2026-01-09   41: 		let me = client.execute(GetBot).await.stack()?;
9c4f09193a 2026-01-09   42: 		Ok(Tg {
9c4f09193a 2026-01-09   43: 			me,
9c4f09193a 2026-01-09   44: 			owner,
9c4f09193a 2026-01-09   45: 			client,
9c4f09193a 2026-01-09   46: 		})
9c4f09193a 2026-01-09   47: 	}
9c4f09193a 2026-01-09   48: 
fabcca1eaf 2026-01-09   49: 	/// Send a text message to a chat, using an optional target and parse mode.
fabcca1eaf 2026-01-09   50: 	///
fabcca1eaf 2026-01-09   51: 	/// # Returns
fabcca1eaf 2026-01-09   52: 	/// The sent `Message` on success.
9c4f09193a 2026-01-09   53: 	pub async fn send <S>(&self, msg: S, target: Option<ChatPeerId>, mode: Option<ParseMode>) -> Result<Message>
9c4f09193a 2026-01-09   54: 	where S: Into<String> {
9c4f09193a 2026-01-09   55: 		let msg = msg.into();
9c4f09193a 2026-01-09   56: 
9c4f09193a 2026-01-09   57: 		let mode = mode.unwrap_or(ParseMode::Html);
9c4f09193a 2026-01-09   58: 		let target = target.unwrap_or(self.owner);
9c4f09193a 2026-01-09   59: 		self.client.execute(
9c4f09193a 2026-01-09   60: 			SendMessage::new(target, msg)
9c4f09193a 2026-01-09   61: 				.with_parse_mode(mode)
9c4f09193a 2026-01-09   62: 		).await.stack()
9c4f09193a 2026-01-09   63: 	}
9c4f09193a 2026-01-09   64: 
fabcca1eaf 2026-01-09   65: 	/// Create a copy of this `Tg` with the owner replaced by the given chat ID.
fabcca1eaf 2026-01-09   66: 	///
fabcca1eaf 2026-01-09   67: 	/// # Parameters
fabcca1eaf 2026-01-09   68: 	/// - `owner`: The Telegram chat identifier to set as the new owner (expressed as an `i64`).
fabcca1eaf 2026-01-09   69: 	///
fabcca1eaf 2026-01-09   70: 	/// # Returns
fabcca1eaf 2026-01-09   71: 	/// A new `Tg` instance identical to the original except its `owner` field is set to the provided chat ID.
fabcca1eaf 2026-01-09   72: 	pub fn with_owner <O>(&self, owner: O) -> Tg
fabcca1eaf 2026-01-09   73: 	where O: Into<i64> {
9c4f09193a 2026-01-09   74: 		Tg {
fabcca1eaf 2026-01-09   75: 			owner: ChatPeerId::from(owner.into()),
9c4f09193a 2026-01-09   76: 			..self.clone()
9c4f09193a 2026-01-09   77: 		}
9c4f09193a 2026-01-09   78: 	}
9c4f09193a 2026-01-09   79: }