Skip to main content

rust_tg_bot_ext/handlers/
poll_answer.rs

1//! [`PollAnswerHandler`] -- handles updates that contain a poll answer.
2//!
3//! Ported from `python-telegram-bot`'s `PollAnswerHandler`. Matches any
4//! update with `Update.poll_answer` 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_answer`.
15///
16/// Bots receive poll answer updates only for polls they sent.
17pub struct PollAnswerHandler {
18    callback: HandlerCallback,
19    block: bool,
20}
21
22impl PollAnswerHandler {
23    /// Create a new `PollAnswerHandler`.
24    pub fn new(callback: HandlerCallback, block: bool) -> Self {
25        Self { callback, block }
26    }
27}
28
29impl Handler for PollAnswerHandler {
30    fn check_update(&self, update: &Update) -> Option<MatchResult> {
31        if update.poll_answer().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}