Struct telers::router::Router

source ·
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 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 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>
where Client: Send + Sync + 'static,

source

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.
source

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.

source

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>

source

pub const fn telegram_observers(&self) -> [&TelegramObserver<Client>; 23]

Get all telegram event observers

source

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

source

pub const fn event_observers(&self) -> [&SimpleObserver; 2]

Get all simple event observers

source

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

source

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

Trait Implementations§

source§

impl<Client> AsRef<Router<Client>> for Router<Client>

source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<Client> Debug for Router<Client>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Client> Default for Router<Client>
where Client: Send + Sync + 'static,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<Client> ToServiceProvider for Router<Client>
where Client: Send + Sync + 'static,

§

type Config = Config<Client>

Service factory configuration
§

type ServiceProvider = Service<Client>

The provider to which the service will be converted
§

type InitError = ()

Errors potentially raised while building a service
source§

fn to_service_provider( self, config: Self::Config, ) -> Result<Self::ServiceProvider, Self::InitError>

Convert the service factory to the service Read more

Auto Trait Implementations§

§

impl<Client> Freeze for Router<Client>

§

impl<Client> !RefUnwindSafe for Router<Client>

§

impl<Client> Send for Router<Client>

§

impl<Client> Sync for Router<Client>

§

impl<Client> Unpin for Router<Client>

§

impl<Client> !UnwindSafe for Router<Client>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T