Struct telexide::client::Client

source ·
pub struct Client {
    pub api_client: Arc<Box<dyn API + Send>>,
    pub data: Arc<RwLock<TypeMap>>,
    pub allowed_updates: Vec<UpdateType>,
    /* private fields */
}
Expand description

The Client is the main object to manage your interaction with telegram.

It handles the incoming update objects from telegram and dispatches them to your event handlers and commands, providing those with access to shared data and easy access to the telegram API itself.

Event Handlers

Event handlers can be configured to be called upon every update that is received. (Later on support will be added for subscribing to more specific update events)

Note that you do not need to manually handle retrieving updates, as they are handled internally and then dispatched to your event handlers.

Examples

use telexide::prelude::*;

#[prepare_listener]
async fn event_listener(ctx: Context, update: Update) {
    println!("received an update!")
}

#[tokio::main]
async fn main() -> telexide::Result<()> {
    let mut client = Client::new(token);
    client.subscribe_handler_func(event_listener);

    client.start().await
}

Fields§

§api_client: Arc<Box<dyn API + Send>>

The API client, it contains all the methods to talk to the telegram api, more documentation can be found over at the API docs

§data: Arc<RwLock<TypeMap>>

Your custom data that you want to be shared amongst event handlers and commands.

The purpose of the data field is to be accessible and persistent across contexts; that is, data can be modified by one context, and will persist through the future and be accessible through other contexts. This is useful for anything that should “live” through the program: counters, database connections, custom user caches, etc. Therefore this TypeMap requires all types it will contain to be Send + Sync.

When using a Context, this data will be available as Context::data.

Refer to the repeat_image_bot example for an example on using the data field

§allowed_updates: Vec<UpdateType>

The update types that you want to receive, see the documentation of UpdateType for more information

Implementations§

source§

impl Client

source

pub fn new(token: impl ToString) -> Self

Creates a Client object with default values and no framework

source

pub fn with_framework(fr: Arc<Framework>, token: impl ToString) -> Self

Creates a Client object with default values, but with a Framework

source

pub fn builder() -> ClientBuilder

Returns a new ClientBuilder

source

pub async fn start(&self) -> Result<()>

Starts the client and blocks until an error happens in the updates stream or the program exits (for example due to a panic). If using the framework, it will update your commands in telegram. If using a webhook, it will handle it, else it will use polling using a default UpdatesStream object

source

pub async fn start_with_stream(&self, stream: &mut UpdatesStream) -> Result<()>

Starts the client and blocks until an error happens in the updates stream or the program exits (for example due to a panic). If using the framework, it will update your commands in telegram You have to provide your own UpdatesStream object

source

pub async fn start_with_webhook(&self, opts: &WebhookOptions) -> Result<()>

Starts the client and blocks until an error happens in the webhook handling or the program exits (for example due to a panic). If using the framework, it will update your commands in telegram You have to provide your own WebhookOptions object

source

pub fn subscribe_handler_func(&mut self, handler: EventHandlerFunc)

Subscribes an update event handler function (EventHandlerFunc) to the client and will be ran whenever a new update is received

source

pub fn subscribe_raw_handler(&mut self, handler: RawEventHandlerFunc)

Subscribes a raw update event handler function (RawEventHandlerFunc) to the client and will be ran whenever a new update is received

Trait Implementations§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl From<Box<dyn API + Send>> for Client

source§

fn from(api: Box<dyn API + Send>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneableStorage for Twhere T: Any + Send + Sync + Clone,

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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