Crate telegram_bot[−][src]
This crate helps writing bots for the messenger Telegram. Here is a minimalistic example:
use telegram_bot::*; // Create the Api from a bot token saved in a environment variable and // test an Api-call let api = Api::from_env("TELEGRAM_BOT_TOKEN").unwrap(); println!("getMe: {:?}", api.get_me()); // We want to listen for new updates via LongPoll let mut listener = api.listener(ListeningMethod::LongPoll(None)); // Fetch new updates listener.listen(|u| { // If the received update contains a message... if let Some(m) = u.message { // if the message was a text message: if let MessageType::Text(_) = m.msg { // Answer message with "Hi" try!(api.send_message( m.chat.id(), format!("Hi, {}!", m.from.first_name), None, None, None, None) ); } } // If none of the "try!" statements returned an error: It's Ok! Ok(ListeningAction::Continue) });
How to use it
Note: You should be familiar with the official HTTP Api to use this library effectivly.
The first step is always to create an Api
object. You need one Api
for
every bot (token) you want to control. You can either create it directly
from a token with from_token
or, since you shouldn't hardcode your token,
a bit easier: From an environment variable with from_env
.
The Api
object has all methods of the Telegram HTTP API, like
send_message
. For more information see the Api
struct documentation.
Next you want to listen for new updates. This is best done via the listen
method on the Listener
type. To obtain a listener, call listener
on the
Api
object.
Examples
There are two examples in the examples/
directory in the project's
repository.
Re-exports
pub use types::*; |
Modules
types |
Types of the Telegram API. |
Structs
Api |
Main type for sending requests to the Telegram bot API. |
Listener |
Offers methods to easily receive new updates via the specified method. This
should be used instead of calling methods like |
Enums
Error |
Telegram-Bot Error: Anything that may fail (HTTP, JSON, ...) |
ListeningAction |
A listening handler returns this type to signal the listening-method either
to stop or to continue. If a handler returns |
ListeningMethod |
Different method how to listen for new updates. Currently |
Constants
API_URL |
API-URL prefix |
Type Definitions
Result |
Telegram-Bot Result |