diff --git a/src/commands.rs b/src/commands.rs index 65fca31..09487b6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -5,8 +5,7 @@ use crate::process; pub async fn start(api: &Api, message: &Message) -> Result<(), Error> { api.send(message.text_reply(format!( "Olá, {}! Esse bot irá lhe ajudar a acompanhar o estado do seu processo.\n\ - Para começarmos, preciso que me envie o código do processo.\n\ - Simplesmente digite /code CODIGO que comecarei a acompanhar.", + Para começarmos, preciso que me envie o código do processo.", message.from.first_name ))) .await?; @@ -14,30 +13,33 @@ pub async fn start(api: &Api, message: &Message) -> Result<(), Error> { } pub async fn invalid(api: &Api, message: &Message) -> Result<(), Error> { - api.send(message.text_reply( - "Command not found!\n\ - Use /help to list the available commands", - )) - .await?; + api.send(message.text_reply("Comando ou codigo inválido")) + .await?; + + start(&api, &message).await?; Ok(()) } -pub async fn code(api: &Api, message: &Message, data: &str) -> Result<(), Error> { - let code: String = data.split_whitespace().skip(1).take(1).collect(); - - let reply = if code.is_empty() { - String::from("Você me precisa me enviar algum código") - } else { - let process = process::start(message.from.id.to_string(), code) - .await - .unwrap(); - format!( - "Status: {}\n\ - Mensagem: {}", - process.status, process.info - ) - }; +pub async fn process_unformatted_code( + api: &Api, + message: &Message, + code: &str, +) -> Result<(), Error> { + let code = format!("{}-{}-{}", &code[0..4], &code[4..8], &code[8..12]); + println!("{}", code); + process_code(&api, &message, &code).await +} + +pub async fn process_code(api: &Api, message: &Message, code: &str) -> Result<(), Error> { + let process = process::start(message.from.id.to_string(), code) + .await + .unwrap(); + let reply = format!( + "Status: {}\n\ + Mensagem: {}", + process.status, process.info + ); api.send(message.text_reply(reply)).await?; diff --git a/src/main.rs b/src/main.rs index 3fee7e4..e79ef89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use std::env; #[macro_use] extern crate diesel_migrations; use futures::StreamExt; +use regex::Regex; use telegram_bot::*; mod commands; @@ -27,6 +28,9 @@ async fn main() -> Result<(), Error> { scheduler::start(token.clone()); + let formatted_code_re = Regex::new(r"^\d{4}-\d{4}-\d{4}$").unwrap(); + let unformatted_code_re = Regex::new(r"^\d{4}\d{4}\d{4}$").unwrap(); + // Fetch new updates via long poll method let mut stream = api.stream(); while let Some(update) = stream.next().await { @@ -36,8 +40,11 @@ async fn main() -> Result<(), Error> { if let MessageKind::Text { ref data, .. } = message.kind { match data.as_str() { "/start" => commands::start(&api, &message).await?, - command if command.starts_with("/code") => { - commands::code(&api, &message, data).await? + command if formatted_code_re.is_match(command) => { + commands::process_code(&api, &message, data).await? + } + command if unformatted_code_re.is_match(command) => { + commands::process_unformatted_code(&api, &message, data).await? } _ => commands::invalid(&api, &message).await?, } diff --git a/src/process.rs b/src/process.rs index 4297dd3..57a4889 100644 --- a/src/process.rs +++ b/src/process.rs @@ -17,7 +17,7 @@ pub struct ProcessResponse { pub async fn start( telegram_id: String, - process_code: String, + process_code: &str, ) -> Result> { let p = fetch_for_one(&process_code).await?; if p.status != "unknown" {