telegrambot

Crates.iotelegrambot
lib.rstelegrambot
version0.1.3
sourcesrc
created_at2019-04-19 08:29:23.960606
updated_at2019-04-24 09:30:57.721019
descriptionTelegram bot api
homepage
repositoryhttps://github.com/fewensa/telegrambot
max_upload_size
id128939
size166,803
(fewensa)

documentation

README

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

Commit count: 55

cargo fmt