AnyEvent-SlackRTM

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

1.3       2022-07-29 12:41:25-05:00 America/Chicago

    * Switch from rtm.start to rtm.connect. (HT: @afresh1)
    * Add keep-alive watchdog timer to keep the connection up. (HT: @afresh1)

1.1       2019-10-03 14:01:15-05:00 America/Chicago

    * Adding client options to constructor to allow options to be passed through
      to the underlying AnyEvent::WebSocket::Client connection. (HT: ibutorin)

1.0       2017-09-21 21:56:14-05:00 America/Chicago

    * Fixed a bug where errors weren't quieted when the quiet flag was set by
      the developer.

Changes  view on Meta::CPAN


0.152660  2015-09-23 10:39:17CDT-0500 America/Chicago

    * Fixed the SYNOPSIS code. HT: robn
    * Removed the dependency on AIO. HT: robn

0.151800  2015-06-29 13:57:02CDT-0500 America/Chicago

    * Fixed a bug in ping where a call with no arguments would die, which was
      especially critical since the API itself calls ping regularly to help keep
      the connection alive (and/or notice when it is not). HT: jsime

0.151570    June 6, 2015

    * Initial release of base API.

lib/AnyEvent/SlackRTM.pm  view on Meta::CPAN


        delete $self->{finished};
        $self->{started}++;
        $self->{id} = 1;

        my $conn = $self->{conn} = $client->recv;
        $conn->on( each_message => sub { $self->_handle_incoming(@_) } );
        $conn->on( finish       => sub { $self->_handle_finish(@_) } );

        my $started = localtime;
        $self->{_last_keep_alive} = time;
        $self->{keep_alive}       = AnyEvent->timer(
            after    => 15,
            interval => 15,
            cb       => sub {
                my $id    = $self->{id};
                my $now   = time;
                my $since = $now - $self->{_last_keep_alive};
                if ( $since > 30 ) {
                    # will trigger a finish, which will reconnect
                    # if $self->{closed} is not set.
                    $conn->close;
                }
                elsif ( $since > 10 ) {
                    $self->ping( { keep_alive => $now } );
                }
            },
        );
    } );
}


sub metadata { shift->{metadata} // {} }
sub quiet {
    my $self = shift;

lib/AnyEvent/SlackRTM.pm  view on Meta::CPAN

    my ($self, $conn, $raw) = @_;

    my $msg = try {
        decode_json($raw->body);
    }
    catch {
        my $message = $raw->body;
        croak "unable to decode incoming message: $message";
    };

    $self->{_last_keep_alive} = time;

    # Handle errors when they occur
    if ($msg->{error}) {
        $self->_handle_error($conn, $msg);
    }

    # Handle the initial hello
    elsif ($msg->{type} eq 'hello') {
        $self->_handle_hello($conn, $msg);
    }

lib/AnyEvent/SlackRTM.pm  view on Meta::CPAN


sub _handle_other {
    my ($self, $conn, $msg) = @_;

    $self->_do($msg->{type}, $msg);
}

sub _handle_finish {
    my ($self, $conn) = @_;

    # Cancel the keep_alive watchdog
    undef $self->{keep_alive};

    $self->{finished}++;

    $self->_do('finish');

    $self->start unless $self->{closed};
}


sub close {

lib/AnyEvent/SlackRTM.pm  view on Meta::CPAN

    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";
            $rtm->ping;
        });

        $counter = AnyEvent->timer(interval => 5, cb => sub {
            print "Send\n";
            $rtm->send({
                type => 'message',
                channel => $channel_id,
                text => "".$i++,

lib/AnyEvent/SlackRTM.pm  view on Meta::CPAN

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

=head2 start

    method start()

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

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

Sets up a "keep alive" timer,
which triggers every 15 seconds to send a C<ping> message
if there hasn't been any activity in the past 10 seconds.
The C<ping> will trigger a C<pong> response,
so there should be at least one message every 15 seconds.
This will disconnect if no messages have been received in the past 30 seconds;
however, it should trigger an automatic reconnect to keep the connection alive.

=head2 metadata

    method metadata() returns HashRef

The initial connection is established after calling the
L<rtm.connect|https://api.slack.com/methods/rtm.connect> method on the web API.
This returns some useful information, which is available here.

This will only contain useful information I<after> L</start> is called.



( run in 0.498 second using v1.01-cache-2.11-cpan-df04353d9ac )