Annotation For src/tg_bot.rs
Logged in as anonymous

Lines of src/tg_bot.rs from check-in fabcca1eaf that are changed by the sequence of edits moving toward check-in 13265e7697:

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