Skip to content

Commit

Permalink
Merge pull request #9 from hails/feature/remove-code-command
Browse files Browse the repository at this point in the history
improve code parsing workflow
  • Loading branch information
hails authored Apr 4, 2020
2 parents 6dd00dd + 1f6f947 commit 2970065
Showing 3 changed files with 34 additions and 25 deletions.
46 changes: 24 additions & 22 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -5,39 +5,41 @@ 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?;
Ok(())
}

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?;

11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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?,
}
2 changes: 1 addition & 1 deletion src/process.rs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ pub struct ProcessResponse {

pub async fn start(
telegram_id: String,
process_code: String,
process_code: &str,
) -> Result<ProcessResponse, Box<dyn error::Error>> {
let p = fetch_for_one(&process_code).await?;
if p.status != "unknown" {

0 comments on commit 2970065

Please sign in to comment.