support chatid command for printing current chat id

This commit is contained in:
2023-03-05 22:07:01 +03:00
parent 31080b742a
commit 75abdf2412
3 changed files with 26 additions and 9 deletions

View File

@ -7,7 +7,7 @@ edition = "2018"
# 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"

View File

@ -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()
@ -105,9 +105,13 @@ async fn run() -> anyhow::Result<()> {
} }
}, },
)) ))
.chain(dptree::map(|g: utils::Generators| -> utils::RequestID {
g.next_request_id()
}))
.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 +124,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 +142,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 +176,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 +203,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 +212,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,7 +264,7 @@ 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(description = "display this text.")]
Help, Help,
@ -261,4 +274,6 @@ enum Command {
HostTemperature, HostTemperature,
#[command(description = "prints current version.")] #[command(description = "prints current version.")]
VersionRequest, VersionRequest,
#[command(description = "prints current chat id.")]
ChatID,
} }

View File

@ -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);