mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-10 20:12:25 +01:00
requester
=> bot
This commit is contained in:
parent
21eb048ca2
commit
5101786d0b
3 changed files with 25 additions and 35 deletions
|
@ -16,7 +16,7 @@ use tokio::{sync::Notify, time::timeout};
|
|||
|
||||
/// The builder for [`Dispatcher`].
|
||||
pub struct DispatcherBuilder<R, Err> {
|
||||
requester: R,
|
||||
bot: R,
|
||||
dependencies: DependencyMap,
|
||||
handler: UpdateHandler<Err>,
|
||||
default_handler: DefaultHandler,
|
||||
|
@ -28,11 +28,11 @@ where
|
|||
R: Clone + Requester + Clone + Send + Sync + 'static,
|
||||
Err: Debug + Send + Sync + 'static,
|
||||
{
|
||||
/// Constructs a new [`DispatcherBuilder`] with `requester` and `handler`.
|
||||
/// Constructs a new [`DispatcherBuilder`] with `bot` and `handler`.
|
||||
#[must_use]
|
||||
pub fn new(requester: R, handler: UpdateHandler<Err>) -> Self {
|
||||
pub fn new(bot: R, handler: UpdateHandler<Err>) -> Self {
|
||||
Self {
|
||||
requester,
|
||||
bot,
|
||||
dependencies: DependencyMap::new(),
|
||||
handler,
|
||||
default_handler: dptree::endpoint(|update: Update| async move {
|
||||
|
@ -70,8 +70,8 @@ where
|
|||
#[must_use]
|
||||
pub fn build(self) -> Dispatcher<R, Err> {
|
||||
Dispatcher {
|
||||
requester: self.requester.clone(),
|
||||
cache_me_requester: self.requester.cache_me(),
|
||||
bot: self.bot.clone(),
|
||||
cache_me_bot: self.bot.cache_me(),
|
||||
dependencies: self.dependencies,
|
||||
handler: self.handler,
|
||||
default_handler: self.default_handler,
|
||||
|
@ -85,8 +85,8 @@ where
|
|||
|
||||
/// The base for update dispatching.
|
||||
pub struct Dispatcher<R, Err> {
|
||||
requester: R,
|
||||
cache_me_requester: CacheMe<R>,
|
||||
bot: R,
|
||||
cache_me_bot: CacheMe<R>,
|
||||
dependencies: DependencyMap,
|
||||
|
||||
handler: UpdateHandler<Err>,
|
||||
|
@ -121,7 +121,7 @@ where
|
|||
R: Requester + Clone,
|
||||
<R as Requester>::GetUpdatesFaultTolerant: Send,
|
||||
{
|
||||
let listener = update_listeners::polling_default(self.requester.clone()).await;
|
||||
let listener = update_listeners::polling_default(self.bot.clone()).await;
|
||||
let error_handler =
|
||||
LoggingErrorHandler::with_custom_text("An error from the update listener");
|
||||
|
||||
|
@ -204,9 +204,9 @@ where
|
|||
Ok(upd) => {
|
||||
let mut deps = self.dependencies.clone();
|
||||
deps.insert(upd);
|
||||
deps.insert(self.requester.clone());
|
||||
deps.insert(self.bot.clone());
|
||||
deps.insert(
|
||||
self.cache_me_requester.get_me().send().await.expect("Failed to retrieve 'me'"),
|
||||
self.cache_me_bot.get_me().send().await.expect("Failed to retrieve 'me'"),
|
||||
);
|
||||
|
||||
match self.handler.dispatch(deps).await {
|
||||
|
|
|
@ -25,7 +25,7 @@ use teloxide_core::requests::Requester;
|
|||
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
|
||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
pub async fn commands_repl<'a, R, Cmd, H, E, Args>(requester: R, handler: H, cmd: PhantomData<Cmd>)
|
||||
pub async fn commands_repl<'a, R, Cmd, H, E, Args>(bot: R, handler: H, cmd: PhantomData<Cmd>)
|
||||
where
|
||||
Cmd: BotCommand + Send + Sync + 'static,
|
||||
H: Injectable<DependencyMap, Result<(), E>, Args> + Send + Sync + 'static,
|
||||
|
@ -33,12 +33,12 @@ where
|
|||
<R as Requester>::GetUpdatesFaultTolerant: Send,
|
||||
E: Debug + Send + Sync + 'static,
|
||||
{
|
||||
let cloned_requester = requester.clone();
|
||||
let cloned_bot = bot.clone();
|
||||
|
||||
commands_repl_with_listener(
|
||||
requester,
|
||||
bot,
|
||||
handler,
|
||||
update_listeners::polling_default(cloned_requester).await,
|
||||
update_listeners::polling_default(cloned_bot).await,
|
||||
cmd,
|
||||
)
|
||||
.await;
|
||||
|
@ -62,7 +62,7 @@ where
|
|||
/// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>(
|
||||
requester: R,
|
||||
bot: R,
|
||||
handler: H,
|
||||
listener: L,
|
||||
_cmd: PhantomData<Cmd>,
|
||||
|
@ -75,7 +75,7 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>(
|
|||
E: Debug + Send + Sync + 'static,
|
||||
{
|
||||
let mut dispatcher = DispatcherBuilder::new(
|
||||
requester,
|
||||
bot,
|
||||
Update::filter_message().add_command::<Cmd>().branch(dptree::endpoint(handler)),
|
||||
)
|
||||
.build();
|
||||
|
|
|
@ -20,7 +20,7 @@ use teloxide_core::requests::Requester;
|
|||
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
|
||||
/// [`Dispatcher`]: crate::dispatching::Dispatcher
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
pub async fn repl<R, H, E, Args>(requester: R, handler: H)
|
||||
pub async fn repl<R, H, E, Args>(bot: R, handler: H)
|
||||
where
|
||||
H: Injectable<DependencyMap, Result<(), E>, Args> + Send + Sync + 'static,
|
||||
Result<(), E>: OnError<E>,
|
||||
|
@ -28,13 +28,8 @@ where
|
|||
R: Requester + Send + Sync + Clone + 'static,
|
||||
<R as Requester>::GetUpdatesFaultTolerant: Send,
|
||||
{
|
||||
let cloned_requester = requester.clone();
|
||||
repl_with_listener(
|
||||
requester,
|
||||
handler,
|
||||
update_listeners::polling_default(cloned_requester).await,
|
||||
)
|
||||
.await;
|
||||
let cloned_bot = bot.clone();
|
||||
repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot).await).await;
|
||||
}
|
||||
|
||||
/// Like [`repl`], but with a custom [`UpdateListener`].
|
||||
|
@ -50,11 +45,8 @@ where
|
|||
/// [`repl`]: crate::dispatching::repls::repl()
|
||||
/// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
pub async fn repl_with_listener<'a, R, H, E, L, ListenerE, Args>(
|
||||
requester: R,
|
||||
handler: H,
|
||||
listener: L,
|
||||
) where
|
||||
pub async fn repl_with_listener<'a, R, H, E, L, ListenerE, Args>(bot: R, handler: H, listener: L)
|
||||
where
|
||||
H: Injectable<DependencyMap, Result<(), E>, Args> + Send + Sync + 'static,
|
||||
L: UpdateListener<ListenerE> + Send + 'a,
|
||||
ListenerE: Debug,
|
||||
|
@ -63,11 +55,9 @@ pub async fn repl_with_listener<'a, R, H, E, L, ListenerE, Args>(
|
|||
R: Requester + Clone + Send + Sync + 'static,
|
||||
{
|
||||
#[allow(unused_mut)]
|
||||
let mut dispatcher = DispatcherBuilder::new(
|
||||
requester,
|
||||
Update::filter_message().branch(dptree::endpoint(handler)),
|
||||
)
|
||||
.build();
|
||||
let mut dispatcher =
|
||||
DispatcherBuilder::new(bot, Update::filter_message().branch(dptree::endpoint(handler)))
|
||||
.build();
|
||||
|
||||
#[cfg(feature = "ctrlc_handler")]
|
||||
dispatcher.setup_ctrlc_handler();
|
||||
|
|
Loading…
Reference in a new issue