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
47
48
49
50
use crate::{
    methods::AnswerCallbackQuery,
    types::{callback, parameters::CallbackAction},
    Bot,
};

/// Provides methods appliable to callback queries.
pub trait Callback<'a, C: 'static>: crate::internal::Sealed {
    #[doc(hidden)]
    fn bot(&self) -> &Bot<C>;
    #[doc(hidden)]
    fn id(&self) -> callback::query::id::Ref<'_>;

    /// 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`]: #method.ignore
    /// [`open_url`]: #method.open_url
    /// [`notify`]: #method.notify
    /// [`alert`]: #method.alert
    fn answer(
        &'a self,
        action: CallbackAction<'a>,
    ) -> AnswerCallbackQuery<'a, C> {
        self.bot().answer_callback_query(self.id(), action)
    }

    /// Answers the query without any action.
    fn ignore(&'a self) -> AnswerCallbackQuery<'a, C> {
        self.answer(CallbackAction::none())
    }

    /// Opens a URL.
    fn open_url(&'a self, url: &'a str) -> AnswerCallbackQuery<'a, C> {
        self.answer(CallbackAction::url(url))
    }

    /// Shows a notification to the user.
    fn notify(&'a self, text: &'a str) -> AnswerCallbackQuery<'a, C> {
        self.answer(CallbackAction::notification(text))
    }

    /// Shows an alert to the user.
    fn alert(&'a self, text: &'a str) -> AnswerCallbackQuery<'a, C> {
        self.answer(CallbackAction::alert(text))
    }
}