|
| 1 | +//! This crate helps writing bots for the messenger Telegram. Here is a |
| 2 | +//! minimalistic example: |
| 3 | +//! |
| 4 | +//! ``` |
| 5 | +//! // Create the Api from a bot token saved in a environment variable and |
| 6 | +//! // test an Api-call |
| 7 | +//! let mut api = Api::from_env("TELEGRAM_BOT_TOKEN").unwrap(); |
| 8 | +//! println!("getMe: {:?}", api.get_me()); |
| 9 | +//! // We want to listen for new updates via LongPoll |
| 10 | +//! let mut listener = api.listener(ListeningMethod::LongPoll(None)); |
| 11 | +//! |
| 12 | +//! // Fetch new updates |
| 13 | +//! listener.listen(|u| { |
| 14 | +//! // If the received update contains a message... |
| 15 | +//! if let Some(m) = u.message { |
| 16 | +//! let name = m.from.first_name; |
| 17 | +//! |
| 18 | +//! // ... and it was a text message: |
| 19 | +//! if let MessageType::Text(_) = m.msg { |
| 20 | +//! // Answer message with "Hi" |
| 21 | +//! try!(api.send_message( |
| 22 | +//! m.chat.id(), |
| 23 | +//! format!("Hi, {}!", m.from.first_name), |
| 24 | +//! None, None, None) |
| 25 | +//! ); |
| 26 | +//! } |
| 27 | +//! } |
| 28 | +//! |
| 29 | +//! // If none of the "try!" statements returned an error: It's Ok! |
| 30 | +//! Ok(HandlerResult::Continue) |
| 31 | +//! }); |
| 32 | +//! ``` |
| 33 | +//! |
| 34 | +//! How to use it |
| 35 | +//! ------------- |
| 36 | +//! |
| 37 | +//! *Note*: You should be familiar with the |
| 38 | +//! [official HTTP Api](https://core.telegram.org/bots/api) to use this library |
| 39 | +//! effectivly. |
| 40 | +//! |
| 41 | +//! The first step is always to create an `Api` object. You need one `Api` for |
| 42 | +//! every bot (token) you want to control. You can either create it directly |
| 43 | +//! from a token with `from_token` or, since you shouldn't hardcode your token, |
| 44 | +//! a bit easier: From an environment variable with `from_env`. |
| 45 | +//! |
| 46 | +//! The `Api` object has all methods of the Telegram HTTP API, like |
| 47 | +//! `send_message`. For more information see the `Api` struct documentation. |
| 48 | +//! |
| 49 | +//! Next you want to listen for new updates. This is best done via the `listen` |
| 50 | +//! method on the `Listener` type. To obtain a listener, call `listener` on the |
| 51 | +//! `Api` object. |
| 52 | +//! |
| 53 | +//! Examples |
| 54 | +//! -------- |
| 55 | +//! |
| 56 | +//! There are two examples in the `examples/` directory in the project's |
| 57 | +//! repository. |
1 | 58 | extern crate hyper;
|
2 | 59 | extern crate rustc_serialize;
|
3 | 60 | extern crate url;
|
|
0 commit comments