AnyEvent::SlackRTM - AnyEvent module for interacting with the Slack RTM API


version 1.2


    use AnyEvent;
    use AnyEvent::SlackRTM;

    my $access_token = "<user or bot token>";
    my $channel_id = "<channel/group/DM id>";

    my $cond = AnyEvent->condvar;
    my $rtm = AnyEvent::SlackRTM->new($access_token);

    my $i = 1;
    my $keep_alive;
    my $counter;
    $rtm->on('hello' => sub {
        print "Ready\n";

        $keep_alive = AnyEvent->timer(interval => 60, cb => sub {
            print "Ping\n";

        $counter = AnyEvent->timer(interval => 5, cb => sub {
            print "Send\n";
                type => 'message',
                channel => $channel_id,
                text => "".$i++,
    $rtm->on('message' => sub {
        my ($rtm, $message) = @_;
        print "> $message->{text}\n";
    $rtm->on('finish' => sub {
        print "Done\n";



This provides an AnyEvent-based interface to the Slack Real-Time Messaging API. This allows a program to interactively send and receive messages of a WebSocket connection and takes care of a few of the tedious details of encoding and decoding messages.

As of this writing, the library is still a fairly low-level experience, but more pieces may be automated or simplified in the future.

Disclaimer: Note also that this API is subject to rate limits and any service limitations and fees associated with your Slack service. Please make sure you understand those limitations before using this library.



    method new($token, $client_opts)

Constructs a AnyEvent::SlackRTM object and returns it.

The $token option 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.

The $client_opts is an optional HashRef of AnyEvent::WebSocket::Client's configuration options, e.g. env_proxy, max_payload_size, timeout, etc.


    method start()

This will establish the WebSocket connection to the Slack RTM service.

You should have registered any events using "on" before doing this or you may miss some events that arrive immediately.


    method metadata() returns HashRef

The initial connection is established after calling the rtm.start method on the web API. This returns some useful information, which is available here.

This will only contain useful information after "start" is called.


    method quiet($quiet?) returns Bool

Normally, errors are sent to standard error. If this flag is set, that does not happen. It is recommended that you provide an error handler if you set the quiet flag.


    method on($type, \&cb, ...)

This sets up a callback handler for the named message type. The available message types are available in the Slack Events documentation. Only one handler may be setup for each event. Setting a new handler with this method will replace any previously set handler. Events with no handler will be ignored/unhandled.

You can specify multiple type/callback pairs to make multiple registrations at once.


    method off(@types)

This removes the handler for the named @types.


    method send(\%msg)

This sends the given message over the RTM socket. Slack requires that every message sent over this socket must have a unique ID set in the "id" key. You, however, do not need to worry about this as the ID will be set for you.


    method ping(\%msg)

This sends a ping message over the Slack RTM socket. You may add any paramters you like to %msg and the return "pong" message will echo back those parameters.


    method said_hello() returns Bool

Returns true after the "hello" message has been received from the server.


    method finished() returns Bool

Returns true after the "finish" message has been received from the server (meaning the connection has been closed). If this is true, this object should be discarded.


    method close()

This closes the WebSocket connection to the Slack RTM API.


This is a low-level API. Therefore, this only aims to handle the basic message handling. You must make sure that any messages you send to Slack are formatted correctly. You must make sure any you receive are handled appropriately. Be sure to read the Slack documentation basic message formatting, attachment formatting, rate limits, etc.



Andrew Sterling Hanenkamp <>


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

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.