Struct slackbot::SlackBot [] [src]

pub struct SlackBot {
    // some fields omitted
}

The bot that handles commands and communication with Slack.

Methods

impl SlackBot

fn new<A, B>(name: A, token: B) -> Self where A: Into<String>, B: Into<String>

Create a new bot to serve your team!

Examples

use slackbot::SlackBot;

let mut my_bot = SlackBot::new("bot", "YOUR_API_TOKEN");

fn on<S: Into<String>>(&mut self, command_name: S, handler: Box<CommandHandler>)

Tell your bot what to do when it sees a command.

The handler can be your own type that implements CommandHandler, but most simple cases can be covered by a simple closure.

Examples

With a simple closure:

my_bot.on("say-hello", Box::new(|sender: &mut Sender, args: &Vec<String>| {
    sender.respond_in_channel("Hello, world!");
}));

With an implemented CommandHandler:

struct SayHelloCommandHandler;

impl CommandHandler for SayHelloCommandHandler {
    fn handle(&mut self, sender: &mut Sender, args: &Vec<String>) {
        sender.respond_in_channel("Hello, world!");
    }
}

my_bot.on("say-hello", Box::new(SayHelloCommandHandler));

fn run(&mut self) -> Result<(), String>

Tell your bot to start pulling its weight!

Examples

match my_bot.run() {
    Ok(()) => println!("Bot shut down successfully. Goodbye world!"),
    Err(err) => println!("Bot crashed. Error message: {}", err)
};