4 releases

0.1.3 Apr 24, 2019
0.1.2 Apr 24, 2019
0.1.1 Apr 24, 2019
0.1.0 Apr 19, 2019

#40 in #bot-api

MIT license

160KB
4K SLoC

Telegrambot

Build Status

A telegram bot api for rust.

Usage

telegrambot = "0.1"

Example

A simple example. (example/simple)

use futures::future::Future;

use telegrambot::api::SendMessage;
use telegrambot::TelegramBot;

fn main() {
  let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
  let cfg = Config::builder(token)
    .proxy("http://127.0.0.1:1081")
    .build()
    .unwrap();

  TelegramBot::new(cfg).unwrap()
    .on_text(|(api, vtm)| {
      let first_name = vtm.message.from.unwrap().first_name;
      println!("<{}>: {}", first_name, vtm.text);
      telegrambot::spawn(
        api.send_message(&SendMessage::new(vtm.message.chat.id(),
         format!("Hi, {}! You just wrote '{}'", first_name, vtm.text)))
          .map(|_| {})
          .map_err(|_| {})
      );
    })
    .start()
    .unwrap();
}

A Command example (example/command.rs)

use futures::future::Future;

use telegrambot::api::SendMessage;
use telegrambot::TelegramBot;

fn main() {
  let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
  let cfg = Config::builder(token)
    .proxy("http://127.0.0.1:1081")
    .build()
    .unwrap();

  TelegramBot::new(cfg).unwrap()
    .on_command("/list", |(api, vcm)| {
      telegrambot::spawn(
        api.send_message(&SendMessage::new(vcm.message.chat.id(),
         format!("Call list command")))
          .map(|_| {})
          .map_err(|_| {})
      );
    })
    .on_command("/page", |(api, vcm)| {
      telegrambot::spawn(
        api.send_message(&SendMessage::new(vcm.message.chat.id(),
         format!("Call page command, and arguments for this command: {:?}", vcm.args)))
          .map(|_| {})
          .map_err(|_| {})
      );
    })
    .start()
    .unwrap();
}

Support for parameter analysis. Some example:

  • /page 1 10

    args: ["1", "10"]

  • /start "Just do it" 'from now on'

    args: ["Just do it", "from now on"]

  • /run 'Just do it'

    args: ["Just do it"]

More

More example you can see examples

Other

Thanks to telegram-rs/telegram-bot project, telegram is a modification to telegram-rs/telegram-bot

Dependencies

~18–27MB
~482K SLoC