Skip to main content

update_stream/
update_stream.rs

1use futures::StreamExt;
2use teloxide::{prelude::*, types::UpdateKind};
3
4#[tokio::main]
5async fn main() -> std::io::Result<()> {
6    pretty_env_logger::init();
7    let bot = Bot::from_env();
8    let mut updates = bot.update_stream().build().await;
9    let mut ctrl_c = std::pin::pin!(tokio::signal::ctrl_c());
10    let mut stopping = false;
11
12    loop {
13        tokio::select! {
14            item = updates.next() => match item {
15                Some(Ok(update)) => {
16                    if let UpdateKind::Message(message) = update.kind {
17                        if let Some(text) = message.text() {
18                            bot.send_message(text).chat_id(message.chat.id).await.log_on_error().await;
19                        }
20                    }
21                }
22                Some(Err(error)) => log::error!("Could not receive update: {error}"),
23                None => break,
24            },
25            signal = &mut ctrl_c, if !stopping => {
26                signal?;
27                updates.shutdown();
28                stopping = true;
29            }
30        }
31    }
32
33    Ok(())
34}