pub struct Application { /* private fields */ }Expand description
The main entry point for a PTB-style Telegram bot application.
Implementations§
Source§impl Application
impl Application
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Returns true if the application has been initialized.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Returns true if the application is currently running.
Sourcepub fn concurrent_updates(&self) -> usize
pub fn concurrent_updates(&self) -> usize
Returns the maximum number of concurrent update processing tasks.
Sourcepub fn user_data(&self) -> &Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>
pub fn user_data(&self) -> &Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>
Returns a reference to the per-user data store.
Sourcepub fn chat_data(&self) -> &Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>
pub fn chat_data(&self) -> &Arc<RwLock<HashMap<i64, HashMap<String, Value>>>>
Returns a reference to the per-chat data store.
Sourcepub fn bot_data(&self) -> &Arc<RwLock<HashMap<String, Value>>>
pub fn bot_data(&self) -> &Arc<RwLock<HashMap<String, Value>>>
Returns a reference to the bot-wide data store.
Sourcepub fn update_sender(&self) -> Sender<Update>
pub fn update_sender(&self) -> Sender<Update>
Returns a clone of the update sender channel.
Sourcepub async fn initialize(&self) -> Result<(), ApplicationError>
pub async fn initialize(&self) -> Result<(), ApplicationError>
Initialize the application: starts the bot, loads persisted data, and starts the job queue.
Sourcepub async fn shutdown(&self) -> Result<(), ApplicationError>
pub async fn shutdown(&self) -> Result<(), ApplicationError>
Shut down the application. Must be called after stopping. Flushes persistence and releases resources.
Sourcepub async fn start(self: &Arc<Application>) -> Result<(), ApplicationError>
pub async fn start(self: &Arc<Application>) -> Result<(), ApplicationError>
Start the update dispatch loop. Must be called after initialize.
Sourcepub async fn stop(&self) -> Result<(), ApplicationError>
pub async fn stop(&self) -> Result<(), ApplicationError>
Stop the application’s update dispatch loop and flush persistence.
Sourcepub fn stop_running(&self)
pub fn stop_running(&self)
Signal the update dispatch loop to stop without awaiting completion.
Sourcepub async fn create_task(
&self,
future: impl Future<Output = ()> + Send + 'static,
)
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.
Sourcepub async fn update_persistence(&self)
pub async fn update_persistence(&self)
Flush in-memory user, chat, and bot data to the persistence backend.
Sourcepub async fn run_polling(self: Arc<Application>) -> Result<(), ApplicationError>
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.
Sourcepub fn polling(self: &Arc<Application>) -> PollingBuilder
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?;Sourcepub async fn add_raw_handler(&self, handler: Handler, group: i32)
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.
Sourcepub async fn add_raw_handlers(&self, new_handlers: Vec<Handler>, group: i32)
pub async fn add_raw_handlers(&self, new_handlers: Vec<Handler>, group: i32)
Register multiple handlers at once into the given dispatch group.
Sourcepub async fn remove_handler(&self, group: i32, index: usize) -> Option<Handler>
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.
Sourcepub 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,
)
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.
Sourcepub async fn add_handler(&self, handler: impl Handler + 'static, group: i32)
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;Sourcepub async fn process_update(
&self,
update: Arc<Update>,
) -> Result<(), ApplicationError>
pub async fn process_update( &self, update: Arc<Update>, ) -> Result<(), ApplicationError>
Dispatch a single update through all registered handler groups.
Sourcepub async fn process_error(
&self,
update: Option<Arc<Update>>,
error: Box<dyn Error + Sync + Send>,
) -> bool
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.
Sourcepub async fn drop_chat_data(&self, chat_id: i64)
pub async fn drop_chat_data(&self, chat_id: i64)
Remove all stored data for the given chat.
Sourcepub async fn drop_user_data(&self, user_id: i64)
pub async fn drop_user_data(&self, user_id: i64)
Remove all stored data for the given user.
Sourcepub async fn migrate_chat_data(&self, old: i64, new: i64)
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).