AnyEvent-SlackRTM

 view release on metacpan or  search on metacpan

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

package AnyEvent::SlackRTM;
$AnyEvent::SlackRTM::VERSION = '1.3';
use v5.14;

# ABSTRACT: AnyEvent module for interacting with the Slack RTM API

use AnyEvent;
use AnyEvent::WebSocket::Client 0.12;
use Carp;
use Furl;
use JSON;
use Try::Tiny;

our $START_URL = 'https://slack.com/api/rtm.connect';


sub new {
    my ($class, $token, $client_opts) = @_;

    $client_opts //= {};
    croak "Client options must be passed as a HashRef" unless ref $client_opts eq 'HASH';

    my $client;
    try {
        $client = AnyEvent::WebSocket::Client->new(%$client_opts);
    } catch {
        croak "Can't create client object: $_";
    };

    return bless {
        token    => $token,
        client   => $client,
        registry => {},
    }, $class;
}


sub start {
    my $self = shift;

    use vars qw( $VERSION );
    $VERSION //= '*-devel';

    my $furl = Furl->new(
        agent   => "AnyEvent::SlackRTM/$VERSION",
        timeout => $self->{client}->timeout,
    );

    my $res = $furl->get($START_URL . '?token=' . $self->{token});
    my $start = try {
        decode_json($res->content);
    }
    catch {
        my $status = $res->status;
        my $message = $res->content;
        croak "unable to start, Slack call failed: $status $message";
    };

    my $ok  = $start->{ok};
    croak "unable to start, Slack returned an error: $start->{error}"
    unless $ok;

    # Store this stuff in case we want it
    $self->{metadata} = $start;

    # We've now asked to re-open the connection,
    # so don't close again on timeout.
    delete $self->{closed};

    $self->{client}->connect( $start->{url} )->cb( sub {
        my $client = shift;



( run in 1.120 second using v1.01-cache-2.11-cpan-39bf76dae61 )