Struct telegram_bot::Listener [−][src]
pub struct Listener { /* fields omitted */ }
Offers methods to easily receive new updates via the specified method. This
should be used instead of calling methods like get_updates
yourself.
To create a listener, you first have to create an Api
object and call
listener
on it. In order to make listening easier in a concurrent
environment, the Listener
object and the Api
object don't share any
internal state. This makes creating a Listener
a bit more expensive, but
it's usually sufficient for any purpose to create a Listener
only once.
Methods
impl Listener
[src]
impl Listener
pub fn listen<H>(&mut self, handler: H) -> Result<()> where
H: FnMut(Update) -> Result<ListeningAction>,
[src]
pub fn listen<H>(&mut self, handler: H) -> Result<()> where
H: FnMut(Update) -> Result<ListeningAction>,
Receive and handle updates with the given closure.
This method will use the specified listening method to receive new
updates and will then call the given handler for every update. Normally
the handler won't ever be called for the same update twice (see Note
below).
When the handler returns an Err
value, this function will stop
listening and return the same Err
. If you want to stop listening you
can return Ok(ListeningAction::Stop)
instead of an Err
value.
When returning an Ok
value, the update that was passed to the handler
is considered handled and won't be passed to a handler again. On the
other hand if an Err
is returned, the update is not considered handled
so it will be passed to a handler the next time again.
Note:
If you are listening via LongPoll
method and your handler panics or
the program is aborted in an abnormal way (e.g. SIGKILL
), the handler
might receive some already handled updates a second time.
pub fn channel(self) -> (Sender<Result<ListeningAction>>, Receiver<Update>)
[src]
pub fn channel(self) -> (Sender<Result<ListeningAction>>, Receiver<Update>)
Consumes self
and returns a sender-receiver pair. You can receive
new updates through the Receiver. Each update needs to be confirmed
with a Result<ListeningAction>
before the next update can be handled.
This means that handling updates isn't done in parallel. The only
advantage of this function over the listen
function is that you can
ask the receiver, if a new update has arrived. This is useful if you
want to handle different events in one thread. E.g. a remainder bot
gets active on every received message AND on timed events.
Note: Remember to send a result through the Sender
after each
update!