Skip to main content

rust_tg_bot_ext/handlers/
poll.rs

1//! [`PollHandler`] -- handles updates that contain a poll.
2//!
3//! Ported from `python-telegram-bot`'s `PollHandler`. This is the simplest
4//! handler: it matches any update that has `Update.poll` set.
5
6use std::future::Future;
7use std::pin::Pin;
8use std::sync::Arc;
9
10use rust_tg_bot_raw::types::update::Update;
11
12use super::base::{Handler, HandlerCallback, HandlerResult, MatchResult};
13
14/// Handler for `Update.poll`.
15///
16/// Bots receive poll updates only for polls they sent or stopped polls.
17pub struct PollHandler {
18    callback: HandlerCallback,
19    block: bool,
20}
21
22impl PollHandler {
23    /// Create a new `PollHandler`.
24    pub fn new(callback: HandlerCallback, block: bool) -> Self {
25        Self { callback, block }
26    }
27}
28
29impl Handler for PollHandler {
30    fn check_update(&self, update: &Update) -> Option<MatchResult> {
31        if update.poll().is_some() {
32            Some(MatchResult::Empty)
33        } else {
34            None
35        }
36    }
37
38    fn handle_update(
39        &self,
40        update: Arc<Update>,
41        match_result: MatchResult,
42    ) -> Pin<Box<dyn Future<Output = HandlerResult> + Send>> {
43        (self.callback)(update, match_result)
44    }
45
46    fn block(&self) -> bool {
47        self.block
48    }
49}