9adc69d514 2026-03-25 1: use crate::{
9adc69d514 2026-03-25 2: Arc,
9adc69d514 2026-03-25 3: Mutex,
9adc69d514 2026-03-25 4: };
9adc69d514 2026-03-25 5:
9adc69d514 2026-03-25 6: use std::{
9adc69d514 2026-03-25 7: borrow::Cow,
9adc69d514 2026-03-25 8: collections::HashMap,
9adc69d514 2026-03-25 9: fmt,
9adc69d514 2026-03-25 10: };
9adc69d514 2026-03-25 11:
13265e7697 2026-01-10 12: use serde::{
13265e7697 2026-01-10 13: Deserialize,
13265e7697 2026-01-10 14: Serialize,
13265e7697 2026-01-10 15: };
9c4f09193a 2026-01-09 16: use stacked_errors::{
9adc69d514 2026-03-25 17: bail,
9c4f09193a 2026-01-09 18: Result,
9c4f09193a 2026-01-09 19: StackableErr,
9c4f09193a 2026-01-09 20: };
9c4f09193a 2026-01-09 21: use tgbot::{
9c4f09193a 2026-01-09 22: api::Client,
9c4f09193a 2026-01-09 23: types::{
9c4f09193a 2026-01-09 24: Bot,
9c4f09193a 2026-01-09 25: ChatPeerId,
9c4f09193a 2026-01-09 26: GetBot,
13265e7697 2026-01-10 27: InlineKeyboardButton,
13265e7697 2026-01-10 28: InlineKeyboardMarkup,
9c4f09193a 2026-01-09 29: Message,
9c4f09193a 2026-01-09 30: ParseMode,
9c4f09193a 2026-01-09 31: SendMessage,
9c4f09193a 2026-01-09 32: },
9c4f09193a 2026-01-09 33: };
13265e7697 2026-01-10 34:
9adc69d514 2026-03-25 35: const CB_VERSION: u8 = 0;
9adc69d514 2026-03-25 36:
13265e7697 2026-01-10 37: #[derive(Serialize, Deserialize, Debug)]
9adc69d514 2026-03-25 38: pub enum Callback {
9adc69d514 2026-03-25 39: // List all feeds (version, name to show, page number)
9adc69d514 2026-03-25 40: List(u8, String, u8),
9adc69d514 2026-03-25 41: }
9adc69d514 2026-03-25 42:
9adc69d514 2026-03-25 43: impl Callback {
9adc69d514 2026-03-25 44: pub fn list (text: &str, page: u8) -> Callback {
9adc69d514 2026-03-25 45: Callback::List(CB_VERSION, text.to_owned(), page)
9adc69d514 2026-03-25 46: }
9adc69d514 2026-03-25 47:
9adc69d514 2026-03-25 48: fn version (&self) -> u8 {
9adc69d514 2026-03-25 49: match self {
9adc69d514 2026-03-25 50: Callback::List(version, .. ) => *version,
9adc69d514 2026-03-25 51: }
9adc69d514 2026-03-25 52: }
9adc69d514 2026-03-25 53: }
9adc69d514 2026-03-25 54:
9adc69d514 2026-03-25 55: impl fmt::Display for Callback {
9adc69d514 2026-03-25 56: fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
9adc69d514 2026-03-25 57: f.write_str(&toml::to_string(self).map_err(|_| fmt::Error)?)
9adc69d514 2026-03-25 58: }
9adc69d514 2026-03-25 59: }
9adc69d514 2026-03-25 60:
9adc69d514 2026-03-25 61: /// Produce new Keyboard Markup from current Callback
9adc69d514 2026-03-25 62: pub async fn get_kb (cb: &Callback, feeds: Arc<Mutex<HashMap<i32, String>>>) -> Result<InlineKeyboardMarkup> {
9adc69d514 2026-03-25 63: if cb.version() != CB_VERSION {
9adc69d514 2026-03-25 64: bail!("Wrong callback version.");
9adc69d514 2026-03-25 65: }
9adc69d514 2026-03-25 66: let mark = match cb {
9adc69d514 2026-03-25 67: Callback::List(_, name, page) => {
9adc69d514 2026-03-25 68: let mut kb = vec![];
9adc69d514 2026-03-25 69: let feeds = feeds.lock_arc().await;
9adc69d514 2026-03-25 70: let long = feeds.len() > 6;
9adc69d514 2026-03-25 71: let (start, end) = if long {
9adc69d514 2026-03-25 72: (page * 5, 5 + page * 5)
9adc69d514 2026-03-25 73: } else {
9adc69d514 2026-03-25 74: (0, 6)
9adc69d514 2026-03-25 75: };
9adc69d514 2026-03-25 76: let mut i = 0;
9adc69d514 2026-03-25 77: if name.is_empty() {
374eadef45 2026-03-28 78: for (id, name) in feeds.iter() {
374eadef45 2026-03-28 79: if i < start { continue }
374eadef45 2026-03-28 80: if i > end { break }
374eadef45 2026-03-28 81: i += 1;
374eadef45 2026-03-28 82: kb.push(vec![
374eadef45 2026-03-28 83: InlineKeyboardButton::for_callback_data(
374eadef45 2026-03-28 84: format!("{}. {}", id, name),
374eadef45 2026-03-28 85: Callback::list("xxx", *page).to_string()), // XXX edit
374eadef45 2026-03-28 86: ]);
374eadef45 2026-03-28 87: }
9adc69d514 2026-03-25 88: } else {
374eadef45 2026-03-28 89: let mut found = false;
374eadef45 2026-03-28 90: let mut first_page = None;
374eadef45 2026-03-28 91: for (id, feed_name) in feeds.iter() {
374eadef45 2026-03-28 92: if name == feed_name {
374eadef45 2026-03-28 93: found = true;
374eadef45 2026-03-28 94: }
374eadef45 2026-03-28 95: i += 1;
374eadef45 2026-03-28 96: kb.push(vec![
374eadef45 2026-03-28 97: InlineKeyboardButton::for_callback_data(
374eadef45 2026-03-28 98: format!("{}. {}", id, feed_name),
374eadef45 2026-03-28 99: Callback::list("xxx", *page).to_string()), // XXX edit
374eadef45 2026-03-28 100: ]);
374eadef45 2026-03-28 101: if i > end {
374eadef45 2026-03-28 102: // page complete, if found we got the right page, if not - reset and
374eadef45 2026-03-28 103: // continue.
374eadef45 2026-03-28 104: if found {
374eadef45 2026-03-28 105: break
374eadef45 2026-03-28 106: } else {
374eadef45 2026-03-28 107: if first_page.is_none() {
374eadef45 2026-03-28 108: first_page = Some(kb);
374eadef45 2026-03-28 109: }
374eadef45 2026-03-28 110: kb = vec![];
374eadef45 2026-03-28 111: i = 0
374eadef45 2026-03-28 112: }
374eadef45 2026-03-28 113: }
374eadef45 2026-03-28 114: }
374eadef45 2026-03-28 115: if !found {
374eadef45 2026-03-28 116: // name not found, showing first page
374eadef45 2026-03-28 117: kb = first_page.unwrap_or_default();
374eadef45 2026-03-28 118: }
9adc69d514 2026-03-25 119: }
9adc69d514 2026-03-25 120: if long {
9adc69d514 2026-03-25 121: kb.push(vec![
9adc69d514 2026-03-25 122: InlineKeyboardButton::for_callback_data("<<",
9adc69d514 2026-03-25 123: Callback::list("", if *page == 0 { *page } else { page - 1 } ).to_string()),
9adc69d514 2026-03-25 124: InlineKeyboardButton::for_callback_data(">>",
9adc69d514 2026-03-25 125: Callback::list("", page + 1).to_string()),
9adc69d514 2026-03-25 126: ]);
9adc69d514 2026-03-25 127: }
9adc69d514 2026-03-25 128: InlineKeyboardMarkup::from(kb)
9adc69d514 2026-03-25 129: },
9adc69d514 2026-03-25 130: };
9adc69d514 2026-03-25 131: Ok(mark)
9adc69d514 2026-03-25 132: }
9adc69d514 2026-03-25 133:
9adc69d514 2026-03-25 134: pub enum MyMessage <'a> {
9adc69d514 2026-03-25 135: Html { text: Cow<'a, str> },
9adc69d514 2026-03-25 136: HtmlTo { text: Cow<'a, str>, to: ChatPeerId },
9adc69d514 2026-03-25 137: HtmlToKb { text: Cow<'a, str>, to: ChatPeerId, kb: InlineKeyboardMarkup },
9adc69d514 2026-03-25 138: Text { text: Cow<'a, str> },
9adc69d514 2026-03-25 139: TextTo { text: Cow<'a, str>, to: ChatPeerId },
13265e7697 2026-01-10 140: }
13265e7697 2026-01-10 141:
9adc69d514 2026-03-25 142: impl MyMessage <'_> {
9adc69d514 2026-03-25 143: pub fn html <'a, S> (text: S) -> MyMessage<'a>
9adc69d514 2026-03-25 144: where S: Into<Cow<'a, str>> {
9adc69d514 2026-03-25 145: let text = text.into();
9adc69d514 2026-03-25 146: MyMessage::Html { text }
9adc69d514 2026-03-25 147: }
9adc69d514 2026-03-25 148:
9adc69d514 2026-03-25 149: pub fn html_to <'a, S> (text: S, to: ChatPeerId) -> MyMessage<'a>
9adc69d514 2026-03-25 150: where S: Into<Cow<'a, str>> {
9adc69d514 2026-03-25 151: let text = text.into();
9adc69d514 2026-03-25 152: MyMessage::HtmlTo { text, to }
9adc69d514 2026-03-25 153: }
9adc69d514 2026-03-25 154:
9adc69d514 2026-03-25 155: pub fn html_to_kb <'a, S> (text: S, to: ChatPeerId, kb: InlineKeyboardMarkup) -> MyMessage<'a>
9adc69d514 2026-03-25 156: where S: Into<Cow<'a, str>> {
9adc69d514 2026-03-25 157: let text = text.into();
9adc69d514 2026-03-25 158: MyMessage::HtmlToKb { text, to, kb }
9adc69d514 2026-03-25 159: }
9adc69d514 2026-03-25 160:
9adc69d514 2026-03-25 161: pub fn text <'a, S> (text: S) -> MyMessage<'a>
9adc69d514 2026-03-25 162: where S: Into<Cow<'a, str>> {
9adc69d514 2026-03-25 163: let text = text.into();
9adc69d514 2026-03-25 164: MyMessage::Text { text }
9adc69d514 2026-03-25 165: }
9adc69d514 2026-03-25 166:
9adc69d514 2026-03-25 167: pub fn text_to <'a, S> (text: S, to: ChatPeerId) -> MyMessage<'a>
9adc69d514 2026-03-25 168: where S: Into<Cow<'a, str>> {
9adc69d514 2026-03-25 169: let text = text.into();
9adc69d514 2026-03-25 170: MyMessage::TextTo { text, to }
9adc69d514 2026-03-25 171: }
9adc69d514 2026-03-25 172:
9adc69d514 2026-03-25 173: fn req (&self, tg: &Tg) -> Result<SendMessage> {
9adc69d514 2026-03-25 174: Ok(match self {
9adc69d514 2026-03-25 175: MyMessage::Html { text } =>
9adc69d514 2026-03-25 176: SendMessage::new(tg.owner, text.as_ref())
9adc69d514 2026-03-25 177: .with_parse_mode(ParseMode::Html),
9adc69d514 2026-03-25 178: MyMessage::HtmlTo { text, to } =>
9adc69d514 2026-03-25 179: SendMessage::new(*to, text.as_ref())
9adc69d514 2026-03-25 180: .with_parse_mode(ParseMode::Html),
9adc69d514 2026-03-25 181: MyMessage::HtmlToKb { text, to, kb } =>
9adc69d514 2026-03-25 182: SendMessage::new(*to, text.as_ref())
9adc69d514 2026-03-25 183: .with_parse_mode(ParseMode::Html)
9adc69d514 2026-03-25 184: .with_reply_markup(kb.clone()),
9adc69d514 2026-03-25 185: MyMessage::Text { text } =>
9adc69d514 2026-03-25 186: SendMessage::new(tg.owner, text.as_ref())
9adc69d514 2026-03-25 187: .with_parse_mode(ParseMode::MarkdownV2),
9adc69d514 2026-03-25 188: MyMessage::TextTo { text, to } =>
9adc69d514 2026-03-25 189: SendMessage::new(*to, text.as_ref())
9adc69d514 2026-03-25 190: .with_parse_mode(ParseMode::MarkdownV2),
9adc69d514 2026-03-25 191: })
9adc69d514 2026-03-25 192: }
13265e7697 2026-01-10 193: }
9c4f09193a 2026-01-09 194:
9c4f09193a 2026-01-09 195: #[derive(Clone)]
9c4f09193a 2026-01-09 196: pub struct Tg {
9c4f09193a 2026-01-09 197: pub me: Bot,
9c4f09193a 2026-01-09 198: pub owner: ChatPeerId,
9c4f09193a 2026-01-09 199: pub client: Client,
9c4f09193a 2026-01-09 200: }
9c4f09193a 2026-01-09 201:
9c4f09193a 2026-01-09 202: impl Tg {
fabcca1eaf 2026-01-09 203: /// Construct a new `Tg` instance from configuration.
fabcca1eaf 2026-01-09 204: ///
fabcca1eaf 2026-01-09 205: /// The `settings` must provide the following keys:
fabcca1eaf 2026-01-09 206: /// - `"api_key"` (string),
fabcca1eaf 2026-01-09 207: /// - `"owner"` (integer chat id),
fabcca1eaf 2026-01-09 208: /// - `"api_gateway"` (string).
fabcca1eaf 2026-01-09 209: ///
fabcca1eaf 2026-01-09 210: /// The function initialises the client, configures the gateway and fetches the bot identity
fabcca1eaf 2026-01-09 211: /// before returning the constructed `Tg`.
9c4f09193a 2026-01-09 212: pub async fn new (settings: &config::Config) -> Result<Tg> {
9c4f09193a 2026-01-09 213: let api_key = settings.get_string("api_key").stack()?;
9c4f09193a 2026-01-09 214:
9c4f09193a 2026-01-09 215: let owner = ChatPeerId::from(settings.get_int("owner").stack()?);
9c4f09193a 2026-01-09 216: let client = Client::new(&api_key).stack()?
9c4f09193a 2026-01-09 217: .with_host(settings.get_string("api_gateway").stack()?)
9c4f09193a 2026-01-09 218: .with_max_retries(0);
9c4f09193a 2026-01-09 219: let me = client.execute(GetBot).await.stack()?;
9c4f09193a 2026-01-09 220: Ok(Tg {
9c4f09193a 2026-01-09 221: me,
9c4f09193a 2026-01-09 222: owner,
9c4f09193a 2026-01-09 223: client,
9c4f09193a 2026-01-09 224: })
9c4f09193a 2026-01-09 225: }
9c4f09193a 2026-01-09 226:
fabcca1eaf 2026-01-09 227: /// Send a text message to a chat, using an optional target and parse mode.
fabcca1eaf 2026-01-09 228: ///
fabcca1eaf 2026-01-09 229: /// # Returns
fabcca1eaf 2026-01-09 230: /// The sent `Message` on success.
9adc69d514 2026-03-25 231: pub async fn send (&self, msg: MyMessage<'_>) -> Result<Message> {
9adc69d514 2026-03-25 232: self.client.execute(msg.req(self)?).await.stack()
fabcca1eaf 2026-01-09 233: }
fabcca1eaf 2026-01-09 234:
fabcca1eaf 2026-01-09 235: /// Create a copy of this `Tg` with the owner replaced by the given chat ID.
fabcca1eaf 2026-01-09 236: ///
fabcca1eaf 2026-01-09 237: /// # Parameters
fabcca1eaf 2026-01-09 238: /// - `owner`: The Telegram chat identifier to set as the new owner (expressed as an `i64`).
fabcca1eaf 2026-01-09 239: ///
fabcca1eaf 2026-01-09 240: /// # Returns
fabcca1eaf 2026-01-09 241: /// A new `Tg` instance identical to the original except its `owner` field is set to the provided chat ID.
fabcca1eaf 2026-01-09 242: pub fn with_owner <O>(&self, owner: O) -> Tg
fabcca1eaf 2026-01-09 243: where O: Into<i64> {
fabcca1eaf 2026-01-09 244: Tg {
fabcca1eaf 2026-01-09 245: owner: ChatPeerId::from(owner.into()),
9c4f09193a 2026-01-09 246: ..self.clone()
9c4f09193a 2026-01-09 247: }
9c4f09193a 2026-01-09 248: }
9c4f09193a 2026-01-09 249: }