pub struct Router<Client> {Show 25 fields
pub message: Observer<Client>,
pub edited_message: Observer<Client>,
pub channel_post: Observer<Client>,
pub edited_channel_post: Observer<Client>,
pub business_connection: Observer<Client>,
pub business_message: Observer<Client>,
pub edited_business_message: Observer<Client>,
pub deleted_business_messages: Observer<Client>,
pub message_reaction: Observer<Client>,
pub message_reaction_count: Observer<Client>,
pub inline_query: Observer<Client>,
pub chosen_inline_result: Observer<Client>,
pub callback_query: Observer<Client>,
pub shipping_query: Observer<Client>,
pub pre_checkout_query: Observer<Client>,
pub poll: Observer<Client>,
pub poll_answer: Observer<Client>,
pub my_chat_member: Observer<Client>,
pub chat_member: Observer<Client>,
pub chat_join_request: Observer<Client>,
pub chat_boost: Observer<Client>,
pub removed_chat_boost: Observer<Client>,
pub update: Observer<Client>,
pub startup: Observer,
pub shutdown: Observer,
/* private fields */
}
Expand description
Router combines all event observers.
Each event observer is a special unit that handles a specific event type. There are two types of event observers:
- Simple observer -
SimpleObserver
Simple observer is used to handle simple events like startup and shutdown.
When you register a handler in this observer,
you specify the arguments that pass to the handler when the event is trigger.
Return type of the handler is Result<(), HandlerError>
.
When observer is trigger, it calls all handlers in order of registration and stops if one of them returns an error.
Registration of the handlers looks like this:
async fn on_startup(message: &str) -> HandlerResult {
...
}
async fn on_shutdown(message: &str) -> HandlerResult {
...
}
let mut router = Router::new("example");
router.startup.register(on_startup, ("Hello, world!",));
router.shutdown.register(on_shutdown, ("Goodbye, world!",));
- Telegram observer -
TelegramObserver
Telegram observer is used to handle telegram events like messages, callback queries, polls and all other event types.
You can register a handler with any arguments that implement crate::extractors::FromEventAndContext
trait,
see crate::extractors
for more details.
Return type of the handler is Result<EventReturn, HandlerError>
,
where EventReturn
is a special enum that can be used to control the propagation of the event,
see EventReturn
for more details.
When observer is trigger, it calls outer middlewares and checks all handlers in order of registration.
It calls all filters for each handler and skips the handler if one of them returns false
.
If the handler is pass the filters, observer calls inner middlewares and the handler itself (in the middleware).
By default, the first handler that pass the filters stop the propagation of the event, so other handlers aren’t calls.
(You can change this behaviour by specify another variant of EventReturn
).
Registration of the handlers looks like this:
async fn on_message(message: Message) -> HandlerResult {
...
}
async fn on_callback_query(callback_query: CallbackQuery) -> HandlerResult {
...
}
let mut router = Router::new("example");
router.message.register(on_message);
router.callback_query.register(on_callback_query);
Fields§
§message: Observer<Client>
§edited_message: Observer<Client>
§channel_post: Observer<Client>
§edited_channel_post: Observer<Client>
§business_connection: Observer<Client>
§business_message: Observer<Client>
§edited_business_message: Observer<Client>
§deleted_business_messages: Observer<Client>
§message_reaction: Observer<Client>
§message_reaction_count: Observer<Client>
§inline_query: Observer<Client>
§chosen_inline_result: Observer<Client>
§callback_query: Observer<Client>
§shipping_query: Observer<Client>
§pre_checkout_query: Observer<Client>
§poll: Observer<Client>
§poll_answer: Observer<Client>
§my_chat_member: Observer<Client>
§chat_member: Observer<Client>
§chat_join_request: Observer<Client>
§chat_boost: Observer<Client>
§removed_chat_boost: Observer<Client>
§update: Observer<Client>
This special event observer is used to handle all telegram events.
It’s called for router and its sub routers and before other telegram observers.
This observer is useful for register important middlewares (often libraries) like FSMContext
and UserContext
,
that set up context for other.
startup: Observer
§shutdown: Observer
Implementations§
source§impl<Client> Router<Client>
impl<Client> Router<Client>
sourcepub fn new(router_name: &'static str) -> Self
pub fn new(router_name: &'static str) -> Self
§Arguments
router_name
- Name of the router. It can be used for logging and debugging and code clarity.
sourcepub fn include_router(&mut self, router: impl Into<Router<Client>>) -> &mut Self
pub fn include_router(&mut self, router: impl Into<Router<Client>>) -> &mut Self
Include a router to the current router as sub router
§Notes
Inner middlewares of this router will be registered to the sub router and its sub routers in the order of registration. Parent middlewares registers on the top of the stack, so parent middlewares calls before.
sourcepub fn include(&mut self, router: impl Into<Router<Client>>) -> &mut Self
pub fn include(&mut self, router: impl Into<Router<Client>>) -> &mut Self
Include a router to the current router as sub router
§Notes
Inner middlewares of this router will be registered to the sub router and its sub routers in the order of registration. Parent middlewares registers on the top of the stack, so parent middlewares calls before.
Alias to Router::include_router
method
source§impl<Client> Router<Client>
impl<Client> Router<Client>
sourcepub const fn telegram_observers(&self) -> [&TelegramObserver<Client>; 23]
pub const fn telegram_observers(&self) -> [&TelegramObserver<Client>; 23]
Get all telegram event observers
sourcepub fn telegram_observers_mut(&mut self) -> Vec<&mut TelegramObserver<Client>>
pub fn telegram_observers_mut(&mut self) -> Vec<&mut TelegramObserver<Client>>
Get all telegram event observers as mutable references
§Notes
This method is useful for registering middlewares to the many observers without code duplication and macros
sourcepub const fn event_observers(&self) -> [&SimpleObserver; 2]
pub const fn event_observers(&self) -> [&SimpleObserver; 2]
Get all simple event observers
sourcepub fn resolve_used_update_types_with_skip(
&self,
skip_update_types: impl IntoIterator<Item = UpdateType>,
) -> HashSet<UpdateType>
pub fn resolve_used_update_types_with_skip( &self, skip_update_types: impl IntoIterator<Item = UpdateType>, ) -> HashSet<UpdateType>
Resolve used update types from the current router and its sub routers with skip some update types. If observer has no handlers, then it will be skipped. If observer update type is in the skip list, then it will be skipped. This method is useful for getting updates only for registered update types.
§Panics
If can’t convert observer event name to UpdateType
sourcepub fn resolve_used_update_types(&self) -> HashSet<UpdateType>
pub fn resolve_used_update_types(&self) -> HashSet<UpdateType>
Resolve used update types from the current router and its sub routers. If observer has no handlers, then it will be skipped. This method is useful for getting updates only for registered update types.
§Panics
If can’t convert observer event name to UpdateType