-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use regex::Regex; | ||
use reqwest; | ||
use select::document::Document; | ||
use select::predicate::{Class, Name, Predicate}; | ||
use telegram_bot::*; | ||
|
||
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.", | ||
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?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn code(api: &Api, message: &Message, data: &String) -> 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 = fetch_citizenship_status(&code).await.unwrap(); | ||
format!( | ||
"Status: {}\n\ | ||
Mensagem: {}", | ||
process.status, process.info | ||
) | ||
}; | ||
|
||
api.send(message.text_reply(reply)).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
struct Process { | ||
status: String, | ||
info: String, | ||
} | ||
|
||
async fn fetch_citizenship_status(code: &String) -> Result<Process, reqwest::Error> { | ||
let res = reqwest::Client::new() | ||
.post("https://nacionalidade.justica.gov.pt/Home/GetEstadoProcessoAjax") | ||
.form(&[("SenhaAcesso", code)]) | ||
.send() | ||
.await?; | ||
let body = res.text().await?; | ||
|
||
let document = Document::from(body.as_str()); | ||
|
||
let mut process = Process { | ||
status: String::from("Unknown"), | ||
info: String::from(""), | ||
}; | ||
|
||
if let Some(st) = document.find(Class("active1").descendant(Name("p"))).last() { | ||
process.status = st.text(); | ||
} | ||
|
||
if let Some(st) = document.find(Class("active2").descendant(Name("p"))).last() { | ||
process.status = st.text(); | ||
} | ||
|
||
if let Some(st) = document.find(Class("active3").descendant(Name("p"))).last() { | ||
process.status = st.text(); | ||
} | ||
|
||
if let Some(st) = document.find(Class("container")).nth(5) { | ||
let re = Regex::new(r"\s+").unwrap(); | ||
|
||
process.info = re.replace_all(&st.text(), " ").trim().to_string(); | ||
} | ||
|
||
Ok(process) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use std::env; | ||
|
||
use futures::StreamExt; | ||
use telegram_bot::*; | ||
mod commands; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
let token = env::var("TELEGRAM_BOT_TOKEN").expect("TELEGRAM_BOT_TOKEN not set"); | ||
let api = Api::new(token); | ||
|
||
// Fetch new updates via long poll method | ||
let mut stream = api.stream(); | ||
while let Some(update) = stream.next().await { | ||
// If the received update contains a new message... | ||
let update = update?; | ||
if let UpdateKind::Message(message) = update.kind { | ||
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? | ||
} | ||
_ => commands::invalid(&api, &message).await?, | ||
} | ||
|
||
// Print received text message to stdout. | ||
println!("<{}>: {}", &message.from.first_name, data); | ||
|
||
() | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |