From 07231c9d28c77693b8bd69cd5bc838981d1e5c85 Mon Sep 17 00:00:00 2001 From: Cristian Colosimo Date: Tue, 7 Nov 2023 16:45:58 +0100 Subject: [PATCH] Added separation between groups and users for send presence status --- .dockerignore | 3 ++- Dockerfile | 6 +++++- dummy.rs | 1 + src/main.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 dummy.rs diff --git a/.dockerignore b/.dockerignore index 86ef35a..a66fe3a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,4 +7,5 @@ run.sh.template .env .env.template Dockerfile -docker-compose.yml \ No newline at end of file +docker-compose.yml +*.db \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 265900c..cdff093 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,10 +4,14 @@ RUN rustup target add x86_64-unknown-linux-musl && apt-get update && apt-get in WORKDIR /app +COPY dummy.rs src/main.rs +COPY Cargo.toml . +RUN cargo build --release --target x86_64-unknown-linux-musl COPY . . - RUN cargo build --release --target x86_64-unknown-linux-musl + + FROM alpine:latest RUN apk add --no-cache tzdata ENV TZ=Europe/Rome diff --git a/dummy.rs b/dummy.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/dummy.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/src/main.rs b/src/main.rs index ab260cb..5e0d271 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,9 +8,11 @@ type DbPool = SqlitePool; #[derive(Debug, Deserialize, sqlx::FromRow)] struct User { id: i32, - telegram_id: i32, + telegram_id: i64, } + + impl User { async fn insert_user_db( telegram_user_id: i32, @@ -34,6 +36,21 @@ impl User { } } + +#[derive(Debug, Deserialize, sqlx::FromRow)] +struct Group { + id: i32, + telegram_id: i64, + disabled: bool, +} +impl Group { + async fn select_all_enabled(pool: &Pool) -> Result, sqlx::Error> { + return sqlx::query_as::("SELECT * FROM groups WHERE disabled = 0") + .fetch_all(pool) + .await; + } +} + #[derive(Debug, Deserialize, sqlx::FromRow)] struct Log { id: i32, @@ -91,6 +108,12 @@ async fn get_db() -> Pool { telegram_id INTEGER, UNIQUE(telegram_id) ); + CREATE TABLE IF NOT EXISTS groups ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + telegram_id INTEGER, + disabled bool DEFAULT 0, + UNIQUE(telegram_id) + ); ", ) .execute(&pool) @@ -202,7 +225,8 @@ async fn fetching_state_loop(pool: &Pool) { let message: String = generate_response(current_state_lab_id, history_user_name); let users = User::select_all_user_db(pool).await.unwrap(); - let bot = Bot::from_env(); + let bot: Bot = Bot::from_env(); + for user in users.iter() { log::info!("Sending message to user: {:?}", user); @@ -217,6 +241,20 @@ async fn fetching_state_loop(pool: &Pool) { } } } + let groups = Group::select_all_enabled(pool).await.unwrap(); + for group in groups.iter() { + log::info!("Sending message to group: {:?}", group); + + let group_id = group.telegram_id as i64; + match bot.send_message(ChatId(group_id), &message).send().await { + Ok(_) => { + log::info!("Message sent to group: {:?}", group); + } + Err(err) => { + log::error!("Error sending message to group: {:?} , {:?}", group, err); + } + } + } } Err(_) => { log::info!("Current state: no last state, inserting genesis status in DB"); @@ -250,6 +288,7 @@ async fn main() { fetching_state_loop(&pool_loop).await; } }); + //Loop telegram bot teloxide::repl(bot, move |bot: Bot, msg: Message| { loop_telegram(bot, msg, pool_telegram.clone()) @@ -274,11 +313,14 @@ async fn loop_telegram(bot: Bot, msg: Message, db: Pool) -> Re Ok(()) => "Ciao, sono il bot HLCS 🦀. Ti avviserò quando il laboratorio sarà aperto o chiuso", Err(()) => "Ciao, sono il bot HLCS 🦀. Ti avviserò quando il laboratorio sarà aperto o chiuso, ti avverto che eri già iscritto" }; - bot.send_message(msg.chat.id, messaggio_benvenuto).await?; + if msg.chat.is_private() { + bot.send_message(msg.chat.id, messaggio_benvenuto).await?; - if message_status.is_some() { - bot.send_message(msg.chat.id, message_status.unwrap()) - .await?; + if message_status.is_some() { + bot.send_message(msg.chat.id, message_status.unwrap()) + .await?; + } } + Ok(()) }