Skip to main content

teloxide/repls/
repl.rs

1use crate::{
2    dispatching::UpdateFilterExt,
3    error_handlers::LoggingErrorHandler,
4    requests::{Requester, ResponseResult},
5    types::Update,
6    update_listeners::{self, UpdateListener},
7};
8use dptree::di::Injectable;
9use std::fmt::Debug;
10
11/// A [REPL] for messages.
12//
13///
14//
15#[doc = include_str!("preamble.md")]
16///
17/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
18///
19/// ## Signature
20///
21/// Don't be scared by many trait bounds in the signature, in essence they
22/// require:
23///
24/// 1. `bot` is a bot, client for the Telegram bot API. It is represented via
25///    the [`Requester`] trait.
26/// 2. `handler` is an `async` function that takes arguments from
27///    [`DependencyMap`] (see below) and returns [`ResponseResult`].
28///
29/// ## Handler arguments
30///
31/// `teloxide` provides the following types to the `handler`:
32/// - [`Message`]
33/// - `R` (type of the `bot`)
34/// - [`BotFullInfo`]
35///
36/// Each of these types can be accepted as a handler parameter. Note that they
37/// aren't all required at the same time: e.g., you can take only the bot and
38/// the message without [`BotFullInfo`].
39///
40/// [`BotFullInfo`]: crate::types::BotFullInfo
41/// [`Message`]: crate::types::Message
42/// [`DependencyMap`]: dptree::di::DependencyMap
43///
44/// ## Stopping
45//
46#[doc = include_str!("stopping.md")]
47///
48/// ## Caution
49//
50#[doc = include_str!("caution.md")]
51///
52#[cfg(feature = "ctrlc_handler")]
53pub async fn repl<R, H, Args>(bot: R, handler: H)
54where
55    R: Requester + Send + Sync + Clone + 'static,
56    <R as Requester>::GetUpdates: Send,
57    H: Injectable<ResponseResult<()>, Args> + Send + Sync + 'static,
58{
59    let cloned_bot = bot.clone();
60    repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot).await).await;
61}
62
63/// A [REPL] for messages, with a custom [`UpdateListener`].
64//
65///
66//
67#[doc = include_str!("preamble.md")]
68///
69/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
70/// [`UpdateListener`]: crate::update_listeners::UpdateListener
71///
72/// ## Signature
73///
74/// Don't be scared by many trait bounds in the signature, in essence they
75/// require:
76///
77/// 1. `bot` is a bot, client for the Telegram bot API. It is represented via
78///    the [`Requester`] trait.
79/// 2. `handler` is an `async` function that takes arguments from
80///    [`DependencyMap`] (see below) and returns [`ResponseResult`].
81/// 3. `listener` is something that takes updates from a Telegram server and
82///    implements [`UpdateListener`].
83///
84/// ## Handler arguments
85///
86/// `teloxide` provides the following types to the `handler`:
87/// - [`Message`]
88/// - `R` (type of the `bot`)
89/// - [`BotFullInfo`]
90///
91/// Each of these types can be accepted as a handler parameter. Note that they
92/// aren't all required at the same time: e.g., you can take only the bot and
93/// the message without [`BotFullInfo`].
94///
95/// [`BotFullInfo`]: crate::types::BotFullInfo
96/// [`Message`]: crate::types::Message
97/// [`DependencyMap`]: dptree::di::DependencyMap
98///
99/// ## Stopping
100//
101#[doc = include_str!("stopping.md")]
102///
103/// ## Caution
104//
105#[doc = include_str!("caution.md")]
106///
107#[cfg(feature = "ctrlc_handler")]
108pub async fn repl_with_listener<R, H, L, Args>(bot: R, handler: H, listener: L)
109where
110    R: Requester + Clone + Send + Sync + 'static,
111    H: Injectable<ResponseResult<()>, Args> + Send + Sync + 'static,
112    L: UpdateListener + Send,
113    L::Err: Debug,
114{
115    use crate::dispatching::Dispatcher;
116
117    // Other update types are of no interest to use since this REPL is only for
118    // messages. See <https://github.com/teloxide/teloxide/issues/557>.
119    let ignore_update = |_upd| Box::pin(async {});
120
121    Dispatcher::builder(bot, Update::filter_message().endpoint(handler))
122        .default_handler(ignore_update)
123        .enable_ctrlc_handler()
124        .build()
125        .dispatch_with_listener(
126            listener,
127            LoggingErrorHandler::with_custom_text("An error from the update listener"),
128        )
129        .await;
130}
131
132#[test]
133fn repl_is_send() {
134    let bot = crate::Bot::new("");
135    let repl = crate::repl(bot, || async { Ok(()) });
136    assert_send(&repl);
137
138    fn assert_send(_: &impl Send) {}
139}