Rust Telegram Bot Library

Build Status License Crates.io

Documentation

A library for writing your own Telegram bots. More information here. Official API here. Note: Work in progress!

Example

Here is a simple example (see example/simple.rs):

``` rust extern crate telegram_bot;

use telegram_bot::*; use std::env;

fn main() { // Fetch environment variable with bot token let token = match env::var("TELEGRAMBOTTOKEN") { Ok(tok) => tok, Err(e) => panic!("Environment variable 'TELEGRAMBOTTOKEN' missing! {}", e), };

// Create bot, test simple API call and print bot information
let mut bot = Bot::new(token);
println!("getMe: {:?}", bot.get_me());

// Fetch new updates via long poll method
let res = bot.long_poll(None, |bot, u| {
    // If the received update contains a message...
    if let Some(m) = u.message {
        let name = m.from.first_name;

        // Match message type
        match m.msg {
            MessageType::Text(t) => {
                // Print received text message to stdout
                println!("<{}> {}", name, t);

                if t == "/exit" {
                    return Err(Error::UserInterrupt);
                }

                // Answer message with "Hi"
                try!(bot.send_message(
                    m.chat.id(),
                    format!("Hi, {}! You just wrote '{}'", name, t),
                    None, None, None));
            },
            _ => {}
        }
    }

    // If none of the "try!" statements returned an error: It's Ok!
    Ok(())
});

// When the method `long_poll` returns, its due to an error. Check it here.
if let Err(e) = res {
    println!("An error occured: {}", e);
}

} `` You can find a bigger example in theexamples` folder.

Usage

This library is available via crates.io. In order to use it, just add this to your Cargo.toml:

telegram-bot = "*"

Collaboration

Yes please! Every type of contribution is welcome: Create issues, hack some code or make suggestions. If you don't know where to start, just contact me (my email is on my github profile).

Please submit pull request against the dev branch, unless all changes are just documentation fixes.

Todo