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

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1496
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

18
Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "altherego"
version = "0.1.0"
authors = ["Aleksandr Trushkin <aleksandr.trushkin@rt.ru>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
teloxide = "0.3"
teloxide-macros = "0.3"
tokio = {version = "0.2.23", features = ["full"]}
reqwest = "0.10.8"
serde = "1.0.117"
serde_json = "1.0.59"
uuid = { version = "0.8.1", features = ["v4"] }
log = "0.4"
env_logger = "0.8.1"

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,
}