Skip to main content

Application

Struct Application 

Source
pub struct Application { /* private fields */ }
Expand description

The main entry point for a PTB-style Telegram bot application.

Implementations§

Source§

impl Application

Source

pub fn bot(&self) -> &Arc<ExtBot>

Returns a reference to the ExtBot used by this application.

Source

pub fn is_initialized(&self) -> bool

Returns true if the application has been initialized.

Source

pub fn is_running(&self) -> bool

Returns true if the application is currently running.

Source

pub fn concurrent_updates(&self) -> usize

Returns the maximum number of concurrent update processing tasks.

Source

pub fn user_data(&self) -> &Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>

Returns a reference to the per-user data store.

Source

pub fn chat_data(&self) -> &Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>

Returns a reference to the per-chat data store.

Source

pub fn bot_data(&self) -> &Arc<RwLock<HashMap<String, Value>>>

Returns a reference to the bot-wide data store.

Source

pub fn update_sender(&self) -> Sender<Update>

Returns a clone of the update sender channel.

Source

pub async fn initialize(&self) -> Result<(), ApplicationError>

Initialize the application: starts the bot, loads persisted data, and starts the job queue.

Source

pub async fn shutdown(&self) -> Result<(), ApplicationError>

Shut down the application. Must be called after stopping. Flushes persistence and releases resources.

Source

pub async fn start(self: &Arc<Application>) -> Result<(), ApplicationError>

Start the update dispatch loop. Must be called after initialize.

Source

pub async fn stop(&self) -> Result<(), ApplicationError>

Stop the application’s update dispatch loop and flush persistence.

Source

pub fn stop_running(&self)

Signal the update dispatch loop to stop without awaiting completion.

Source

pub async fn create_task( &self, future: impl Future<Output = ()> + Send + 'static, )

Spawn a background task and track its JoinHandle in pending_tasks.

The task will be awaited (with a timeout) when the application stops, preventing fire-and-forget futures from silently vanishing.

This mirrors Python’s Application.create_task.

Source

pub async fn update_persistence(&self)

Flush in-memory user, chat, and bot data to the persistence backend.

Source

pub async fn run_polling(self: Arc<Application>) -> Result<(), ApplicationError>

Start the bot with long-polling using sensible defaults.

Matches Python’s application.run_polling() – zero arguments needed. Defaults: poll_interval=0s, timeout=10s, no update filter, don’t drop pending.

Source

pub fn polling(self: &Arc<Application>) -> PollingBuilder

Returns a PollingBuilder for configuring and starting polling.

§Example
app.polling()
    .timeout(Duration::from_secs(30))
    .drop_pending(true)
    .start()
    .await?;
Source

pub async fn add_raw_handler(&self, handler: Handler, group: i32)

Register a single handler in the given dispatch group.

Handlers are dispatched in group order (ascending). Within a group, the first matching handler wins.

Source

pub async fn add_raw_handlers(&self, new_handlers: Vec<Handler>, group: i32)

Register multiple handlers at once into the given dispatch group.

Source

pub async fn remove_handler(&self, group: i32, index: usize) -> Option<Handler>

Remove the handler at index from group. Returns the removed handler, if found.

Source

pub async fn add_error_handler( &self, callback: Arc<dyn Fn(Option<Arc<Update>>, CallbackContext) -> Pin<Box<dyn Future<Output = bool> + Send>> + Sync + Send>, block: bool, )

Register an error handler that is invoked when a handler returns an error.

Source

pub async fn add_handler(&self, handler: impl Handler + 'static, group: i32)

Register a trait-based handler (CommandHandler, MessageHandler, etc.) into the Application’s dispatch system.

This bridges the trait-based handler system into the Application’s internal Handler struct, creating the CallbackContext and calling handle_update_with_context so that ergonomic handlers receive both the typed Update and a fully-populated CallbackContext.

§Example
use rust_tg_bot_ext::prelude::*;

async fn start(update: Update, context: Context) -> HandlerResult {
    context.reply_text(&update, "Hello!").await?;
    Ok(())
}

app.add_handler(CommandHandler::new("start", start), 0).await;
Source

pub async fn process_update( &self, update: Arc<Update>, ) -> Result<(), ApplicationError>

Dispatch a single update through all registered handler groups.

Source

pub async fn process_error( &self, update: Option<Arc<Update>>, error: Box<dyn Error + Sync + Send>, ) -> bool

M9: error handlers can signal stop by returning true.

Source

pub async fn drop_chat_data(&self, chat_id: i64)

Remove all stored data for the given chat.

Source

pub async fn drop_user_data(&self, user_id: i64)

Remove all stored data for the given user.

Source

pub async fn migrate_chat_data(&self, old: i64, new: i64)

Move chat data from old chat ID to new chat ID (e.g. after a group migration).

Trait Implementations§

Source§

impl Debug for Application

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

Source§

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>,

Source§

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