-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast.rs
35 lines (30 loc) · 1.32 KB
/
broadcast.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use teloxide::Bot;
use teloxide::prelude::{Message, Requester};
use teloxide::types::ChatId;
use tracing::debug;
use crate::bot::{HandlerResult, MyDialogue, State};
use crate::bot::core::db::client::DatabaseClient;
pub(crate) async fn broadcast_start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
bot.send_message(msg.chat.id, "Send me the text to broadcast a message to all users.").await?;
dialogue.update(State::Broadcast).await?;
Ok(())
}
pub(crate) async fn receive_broadcast_message(bot: Bot, dialogue: MyDialogue, msg: Message, db_client: DatabaseClient) -> HandlerResult {
match msg.text().map(ToOwned::to_owned) {
Some(broadcast_message) => {
let reply = format!("Sending broadcast to all users:\n{}", broadcast_message);
bot.send_message(msg.chat.id, reply).await?;
let users = db_client.list_registered_users().await?;
for user in users {
let id = ChatId(user.telegram_id.unwrap());
debug!("Sending broadcast to {}", id);
bot.send_message(id, broadcast_message.clone()).await?;
}
dialogue.update(State::Start).await?;
}
None => {
bot.send_message(msg.chat.id, "Please, send me a proper broadcast message.").await?;
}
}
Ok(())
}