initial commit

This commit is contained in:
Aleksandr Trushkin
2020-11-13 19:08:34 +03:00
commit 350d0dbceb
4 changed files with 1593 additions and 0 deletions

78
src/main.rs Normal file
View File

@ -0,0 +1,78 @@
use std::env;
use teloxide::{prelude::*, utils::command::BotCommand};
use log::{debug, info, warn};
use env_logger;
#[tokio::main]
async fn main() {
env_logger::init();
// 170515067:AAElDJ8Sq_oIqo9WaL4DKvUr13nSEIdHCYs
let token = match env::var("ALTHEREGO_TOKEN") {
Ok(token) => token,
Err(_) => "170515067:AAElDJ8Sq_oIqo9WaL4DKvUr13nSEIdHCYs".to_string(),
};
let bot = teloxide::Bot::builder().token(token).build();
let bot_name = "AltherEgo";
teloxide::commands_repl(bot, bot_name, handler).await;
}
#[derive(serde::Deserialize, Debug)]
struct Climate {
humidity: f32,
temperature: f32,
}
const CLIMATE_REQ_URL: &str = "http://127.0.0.1:18081/v1/home/temperature";
async fn handler(cx: UpdateWithCx<Message>, command: Command) -> ResponseResult<()> {
let request_id = uuid::Uuid::new_v4();
info!("incoming request xreqid={} command={:?}", request_id, command);
match command {
Command::Help => cx.answer(Command::descriptions()).send().await?,
Command::RoomTemperature => {
let response = match reqwest::get(CLIMATE_REQ_URL).await {
Ok(response) => response,
Err(err) => {
warn!("unable to handle request xreqid={} error={:?}", request_id, err);
cx.answer_str(format!("something went wrong, reference to {}", request_id)).await?;
return Err(RequestError::NetworkError(err));
}
};
let info: Climate = match response.json::<Climate>().await {
Ok(result) => result,
Err(err) => {
warn!("unable to handle request xreqid={} error={:?}", request_id, err);
cx.answer_str(format!("something went wrong, reference to {}", request_id)).await?;
return Err(RequestError::NetworkError(err));
},
};
debug!("parsed value: {:?}", info);
cx.answer_str(format!(
"your temperature is {:.2} and humidity is {:.2}",
info.temperature, info.humidity
))
.await?
}
};
Ok(())
}
#[derive(BotCommand, Debug)]
#[command(rename = "lowercase", description = "These commands are supported:")]
enum Command {
#[command(description = "display this text.")]
Help,
#[command(description = "temperature of your room.")]
RoomTemperature,
}