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
[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"]}
uuid = { version = "0.8.1", features = ["v4"] }
log = "0.4"

View File

@ -57,7 +57,7 @@ async fn run() -> anyhow::Result<()> {
info!("starting");
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 {
source: settings.db_source,
..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::HostTemperature].endpoint(handle_host_temperature))
.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));
let mut dependencies = DependencyMap::new();
@ -120,11 +124,11 @@ async fn run() -> anyhow::Result<()> {
Dispatcher::builder(bot, handler)
.dependencies(dependencies)
.enable_ctrlc_handler()
.default_handler(|upd| async move {
warn!("unhandled update: {:?}", upd);
})
.build()
.setup_ctrlc_handler()
.dispatch()
.await;
@ -138,7 +142,7 @@ fn error_msg(reqid: &utils::RequestID) -> String {
}
async fn handle_temperature_sensor(
bot: AutoSend<Bot>,
bot: Bot,
msg: Message,
climate: climate::Client,
next_req_id: utils::Generators,
@ -172,7 +176,7 @@ async fn handle_temperature_sensor(
}
async fn handle_host_temperature(
bot: AutoSend<Bot>,
bot: Bot,
msg: Message,
temp: SelfTemperature,
next_req_id: utils::Generators,
@ -199,7 +203,7 @@ async fn handle_host_temperature(
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 text = format!("Bot version is {} (branch: {})", VERSION, BRANCH,);
@ -208,7 +212,16 @@ async fn handle_version(bot: AutoSend<Bot>, msg: Message) -> Result<()> {
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;
bot.send_message(chat_id, Command::descriptions().to_string())
@ -251,7 +264,7 @@ struct Climate {
}
#[derive(BotCommands, Debug, Clone, PartialEq, Eq)]
#[command(rename = "lowercase", description = "These commands are supported:")]
#[command(description = "These commands are supported:")]
enum Command {
#[command(description = "display this text.")]
Help,
@ -261,4 +274,6 @@ enum Command {
HostTemperature,
#[command(description = "prints current version.")]
VersionRequest,
#[command(description = "prints current chat id.")]
ChatID,
}

View File

@ -9,7 +9,7 @@ use sqlx::{
migrate,
};
#[derive(Envconfig, Clone, Debug)]
#[derive(Envconfig)]
struct Settings {
#[envconfig(from = "ALTEREGO_DATABASE_URL", default = "./db.sqlite")]
pub db_source: String,
@ -30,6 +30,8 @@ async fn run() -> Result<()> {
let settings = Settings::init_from_env().expect("reading config values");
info!("opening database {}", settings.db_source);
let opts = SqliteConnectOptions::from_str(&settings.db_source)?
.create_if_missing(true)
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);