Bot::Backbone::Service::SlackChat - Connect and chat with a Slack server


version 0.161950


    package MyBot;
    use Bot::Backbone;

    service slack_chat => (
        service => 'SlackChat',
        token   => '...', # see for your tokens

    service dice => (
        service => 'OFun::Dice',

    service "general_chat" => (
        service    => 'GroupChat',
        chat       => 'SlackChat',
        group      => 'C',
        dispatcher => 'general_dispatch',

    dispatcher 'general_dispatch' => as {
        redispatch_to "dice";



This allows a Bot::Backbone chat bot to be connect to a Slack server using their Real-Time Messaging API.

This is based on AnyEvent::SlackRTM and WebService::Slack::WebApi. It also uses a CHI cache to help avoid contacting the Slack server too often, which could result in your bot becoming rate limited.



The token is the access token from Slack to use. This may be either of the following type of tokens:

  • User Token. This is a token to perform actions on behalf of a user account.

  • Bot Token. If you configure a bot integration, you may use the access token on the bot configuration page to use this library to act on behalf of the bot account. Bot accounts may not have the same features as a user account, so please be sure to read the Slack documentation to understand any differences or limitations.

Which you use will depend on whether you want the bot to control a user account or a bot integration account. You are responsible for adhering to the Slack terms of use in whatever you do.


This may be set to a subroutine to call whenever the bot is invited to join a channel. This allows the bot to be configured to handle channels as it is invited to them.

For example:

    service slack_chat => (
        service           => 'SlackChat',
        on_channel_joined => sub {
            my ($slack, $channel, $name, $during_init) = @_;
            service "group_$name" => (
                service    => 'GroupChat',
                chat       => 'slack_chat',
                group      => $channel,
                dispatcher => 'general',

The called subroutine will be passed this object (from which you can make API calls via $slack->api), the Slack ID of the newly joined channel, the human name of the newly joined channel, and the "during init" flag. The boolean flag sent as the third argument is set to true if the callback is being called while the SlackChat service is being initialized. If the flag is false, this indicates that it is happening in reaction to the bot receiving a "channel_joined" message while running.


This is a CHI cache to use to temporarily store response from the Slack APIs. By default, this is a memory-only cache that caches data for only 60 seconds. The purpose is mainly to prevent repeated requests to the API, which might result in rate limiting.


This is the WebService::Slack::WebApi object used to contact Slack for information about channels, users, etc.


This is the AnyEvent::SlackRTM object used to communicate with Slack and trigger events from the Real-Time Messaging API.


This is a callback sub that may be used to report error events from the RTM API. Set it to a sub that will be called as follows:

    sub {
        my ($self, $rtm, $message) = @_;


Here, $self is the Bot::Backbone::Service::SlackChat object, $rtm is the AnyEvent::SlackRTM object, and $message is a hash containing the error message, as described on the Real Time Messaging API documentation.


This returns a hash containing information about who the bot is.



Returns the name of the bot.


Returns the user ID for the bot.


Returns the team ID for the team account.


This connects to Slack and prepares the bot for communication.


    method load_user($by, $value) returns Bot::Backbone::Identity

Fetches information about a user from Slack and returns the user as a Bot::Backbone::Identity. The $by setting determines how the user is looked up, which may either be by "id" or by "name". The value, then, is the value to check.


    method load_me() returns Bot::Backbone::Identity

Returns the identity object for the bot itself.


    method load_user_channel($by, $value) returns Str

Returns the ID of a user's IM channel. Here $by may be "user" to lookup by user ID.


    method join_group({ group => $group })

Given the ID of a channel or group, this causes the bot to open or join it. Note that Slack bot integration accounts might not be able to join team channels, but may still be invited.

CAVEAT: Slack does not permit bots to join groups, so this method call will be a no-op for bot users. This will only work if this code is operating a regular user account.


Handles messages from Slack. Decides whether they are group messages or direct and forwards them on as appropriate. Messages with a "subtype" will be ignored as will messages that are "edited".

This method also marks messages as read.


Handles direct messages received from an IM channel.


This determines whether or not the message is to the bot.


This handles message received from private group or team channels.


    method send_message({
        to          => $user_id,
        group       => $group_id,
        text        => $message,
        attachments => $attachments,

This sends a message to a Slack channel. To the named user's IM channel or to a private group or team channel named by $group_id. Attachments can be included to produce formatted messages.

Slack Message Attachment API


Andrew Sterling Hanenkamp <>


This software is copyright (c) 2016 by Qubling Software LLC.
