Crate awesome_bot[][src]

This crate helps writing bots for Telegram. This is a framework to build the bots, the main wrapped crate is telegram-bot which provides the actual protocol access.

How to use it

The first step is always to create the AwesomeBot instance, this will represent your bot, so, if you have more than one bot, you will have to create more instances.

You have two ways to create the bot, with new, that you pass the bot token directly, or with from_env, more recommended.

This framework uses the "route" pattern to apply behavior. There are plenty of routing ways available, the main ones are: command, simple_command, any_fn. But there are many more, check the docs of AwesomeBot

Then, to send things you need to use the SendBuilder struct, to create an instance use the send or answer methods in AwesomeBot.

This uses the "builder" pattern, for example, to answer a message with a text, while disabling web pages, you do (Assuming that bot is &AwesomeBot and msg is &Message):

This example is not tested
bot.answer(msg).text("Answering this text").disable_preview(true).end();

Check SendBuilder struct implementation to see the methods available (text, photo, audio, ...)

Once you have all your routings, you need to start the bot, right now it supports only getUpdates method, just call the simple_start method in AwesomeBot.

You don't have to worry about blocking the bot in a function handler, because it uses a thread pool (of 4 threads right now, it will be configurable in the future), so the handling for a message is done in his own thread.

Examples

Minimalistic example (Echo text)

extern crate awesome_bot;

use awesome_bot::*;

fn echohandler(bot: &AwesomeBot, msg: &Message, _: String, args: Vec<String>) {
    // We can access safely because the pattern match have that argument mandatory
    let toecho = &args[1];
    let phrase = format!("Echoed: {}", toecho);
    // Send the text in a beauty way :)
    let sended = bot.answer(msg).text(&phrase).end();
    println!("{:?}", sended);
}

fn main() {
    // Create the Awesome Bot (You need TELEGRAM_BOT_TOKEN environment with the token)
    let mut bot = AwesomeBot::from_env("TELEGRAM_BOT_TOKEN");
    // Add a command, this will add the routing to that function.
    bot.command("echo (.+)", echohandler);

    // Start the bot with getUpdates
    let res = bot.simple_start();
    if let Err(e) = res {
        println!("An error occurred: {}", e);
    }
}

More examples

You have more examples in examples/ directory in the project's repository.

Re-exports

pub use telegram_bot::*;

Structs

AwesomeBot

Main type for building the Telegram Bot.

SendAction

Transparent struct built by SendBuilder to send Action messages.

SendAudio

Transparent struct built by SendBuilder to send Audio messages.

SendBuilder

SendBuilder it's a builder struct that allows you to construct answers in a composable way, you will use it transparently with the send and answer methods of AwesomeBot

SendDocument

Transparent struct built by SendBuilder to send Document messages.

SendForward

Transparent struct built by SendBuilder to send Forward messages.

SendLocation

Transparent struct built by SendBuilder to send Location messages.

SendPhoto

Transparent struct built by SendBuilder to send Photo messages.

SendSticker

Transparent struct built by SendBuilder to send Sticker messages.

SendText

Transparent struct built by SendBuilder to send Text messages.

SendVideo

Transparent struct built by SendBuilder to send Video messages.

SendVoice

Transparent struct built by SendBuilder to send Voice messages.

Enums

GeneralSound

Represents audio and voice, this is used in all_music_fn handler.

Traits

Finisher

Help trait indicating that at least the end method is implemented for the SendBuilder structs