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
36
37
38
39
40
41
42
43
44
45
46
use crate::{
    contexts::fields, methods::AnswerCallbackQuery,
    types::parameters::CallbackAction,
};

/// Provides methods appliable to callback queries.
pub trait Callback: fields::Callback {
    /// Answers the callback query.
    ///
    /// If you don't need to choose the action dynamically, using dedicated
    /// methods will be more convenient: [`ignore`], [`open_url`], [`notify`]
    /// and [`alert`].
    ///
    /// [`ignore`]: Self::ignore
    /// [`open_url`]: Self::open_url
    /// [`notify`]: Self::notify
    /// [`alert`]: Self::alert
    fn answer(
        &self,
        action: Option<CallbackAction>,
    ) -> AnswerCallbackQuery<'_> {
        self.bot().answer_callback_query(self.id().clone(), action)
    }

    /// Answers the query without any action.
    fn ignore(&self) -> AnswerCallbackQuery<'_> {
        self.answer(None)
    }

    /// Opens a URL.
    fn open_url(&self, url: impl Into<String>) -> AnswerCallbackQuery<'_> {
        self.answer(Some(CallbackAction::with_url(url)))
    }

    /// Shows a notification to the user.
    fn notify(&self, text: impl Into<String>) -> AnswerCallbackQuery<'_> {
        self.answer(Some(CallbackAction::with_notification(text)))
    }

    /// Shows an alert to the user.
    fn alert(&self, text: impl Into<String>) -> AnswerCallbackQuery<'_> {
        self.answer(Some(CallbackAction::with_alert(text)))
    }
}

impl<T: fields::Callback> Callback for T {}