Mojo::SlackRTM - non-blocking SlackRTM client using Mojo::IOLoop
use Mojo::SlackRTM;
# get from https://api.slack.com/web#authentication
my $token = "xoxb-12345678901-AbCdEfGhIjKlMnoPqRsTuVWx";
my $slack = Mojo::SlackRTM->new(token => $token);
$slack->on(message => sub {
my ($slack, $event) = @_;
my $channel_id = $event->{channel};
my $user_id = $event->{user};
my $user_name = $slack->find_user_name($user_id);
my $text = $event->{text};
$slack->send_message($channel_id => "hello $user_name!");
});
$slack->start;
Mojo::SlackRTM is a non-blocking SlackRTM client using Mojo::IOLoop.
This class inherits all events, methods, attributes from Mojo::EventEmitter.
There are a lot of events, eg, hello, message, user_typing, channel_marked, ....
See https://api.slack.com/rtm for details.
$slack->on(reaction_added => sub {
my ($slack, $event) = @_;
my $reaction = $event->{reaction};
my $user_id = $event->{user};
my $user_name = $slack->find_user_name($user_id);
$slack->log->info("$user_name reacted with $reaction");
});
$slack->call_api($method);
$slack->call_api($method, $param);
$slack->call_api($method, $cb);
$slack->call_api($method, $param, $cb);
Call slack web api. See https://api.slack.com/methods for details.
$slack->call_api("channels.list", {exclude_archived => 1}, sub {
my ($slack, $tx) = @_;
if ($tx->success and $tx->res->json("/ok")) {
my $channels = $tx->res->json("/channels");
$slack->log->info($_->{name}) for @$channels;
return;
}
my $error = $tx->success ? $tx->res->json("/error") : $tx->error->{message};
$slack->log->error($error);
});
$slack->connect;
my $id = $slack->find_channel_id($name);
my $name = $slack->find_channel_name($id);
my $id = $slack->find_user_id($name);
my $name = $slack->find_user_name($id);
$slack->finish;
my $id = $slack->next_id;
$slack->ping;
$slack->reconnect;
$slack->send_message($channel => $text);
Send $text
to slack $channel
via the websocket transaction.
$slack->start;
This is a convenient method. In fact it is equivalent to:
$slack->connect;
$slack->ioloop->start unless $slack->ioloop->is_running;
Automatically reconnect to slack
Mojo::IOLoop singleton
Mojo::Log instance
The response of rtm.start. See https://api.slack.com/methods/rtm.start for details.
slack access token
Mojo::UserAgent instance
Websocket transaction
Set MOJO_SLACKRTM_DEBUG=1
.
http://perladvent.org/2015/2015-12-23.html
Shoichi Kaji skaji@cpan.org
Copyright 2016 Shoichi Kaji skaji@cpan.org
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.