Merge pull request 'feature/chat-id-cmd' (#2) from feature/chat-id-cmd into master
Reviewed-on: https://git.loyso.art/frx/altherego/pulls/2
This commit is contained in:
@ -3,11 +3,12 @@ name = "altherego"
|
|||||||
version = "0.9.9"
|
version = "0.9.9"
|
||||||
authors = ["Aleksandr Trushkin <atrushkin@outlook.com>"]
|
authors = ["Aleksandr Trushkin <atrushkin@outlook.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
default-run = "altherego"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
teloxide = { version = "0.9.0", features = ["macros", "auto-send"] }
|
teloxide = { version = "0.12.2", features = ["macros", "auto-send"] }
|
||||||
tokio = {version = "1.8", features = ["full"]}
|
tokio = {version = "1.8", features = ["full"]}
|
||||||
uuid = { version = "0.8.1", features = ["v4"] }
|
uuid = { version = "0.8.1", features = ["v4"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|||||||
34
src/main.rs
34
src/main.rs
@ -57,7 +57,7 @@ async fn run() -> anyhow::Result<()> {
|
|||||||
info!("starting");
|
info!("starting");
|
||||||
|
|
||||||
let settings = Settings::init_from_env().expect("reading config values");
|
let settings = Settings::init_from_env().expect("reading config values");
|
||||||
let bot = teloxide::Bot::new(&settings.telegram_token).auto_send();
|
let bot = teloxide::Bot::new(&settings.telegram_token);
|
||||||
let repo_config = repo::SqliteConfig {
|
let repo_config = repo::SqliteConfig {
|
||||||
source: settings.db_source,
|
source: settings.db_source,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -108,6 +108,7 @@ async fn run() -> anyhow::Result<()> {
|
|||||||
.branch(dptree::case![Command::RoomTemperature].endpoint(handle_temperature_sensor))
|
.branch(dptree::case![Command::RoomTemperature].endpoint(handle_temperature_sensor))
|
||||||
.branch(dptree::case![Command::HostTemperature].endpoint(handle_host_temperature))
|
.branch(dptree::case![Command::HostTemperature].endpoint(handle_host_temperature))
|
||||||
.branch(dptree::case![Command::VersionRequest].endpoint(handle_version))
|
.branch(dptree::case![Command::VersionRequest].endpoint(handle_version))
|
||||||
|
.branch(dptree::case![Command::ChatID].endpoint(handle_chat_id))
|
||||||
.branch(dptree::case![Command::Help].endpoint(handle_help));
|
.branch(dptree::case![Command::Help].endpoint(handle_help));
|
||||||
|
|
||||||
let mut dependencies = DependencyMap::new();
|
let mut dependencies = DependencyMap::new();
|
||||||
@ -120,11 +121,11 @@ async fn run() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
.dependencies(dependencies)
|
.dependencies(dependencies)
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.default_handler(|upd| async move {
|
.default_handler(|upd| async move {
|
||||||
warn!("unhandled update: {:?}", upd);
|
warn!("unhandled update: {:?}", upd);
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@ -138,7 +139,7 @@ fn error_msg(reqid: &utils::RequestID) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_temperature_sensor(
|
async fn handle_temperature_sensor(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
climate: climate::Client,
|
climate: climate::Client,
|
||||||
next_req_id: utils::Generators,
|
next_req_id: utils::Generators,
|
||||||
@ -172,7 +173,7 @@ async fn handle_temperature_sensor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_host_temperature(
|
async fn handle_host_temperature(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
temp: SelfTemperature,
|
temp: SelfTemperature,
|
||||||
next_req_id: utils::Generators,
|
next_req_id: utils::Generators,
|
||||||
@ -199,7 +200,7 @@ async fn handle_host_temperature(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_version(bot: AutoSend<Bot>, msg: Message) -> Result<()> {
|
async fn handle_version(bot: Bot, msg: Message) -> Result<()> {
|
||||||
let chat_id = msg.chat.id;
|
let chat_id = msg.chat.id;
|
||||||
let text = format!("Bot version is {} (branch: {})", VERSION, BRANCH,);
|
let text = format!("Bot version is {} (branch: {})", VERSION, BRANCH,);
|
||||||
|
|
||||||
@ -208,7 +209,16 @@ async fn handle_version(bot: AutoSend<Bot>, msg: Message) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_help(bot: AutoSend<Bot>, msg: Message) -> Result<()> {
|
async fn handle_chat_id(bot: Bot, msg: Message) -> Result<()> {
|
||||||
|
let chat_id = msg.chat.id;
|
||||||
|
let text = format!("Current chat id: {chat_id}");
|
||||||
|
|
||||||
|
bot.send_message(chat_id, text).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_help(bot: Bot, msg: Message) -> Result<()> {
|
||||||
let chat_id = msg.chat.id;
|
let chat_id = msg.chat.id;
|
||||||
|
|
||||||
bot.send_message(chat_id, Command::descriptions().to_string())
|
bot.send_message(chat_id, Command::descriptions().to_string())
|
||||||
@ -251,14 +261,16 @@ struct Climate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(BotCommands, Debug, Clone, PartialEq, Eq)]
|
#[derive(BotCommands, Debug, Clone, PartialEq, Eq)]
|
||||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
#[command(description = "These commands are supported:")]
|
||||||
enum Command {
|
enum Command {
|
||||||
#[command(description = "display this text.")]
|
#[command(rename="help", description = "display this text.")]
|
||||||
Help,
|
Help,
|
||||||
#[command(description = "temperature of your room.")]
|
#[command(rename="roomtemp", description = "temperature of your room.")]
|
||||||
RoomTemperature,
|
RoomTemperature,
|
||||||
#[command(description = "temperature of raspberry.")]
|
#[command(rename="hosttemp", description = "temperature of raspberry.")]
|
||||||
HostTemperature,
|
HostTemperature,
|
||||||
#[command(description = "prints current version.")]
|
#[command(rename="version", description = "prints current version.")]
|
||||||
VersionRequest,
|
VersionRequest,
|
||||||
|
#[command(rename="chatid", description = "prints current chat id.")]
|
||||||
|
ChatID,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ use sqlx::{
|
|||||||
migrate,
|
migrate,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Envconfig, Clone, Debug)]
|
#[derive(Envconfig)]
|
||||||
struct Settings {
|
struct Settings {
|
||||||
#[envconfig(from = "ALTEREGO_DATABASE_URL", default = "./db.sqlite")]
|
#[envconfig(from = "ALTEREGO_DATABASE_URL", default = "./db.sqlite")]
|
||||||
pub db_source: String,
|
pub db_source: String,
|
||||||
@ -30,6 +30,8 @@ async fn run() -> Result<()> {
|
|||||||
|
|
||||||
let settings = Settings::init_from_env().expect("reading config values");
|
let settings = Settings::init_from_env().expect("reading config values");
|
||||||
|
|
||||||
|
info!("opening database {}", settings.db_source);
|
||||||
|
|
||||||
let opts = SqliteConnectOptions::from_str(&settings.db_source)?
|
let opts = SqliteConnectOptions::from_str(&settings.db_source)?
|
||||||
.create_if_missing(true)
|
.create_if_missing(true)
|
||||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
|
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
|
||||||
|
|||||||
Reference in New Issue
Block a user