9c4f09193a 2026-01-09 arcade: use stacked_errors::{
9c4f09193a 2026-01-09 arcade: Result,
9c4f09193a 2026-01-09 arcade: StackableErr,
9c4f09193a 2026-01-09 arcade: };
9c4f09193a 2026-01-09 arcade: use tgbot::{
9c4f09193a 2026-01-09 arcade: api::Client,
9c4f09193a 2026-01-09 arcade: types::{
9c4f09193a 2026-01-09 arcade: Bot,
9c4f09193a 2026-01-09 arcade: ChatPeerId,
9c4f09193a 2026-01-09 arcade: GetBot,
9c4f09193a 2026-01-09 arcade: Message,
9c4f09193a 2026-01-09 arcade: ParseMode,
9c4f09193a 2026-01-09 arcade: SendMessage,
9c4f09193a 2026-01-09 arcade: },
9c4f09193a 2026-01-09 arcade: };
9c4f09193a 2026-01-09 arcade:
9c4f09193a 2026-01-09 arcade: #[derive(Clone)]
9c4f09193a 2026-01-09 arcade: pub struct Tg {
9c4f09193a 2026-01-09 arcade: pub me: Bot,
9c4f09193a 2026-01-09 arcade: pub owner: ChatPeerId,
9c4f09193a 2026-01-09 arcade: pub client: Client,
9c4f09193a 2026-01-09 arcade: }
9c4f09193a 2026-01-09 arcade:
9c4f09193a 2026-01-09 arcade: impl Tg {
fabcca1eaf 2026-01-09 arcade: /// Construct a new `Tg` instance from configuration.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// The `settings` must provide the following keys:
fabcca1eaf 2026-01-09 arcade: /// - `"api_key"` (string),
fabcca1eaf 2026-01-09 arcade: /// - `"owner"` (integer chat id),
fabcca1eaf 2026-01-09 arcade: /// - `"api_gateway"` (string).
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// The function initialises the client, configures the gateway and fetches the bot identity
fabcca1eaf 2026-01-09 arcade: /// before returning the constructed `Tg`.
9c4f09193a 2026-01-09 arcade: pub async fn new (settings: &config::Config) -> Result<Tg> {
9c4f09193a 2026-01-09 arcade: let api_key = settings.get_string("api_key").stack()?;
9c4f09193a 2026-01-09 arcade:
9c4f09193a 2026-01-09 arcade: let owner = ChatPeerId::from(settings.get_int("owner").stack()?);
9c4f09193a 2026-01-09 arcade: let client = Client::new(&api_key).stack()?
9c4f09193a 2026-01-09 arcade: .with_host(settings.get_string("api_gateway").stack()?)
9c4f09193a 2026-01-09 arcade: .with_max_retries(0);
9c4f09193a 2026-01-09 arcade: let me = client.execute(GetBot).await.stack()?;
9c4f09193a 2026-01-09 arcade: Ok(Tg {
9c4f09193a 2026-01-09 arcade: me,
9c4f09193a 2026-01-09 arcade: owner,
9c4f09193a 2026-01-09 arcade: client,
9c4f09193a 2026-01-09 arcade: })
9c4f09193a 2026-01-09 arcade: }
9c4f09193a 2026-01-09 arcade:
fabcca1eaf 2026-01-09 arcade: /// Send a text message to a chat, using an optional target and parse mode.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// # Returns
fabcca1eaf 2026-01-09 arcade: /// The sent `Message` on success.
9c4f09193a 2026-01-09 arcade: pub async fn send <S>(&self, msg: S, target: Option<ChatPeerId>, mode: Option<ParseMode>) -> Result<Message>
9c4f09193a 2026-01-09 arcade: where S: Into<String> {
9c4f09193a 2026-01-09 arcade: let msg = msg.into();
9c4f09193a 2026-01-09 arcade:
9c4f09193a 2026-01-09 arcade: let mode = mode.unwrap_or(ParseMode::Html);
9c4f09193a 2026-01-09 arcade: let target = target.unwrap_or(self.owner);
9c4f09193a 2026-01-09 arcade: self.client.execute(
9c4f09193a 2026-01-09 arcade: SendMessage::new(target, msg)
9c4f09193a 2026-01-09 arcade: .with_parse_mode(mode)
9c4f09193a 2026-01-09 arcade: ).await.stack()
9c4f09193a 2026-01-09 arcade: }
9c4f09193a 2026-01-09 arcade:
fabcca1eaf 2026-01-09 arcade: /// Create a copy of this `Tg` with the owner replaced by the given chat ID.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// # Parameters
fabcca1eaf 2026-01-09 arcade: /// - `owner`: The Telegram chat identifier to set as the new owner (expressed as an `i64`).
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// # Returns
fabcca1eaf 2026-01-09 arcade: /// A new `Tg` instance identical to the original except its `owner` field is set to the provided chat ID.
fabcca1eaf 2026-01-09 arcade: pub fn with_owner <O>(&self, owner: O) -> Tg
fabcca1eaf 2026-01-09 arcade: where O: Into<i64> {
9c4f09193a 2026-01-09 arcade: Tg {
fabcca1eaf 2026-01-09 arcade: owner: ChatPeerId::from(owner.into()),
9c4f09193a 2026-01-09 arcade: ..self.clone()
9c4f09193a 2026-01-09 arcade: }
9c4f09193a 2026-01-09 arcade: }
9c4f09193a 2026-01-09 arcade: }