Index: src/command.rs ================================================================== --- src/command.rs +++ src/command.rs @@ -9,16 +9,16 @@ static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap(); static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap(); } pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> { - core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.", Some(sender))?; + core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.", Some(sender), None)?; Ok(()) } pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> { - core.send(core.list(sender).await?.join("\n"), Some(sender))?; + core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2))?; Ok(()) } pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> { core.send( match &command[1].parse::() { @@ -32,11 +32,11 @@ "/delete" => core.delete(&number, sender).await?, "/disable" => core.disable(&number, sender).await? .to_string(), _ => bail!("Command {} not handled.", &command[0]), }, - }, Some(sender))?; + }, Some(sender), None)?; Ok(()) } pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> { let mut source_id: Option = None; @@ -56,21 +56,21 @@ i_command.next().context(at_least)?, i_command.next().context(at_least)?, i_command.next(), i_command.next()); if ! RE_USERNAME.is_match(&channel) { - core.send(format!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel), Some(sender))?; + core.send(format!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel), Some(sender), None)?; return Ok(()) } if ! RE_LINK.is_match(&url) { - core.send(format!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url), Some(sender))?; + core.send(format!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url), Some(sender), None)?; return Ok(()) } let iv_hash = match iv_hash { Some(hash) => { if ! RE_IV_HASH.is_match(hash) { - core.send(format!("IV hash should be 14 hex digits.\nNot {:?}", hash), Some(sender))?; + core.send(format!("IV hash should be 14 hex digits.\nNot {:?}", hash), Some(sender), None)?; return Ok(()) }; Some(*hash) } None => None, @@ -90,7 +90,8 @@ user = true; }; }; if ! me { bail!("I need to be admin on that channel\\."); }; if ! user { bail!("You should be admin on that channel\\."); }; - core.send(core.update(source_id, channel, channel_id, url, iv_hash, None, sender).await?, Some(sender)) + core.send(core.update(source_id, channel, channel_id, url, iv_hash, None, sender).await?, Some(sender), None)?; + Ok(()) } Index: src/core.rs ================================================================== --- src/core.rs +++ src/core.rs @@ -48,11 +48,11 @@ sources: Arc::new(Mutex::new(HashSet::new())), }; let clone = core.clone(); tokio::spawn(async move { if let Err(err) = &clone.autofetch().await { - if let Err(err) = clone.send(&format!("🛑 {:?}", err), None) { + if let Err(err) = clone.send(&format!("🛑 {:?}", err), None, None) { eprintln!("Autofetch error: {}", err); }; } }); Ok(core) @@ -60,17 +60,21 @@ pub fn stream(&self) -> telegram_bot::UpdatesStream { self.tg.stream() } - pub fn send(&self, msg: S, target: Option) -> Result<()> + pub fn send(&self, msg: S, target: Option, parse_mode: Option) -> Result<()> where S: Into { let msg: String = msg.into(); + let parse_mode = match parse_mode { + Some(mode) => mode, + None => telegram_bot::types::ParseMode::Html, + }; self.tg.spawn(telegram_bot::SendMessage::new(match target { Some(user) => user, None => self.owner_chat, - }, msg.to_owned())); + }, msg.to_owned()).parse_mode(parse_mode)); Ok(()) } pub async fn check(&self, id: &i32, owner: S, real: bool) -> Result where S: Into { @@ -320,11 +324,11 @@ owner_chat: telegram_bot::UserId::new(owner), ..self.clone() }; tokio::spawn(async move { if let Err(err) = clone.check(&source_id, owner, true).await { - if let Err(err) = clone.send(&format!("🛑 {:?}", err), None) { + if let Err(err) = clone.send(&format!("🛑 {:?}", err), None, None) { eprintln!("Check error: {}", err); }; }; }); } else { @@ -337,11 +341,11 @@ tokio::time::sleep(delay.to_std()?).await; delay = chrono::Duration::minutes(1); } } - pub async fn list(&self, owner: S) -> Result> + pub async fn list(&self, owner: S) -> Result where S: Into { let owner = owner.into(); let mut reply = vec![]; let mut conn = self.pool.acquire().await .with_context(|| format!("List fetch conn:\n{:?}", &self.pool))?; @@ -362,8 +366,8 @@ }, url)); if let Some(hash) = iv_hash { reply.push(format!("IV `{}`", hash)); } }; - Ok(reply) + Ok(reply.join("\n")) } } Index: src/main.rs ================================================================== --- src/main.rs +++ src/main.rs @@ -2,12 +2,11 @@ mod core; use config; use futures::StreamExt; use tokio; - -use telegram_bot::*; +use telegram_bot; #[macro_use] extern crate lazy_static; use anyhow::Result; @@ -18,35 +17,35 @@ settings.merge(config::File::with_name("rsstg"))?; let core = core::Core::new(settings).await?; let mut stream = core.stream(); - stream.allowed_updates(&[AllowedUpdate::Message]); - let mut reply_to: Option; + stream.allowed_updates(&[telegram_bot::AllowedUpdate::Message]); + let mut reply_to: Option; loop { reply_to = None; match stream.next().await { Some(update) => { if let Err(err) = handle(update?, &core, &mut reply_to).await { - core.send(&format!("🛑 {:?}", err), reply_to)?; + core.send(&format!("🛑 {:?}", err), reply_to, None)?; }; }, None => { - core.send(&format!("🛑 None error."), None)?; + core.send(&format!("🛑 None error."), None, None)?; } }; } //Ok(()) } -async fn handle(update: telegram_bot::Update, core: &core::Core, mut _reply_to: &Option) -> Result<()> { +async fn handle(update: telegram_bot::Update, core: &core::Core, mut _reply_to: &Option) -> Result<()> { match update.kind { - UpdateKind::Message(message) => { + telegram_bot::UpdateKind::Message(message) => { match message.kind { - MessageKind::Text { ref data, .. } => { + telegram_bot::MessageKind::Text { ref data, .. } => { let sender = message.from.id; let words: Vec<&str> = data.split_whitespace().collect(); match words[0] { "/check" | "/clean" | "/enable" | "/delete" | "/disable" => command::command(core, sender, words).await?, "/start" => command::start(core, sender).await?,