rust_tg_bot_ext/handlers/
pre_checkout_query.rs1use std::future::Future;
7use std::pin::Pin;
8use std::sync::Arc;
9
10use regex::Regex;
11use rust_tg_bot_raw::types::update::Update;
12
13use super::base::{Handler, HandlerCallback, HandlerResult, MatchResult};
14
15pub struct PreCheckoutQueryHandler {
20 callback: HandlerCallback,
21 pattern: Option<Regex>,
22 block: bool,
23}
24
25impl PreCheckoutQueryHandler {
26 pub fn new(callback: HandlerCallback, pattern: Option<Regex>, block: bool) -> Self {
28 Self {
29 callback,
30 pattern,
31 block,
32 }
33 }
34}
35
36impl Handler for PreCheckoutQueryHandler {
37 fn check_update(&self, update: &Update) -> Option<MatchResult> {
38 let pcq = update.pre_checkout_query()?;
39
40 if let Some(ref re) = self.pattern {
41 let payload = &pcq.invoice_payload;
42 if re.is_match(payload) {
43 Some(MatchResult::Empty)
44 } else {
45 None
46 }
47 } else {
48 Some(MatchResult::Empty)
49 }
50 }
51
52 fn handle_update(
53 &self,
54 update: Arc<Update>,
55 match_result: MatchResult,
56 ) -> Pin<Box<dyn Future<Output = HandlerResult> + Send>> {
57 (self.callback)(update, match_result)
58 }
59
60 fn block(&self) -> bool {
61 self.block
62 }
63}