Skip to content

Commit

Permalink
Added separation between groups and users for send presence status
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiancolosimo committed Nov 7, 2023
1 parent 6ae54a9 commit cd0db1d
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ struct User {
telegram_id: i32,
}



impl User {
async fn insert_user_db(
telegram_user_id: i32,
Expand All @@ -34,6 +36,21 @@ impl User {
}
}


#[derive(Debug, Deserialize, sqlx::FromRow)]
struct Group {
id: i32,
telegram_id: i32,
disabled: bool,
}
impl Group {
async fn select_all_enabled(pool: &Pool<DbConnectionType>) -> Result<Vec<Group>, sqlx::Error> {
return sqlx::query_as::<DbConnectionType, Group>("SELECT * FROM groups WHERE disabled = 0")
.fetch_all(pool)
.await;
}
}

#[derive(Debug, Deserialize, sqlx::FromRow)]
struct Log {
id: i32,
Expand Down Expand Up @@ -91,6 +108,12 @@ async fn get_db() -> Pool<DbConnectionType> {
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)
Expand Down Expand Up @@ -202,7 +225,8 @@ async fn fetching_state_loop(pool: &Pool<DbConnectionType>) {
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);
Expand All @@ -217,6 +241,20 @@ async fn fetching_state_loop(pool: &Pool<DbConnectionType>) {
}
}
}
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");
Expand Down Expand Up @@ -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())
Expand All @@ -274,11 +313,14 @@ async fn loop_telegram(bot: Bot, msg: Message, db: Pool<DbConnectionType>) -> 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(())
}

0 comments on commit cd0db1d

Please sign in to comment.