teloxide/repls/commands_repl.rs
1use std::fmt::Debug;
2
3use dptree::di::Injectable;
4use futures::future::BoxFuture;
5
6use crate::{
7 dispatching::{HandlerExt, UpdateFilterExt},
8 error_handlers::LoggingErrorHandler,
9 requests::{Requester, ResponseResult},
10 types::Update,
11 update_listeners::{self, UpdateListener},
12 utils::command::BotCommands,
13};
14
15/// A [REPL] for commands.
16///
17/// REPLs are meant only for simple bots and rapid prototyping. If you need to
18/// supply dependencies or describe more complex dispatch logic, please use
19/// [`Dispatcher`]. See also: ["Dispatching or
20/// REPLs?"](../index.html#dispatching-or-repls).
21///
22/// [`Dispatcher`]: crate::dispatching::Dispatcher
23///
24/// All errors from the handler and update listener will be logged.
25///
26/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
27///
28/// This trait extends your [`BotCommands`] type with REPL facilities.
29///
30/// ## Signatures
31///
32/// Don't be scared by many trait bounds in the signatures, in essence they
33/// require:
34///
35/// 1. `bot` is a bot, client for the Telegram bot API. It is represented via
36/// the [`Requester`] trait.
37/// 2. `handler` is an `async` function that takes arguments from
38/// [`DependencyMap`] (see below) and returns [`ResponseResult`].
39/// 3. `listener` is something that takes updates from a Telegram server and
40/// implements [`UpdateListener`].
41///
42/// All the other requirements are about thread safety and data validity and can
43/// be ignored for most of the time.
44///
45/// ## Handler arguments
46///
47/// `teloxide` provides the following types to the `handler`:
48/// - [`Message`]
49/// - `R` (type of the `bot`)
50/// - `Cmd` (type of the parsed command)
51/// - [`BotFullInfo`]
52///
53/// Each of these types can be accepted as a handler parameter. Note that they
54/// aren't all required at the same time: e.g., you can take only the bot and
55/// the command without [`BotFullInfo`] and [`Message`].
56///
57/// [`BotFullInfo`]: crate::types::BotFullInfo
58/// [`Message`]: crate::types::Message
59/// [`DependencyMap`]: dptree::di::DependencyMap
60///
61/// ## Stopping
62//
63#[doc = include_str!("stopping.md")]
64///
65/// ## Caution
66//
67#[doc = include_str!("caution.md")]
68///
69#[cfg(feature = "ctrlc_handler")]
70pub trait CommandReplExt {
71 /// A REPL for commands.
72 ///
73 /// See [`CommandReplExt`] for more details.
74 fn repl<'a, R, H, Args>(bot: R, handler: H) -> BoxFuture<'a, ()>
75 where
76 R: Requester + Clone + Send + Sync + 'static,
77 <R as Requester>::GetUpdates: Send,
78 <R as Requester>::GetWebhookInfo: Send,
79 <R as Requester>::GetMe: Send,
80 <R as Requester>::DeleteWebhook: Send,
81 H: Injectable<ResponseResult<()>, Args> + Send + Sync + 'static;
82
83 /// A REPL for commands with a custom [`UpdateListener`].
84 ///
85 /// See [`CommandReplExt`] for more details.
86 fn repl_with_listener<'a, R, H, L, Args>(bot: R, handler: H, listener: L) -> BoxFuture<'a, ()>
87 where
88 H: Injectable<ResponseResult<()>, Args> + Send + Sync + 'static,
89 L: UpdateListener + Send + 'a,
90 L::Err: Debug + Send + 'a,
91 R: Requester + Clone + Send + Sync + 'static,
92 <R as Requester>::GetMe: Send;
93}
94
95#[cfg(feature = "ctrlc_handler")]
96impl<Cmd> CommandReplExt for Cmd
97where
98 Cmd: BotCommands + Send + Sync + 'static,
99{
100 fn repl<'a, R, H, Args>(bot: R, handler: H) -> BoxFuture<'a, ()>
101 where
102 R: Requester + Clone + Send + Sync + 'static,
103 <R as Requester>::GetUpdates: Send,
104 <R as Requester>::GetWebhookInfo: Send,
105 <R as Requester>::GetMe: Send,
106 <R as Requester>::DeleteWebhook: Send,
107 H: Injectable<ResponseResult<()>, Args> + Send + Sync + 'static,
108 {
109 let cloned_bot = bot.clone();
110
111 Box::pin(async move {
112 Self::repl_with_listener(
113 bot,
114 handler,
115 update_listeners::polling_default(cloned_bot).await,
116 )
117 .await
118 })
119 }
120
121 fn repl_with_listener<'a, R, H, L, Args>(bot: R, handler: H, listener: L) -> BoxFuture<'a, ()>
122 where
123 H: Injectable<ResponseResult<()>, Args> + Send + Sync + 'static,
124 L: UpdateListener + Send + 'a,
125 L::Err: Debug + Send + 'a,
126 R: Requester + Clone + Send + Sync + 'static,
127 <R as Requester>::GetMe: Send,
128 {
129 use crate::dispatching::Dispatcher;
130
131 // Other update types are of no interest to use since this REPL is only for
132 // commands. See <https://github.com/teloxide/teloxide/issues/557>.
133 let ignore_update = |_upd| Box::pin(async {});
134
135 Box::pin(async move {
136 Dispatcher::builder(
137 bot,
138 Update::filter_message().filter_command::<Cmd>().endpoint(handler),
139 )
140 .default_handler(ignore_update)
141 .enable_ctrlc_handler()
142 .build()
143 .dispatch_with_listener(
144 listener,
145 LoggingErrorHandler::with_custom_text("An error from the update listener"),
146 )
147 .await
148 })
149 }
150}