To create a custom asynchronous Telegram client, you must implement the AsyncTelegramApi trait. This trait provides a high-level interface for all Telegram Bot API methods.
Key requirements for implementation:
- The implementation must be
Sync. - You must implement the core request methods:
request, request_with_form_data, and request_with_possible_form_data to handle the actual network communication. - The trait is designed to work in both standard and
wasm32 environments. In wasm32 targets, the implementation does not need to be Send because the runtime is single-threaded.
Once implemented, you gain access to all Telegram methods (e.g., get_updates, send_message, send_photo) as asynchronous functions.
pub trait AsyncTelegramApi
where
Self: Sync,
{
type Error;
// You must implement these core methods to power the rest of the API
async fn request<Params, Output>(&self, method: &str, params: Option<Params>) -> Result<Output, Self::Error>
where Params: serde::ser::Serialize + std::fmt::Debug + std::marker::Send, Output: serde::de::DeserializeOwned;
async fn request_with_form_data<Params, Output>(&self, method: &str, params: Params, files: Vec<(&str, PathBuf)>) -> Result<Output, Self::Error>
where Params: serde::ser::Serialize + std::fmt::Debug + std::marker::Send, Output: serde::de::DeserializeOwned;
async fn request_with_possible_form_data<Params, Output>(&self, method_name: &str, params: Params, files: Vec<(&str, PathBuf)>) -> Result<Output, Self::Error>
where Params: serde::ser::Serialize + std::fmt::Debug + std::marker::Send, Output: serde::de::DeserializeOwned;
}