Slack.pm

filter: Statementstmt 41.4% 87 / 210 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 49.0% 27 / 55 Podpod 70.0% 14 / 20 totaltotal 34.3% 128 / 373 CC 35 SCAR 27.1
CC
35
Cov
31%
send_message
90.0
__ANON__
20.0
__ANON__
20.0
CRAP 15.0
« Slack/EventType.pm ↑ Summary Slack/Socket.pm »
1 package Net::Async::Slack;
2 # ABSTRACT: Slack realtime messaging API support for IO::Async
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.006';
8  
9 1 ▶use parent qw(IO::Async::Notifier Net::Async::Slack::Commands);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Net::Async::Slack - support for the L<https://slack.com> APIs with L<IO::Async>
14  
15 =head1 SYNOPSIS
16  
17 use IO::Async::Loop;
18 use Net::Async::Slack;
19 my $loop = IO::Async::Loop->new;
20 $loop->add(
21 my $slack = Net::Async::Slack->new(
22 token => '...',
23 )
24 );
25  
26 =head1 DESCRIPTION
27  
28 This is a basic wrapper for Slack's API. It's an early version, the module API is likely
29 to change somewhat over time.
30  
31 See the C<< examples/ >> directory for usage.
32  
33 =cut
34  
35 1 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Dir::Self;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use URI::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 1 ▶use Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 1 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Ryu::Observable;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Net::Async::WebSocket::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶use Net::Async::OAuth::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 ▶use Net::Async::Slack::RTM;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Net::Async::Slack::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Net::Async::Slack::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 =head1 METHODS
65  
66 =cut
67  
68 =head2 rtm
69  
70 Establishes a connection to the Slack RTM websocket API, and
71 resolves to a L<Net::Async::Slack::RTM> instance.
72  
73 =cut
74  
75 sub rtm {
76 0 ▶ my ($self, %args) = @_;
Subroutine rtm: not called CC=2 SCAR=17.9
Pod documented
77 0 warn "RTM is deprecated and will no longer be supported by slack.com, please use socket mode instead: https://slack.com/apis/connections/socket";
78 0 $log->tracef('Endpoint is %s', $self->endpoint(
79 'rtm_connect',
80 ));
81 $self->{rtm} //= $self->http_get(
82 uri => URI->new(
83 $self->endpoint(
84 'rtm_connect',
85 )
86 ),
87 headers => {
88 Authorization => 'Bearer ' . $self->token
89 }
90 )->then(sub {
91 0 ▶ my $result = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
92 0 ▶ return Future->done(URI->new($result->{url})) if exists $result->{url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"url"} 0 0 0
93 0 return Future->fail('invalid URL');
94 })->then(sub {
95 0 ▶ my ($uri) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 $self->add_child(
97 my $rtm = Net::Async::Slack::RTM->new(
98 slack => $self,
99 wss_uri => $uri,
100 )
101 );
102 0 ▶ $rtm->connect->transform(done => sub { $rtm })
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 })
104 0 ▶}
Condition0%0/3
$self->{"rtm"} //= $self->http_get("uri", "URI"->new($self->endpoint("rtm_connect")), "headers", {"Authorization", 'Bearer ' . $self->token})->then(sub { my $result = shift(); return "Future"->done("URI"->new($result->{"url"})) if exists $result->{"url"}; return "Future"->fail("invalid URL"); } )->then(sub { my($uri) = @_; $self->add_child(my $rtm = "Net::Async::Slack::RTM"->new("slack", $self, "wss_uri", $uri)); $rtm->connect->transform("done", sub { $rtm; } ); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"rtm"} //= $self->http_get("uri", "URI"->new($self->endpoint("rtm_connect")), "headers", {"Authorization", 'Bearer ' . $self->token})->then(sub { my $result = shift(); return "Future"->done("URI"->new($result->{"url"})) if exists $result->{"url"}; return "Future"->fail("invalid URL"); } )->then(sub { my($uri) = @_; $self->add_child(my $rtm = "Net::Async::Slack::RTM"->new("slack", $self, "wss_uri", $uri)); $rtm->connect->transform("done", sub { $rtm; } ); } )
$self->{"rtm"} $self->http_get("uri", "URI"->new($self->endpoint("rtm_connect")), "headers", {"Authorization", 'Bearer ' . $self->token})->then(sub { my $result = shift(); return "Future"->done("URI"->new($result->{"url"})) if exists $result->{"url"}; return "Future"->fail("invalid URL"); } )->then(sub { my($uri) = @_; $self->add_child(my $rtm = "Net::Async::Slack::RTM"->new("slack", $self, "wss_uri", $uri)); $rtm->connect->transform("done", sub { $rtm; } ); } )
Truth table0%0/3
A //= B
A= $self->{"rtm"}B= $self->http_get("uri", "URI"->new($self->endpoint("rtm_connect")), "headers", {"Authorization", 'Bearer ' . $self->token})->then(sub { my $result = shift(); return "Future"->done("URI"->new($result->{"url"})) if exists $result->{"url"}; return "Future"->fail("invalid URL"); } )->then(sub { my($uri) = @_; $self->add_child(my $rtm = "Net::Async::Slack::RTM"->new("slack", $self, "wss_uri", $uri)); $rtm->connect->transform("done", sub { $rtm; } ); } )
ABresult
1X1
011
000
105  
106 0 ▶async sub socket_mode {
Subroutine socket_mode: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my ($self, %args) = @_;
108 0 my ($uri) = await $self->socket;
109 0 $self->add_child(
110 my $socket = Net::Async::Slack::Socket->new(
111 slack => $self,
112 wss_uri => $uri,
113 )
114 );
115 0 await $socket->connect;
116 0 return $socket;
117 1 }
118  
119 =head2 send_message
120  
121 Send a message to a user or channel.
122  
123 Supports the following named parameters:
124  
125 =over 4
126  
127 =item * channel - who to send the message to, can be a channel ID or C<< #channel >> name, or user ID
128  
129 =item * text - the message, see L<https://api.slack.com/docs/message-formatting> for details
130  
131 =item * attachments - more advanced messages, see L<https://api.slack.com/docs/message-attachments>
132  
133 =item * parse - whether to parse content and convert things like links
134  
135 =back
136  
137 and the following named boolean parameters:
138  
139 =over 4
140  
141 =item * link_names - convert C<< @user >> and C<< #channel >> to links
142  
143 =item * unfurl_links - show preview for URLs
144  
145 =item * unfurl_media - show preview for things that look like media links
146  
147 =item * as_user - send as user
148  
149 =item * reply_broadcast - send to all users when replying to a thread
150  
151 =back
152  
153 Returns a L<Future>, although the content of the response is subject to change.
154  
155 =cut
156  
157 0 ▶async sub send_message {
Subroutine send_message: not called CC=9 SCAR=45.0
Pod documented
158 0 my ($self, %args) = @_;
159 0 ▶ die 'You need to pass either text or attachments' unless $args{text} || $args{attachments};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"text"} or $args{"attachments"} 0 0 0
Condition0%0/3
$args{"text"} or $args{"attachments"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"text"} or $args{"attachments"}
$args{"text"} $args{"attachments"}
Truth table0%0/3
A or B
A= $args{"text"}B= $args{"attachments"}
ABresult
1X1
011
000
160 0 my @content;
161 0 push @content, token => $self->token;
162 0 ▶ push @content, channel => $args{channel} || die 'need a channel';
Condition0%0/2
$args{'channel'} || die('need a channel')
l!l
00
MC/DC0%0/1
$args{'channel'} || die('need a channel')
$args{'channel'}
Truth table0%0/2
A
A= $args{'channel'}
Aresult
11
00
163 0 ▶ push @content, text => $args{text} if defined $args{text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"text"} 0 0 0
164 0 ▶ push @content, attachments => encode_json_text($args{attachments}) if $args{attachments};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"attachments"} 0 0 0
165 0 ▶ push @content, blocks => encode_json_text($args{blocks}) if $args{blocks};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"blocks"} 0 0 0
166 0 push @content, $_ => $args{$_} for grep exists $args{$_}, qw(parse link_names unfurl_links unfurl_media as_user reply_broadcast thread_ts);
167 0 my ($data) = await $self->http_post(
168 $self->endpoint(
169 'chat.postMessage',
170 ),
171 \@content,
172 );
173 0 ▶ Future::Exception->throw('send failed', slack => $data) unless $data->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"ok"} 0 0 0
174 return Net::Async::Slack::Message->new(
175 slack => $self,
176 channel => $data->{channel},
177 thread_ts => $data->{ts},
178 0 );
179 1 }
180  
181 =head2 conversations_info
182  
183 Provide information about a channel.
184  
185 Takes the following named parameters:
186  
187 =over 4
188  
189 =item * C<channel> - the channel ID to look up
190  
191 =back
192  
193 and returns a L<Future> which will resolve to a hashref containing
194 C<< { channel => { name => '...' } } >>.
195  
196 =cut
197  
198 sub conversations_info {
199 0 ▶ my ($self, %args) = @_;
Subroutine conversations_info: not called CC=2 SCAR=17.9
Pod documented
200 0 my @content;
201 0 push @content, token => $self->token;
202 0 ▶ push @content, channel => $args{channel} || die 'need a channel';
Condition0%0/2
$args{'channel'} || die('need a channel')
l!l
00
MC/DC0%0/1
$args{'channel'} || die('need a channel')
$args{'channel'}
Truth table0%0/2
A
A= $args{'channel'}
Aresult
11
00
203 0 return $self->http_post(
204 $self->endpoint(
205 'conversations.info',
206 ),
207 \@content,
208 )
209 }
210  
211 0 ▶async sub conversations_history {
Subroutine conversations_history: not called CC=1 SCAR=6.9
Pod documented
212 0 my ($self, %args) = @_;
213 0 return await $self->http_get(
214 uri => $self->endpoint(
215 'conversations.history',
216 %args
217 ),
218 )
219 1 }
220  
221 =head2 join_channel
222  
223 Attempt to join the given channel.
224  
225 Takes the following named parameters:
226  
227 =over 4
228  
229 =item * C<channel> - the channel ID or name to join
230  
231 =back
232  
233 =cut
234  
235 sub join_channel {
236 0 ▶ my ($self, %args) = @_;
Subroutine join_channel: not called CC=2 SCAR=17.9
Pod documented
237 0 ▶ die 'You need to pass a channel name' unless $args{channel};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"channel"} 0 0 0
238 0 my @content;
239 0 push @content, token => $self->token;
240 0 push @content, name => $args{channel};
241 0 $self->http_post(
242 $self->endpoint(
243 'channels.join',
244 ),
245 \@content,
246 )
247 }
248  
249 =head1 METHODS - Internal
250  
251 =head2 endpoints
252  
253 Returns the hashref of API endpoints, loading them on first call from the C<share/endpoints.json> file.
254  
255 =cut
256  
257 sub endpoints {
258 0 ▶ my ($self) = @_;
Subroutine endpoints: not called CC=3 SCAR=24.8
Pod documented
259 0 ▶ $self->{endpoints} ||= do {
Condition0%0/3
$self->{"endpoints"} ||= do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-Slack", "endpoints.json")) unless $path->exists; $Net::Async::Slack::log->tracef("Loading endpoints from %s", $path); decode_json_text($path->slurp_utf8) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"endpoints"} ||= do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-Slack", "endpoints.json")) unless $path->exists; $Net::Async::Slack::log->tracef("Loading endpoints from %s", $path); decode_json_text($path->slurp_utf8) }
$self->{"endpoints"} do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-Slack", "endpoints.json")) unless $path->exists; $Net::Async::Slack::log->tracef("Loading endpoints from %s", $path); decode_json_text($path->slurp_utf8) }
Truth table0%0/3
A ||= B
A= $self->{"endpoints"}B= do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-Slack", "endpoints.json")) unless $path->exists; $Net::Async::Slack::log->tracef("Loading endpoints from %s", $path); decode_json_text($path->slurp_utf8) }
ABresult
1X1
011
000
260 0 my $path = Path::Tiny::path(__DIR__)->parent(3)->child('share/endpoints.json');
261 0 ▶ $path = Path::Tiny::path(
Branch: 1 branch
BranchTrueFalseTotal
unless $path->exists 0 0 0
262 File::ShareDir::dist_file(
263 'Net-Async-Slack',
264 'endpoints.json'
265 )
266 ) unless $path->exists;
267 0 $log->tracef('Loading endpoints from %s', $path);
268 0 decode_json_text($path->slurp_utf8)
269 };
270 }
271  
272 0 ▶sub slack_host { shift->{slack_host} }
Subroutine slack_host: not called CC=1 SCAR=6.9
Pod undocumented
273  
274 =head2 endpoint
275  
276 Processes the given endpoint as a template, using the named parameters
277 passed to the method.
278  
279 =cut
280  
281 sub endpoint {
282 0 ▶ my ($self, $endpoint, %args) = @_;
Subroutine endpoint: not called CC=2 SCAR=17.9
Pod documented
283 0 my $uri = URI::Template->new($self->endpoints->{$endpoint})->process(%args);
284 0 ▶ $uri->host($self->slack_host) if $self->slack_host;
Branch: 1 branch
BranchTrueFalseTotal
if $self->slack_host 0 0 0
285 0 $uri
286 }
287  
288 sub oauth {
289 0 ▶ my ($self) = @_;
Subroutine oauth: not called CC=2 SCAR=17.9
Pod undocumented
290 0 ▶ $self->{oauth} //= Net::Async::OAuth::Client->new(
Condition0%0/3
$self->{"oauth"} //= "Net::Async::OAuth::Client"->new("realm", "Slack", "consumer_key", $self->key, "consumer_secret", $self->secret, "token", $self->token, "token_secret", $self->token_secret)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"oauth"} //= "Net::Async::OAuth::Client"->new("realm", "Slack", "consumer_key", $self->key, "consumer_secret", $self->secret, "token", $self->token, "token_secret", $self->token_secret)
$self->{"oauth"} "Net::Async::OAuth::Client"->new("realm", "Slack", "consumer_key", $self->key, "consumer_secret", $self->secret, "token", $self->token, "token_secret", $self->token_secret)
Truth table0%0/3
A //= B
A= $self->{"oauth"}B= "Net::Async::OAuth::Client"->new("realm", "Slack", "consumer_key", $self->key, "consumer_secret", $self->secret, "token", $self->token, "token_secret", $self->token_secret)
ABresult
1X1
011
000
291 realm => 'Slack',
292 consumer_key => $self->key,
293 consumer_secret => $self->secret,
294 token => $self->token,
295 token_secret => $self->token_secret,
296 )
297 }
298  
299 0 ▶sub client_id { shift->{client_id} }
Subroutine client_id: not called CC=1 SCAR=6.9
Pod documented
300  
301 =head2 oauth_request
302  
303 =cut
304  
305 sub oauth_request {
306 1 ▶ use Bytes::Random::Secure qw(random_string_from);
Subroutine BEGIN: called CC=1 SCAR=0.0
307 1 ▶ use namespace::clean qw(random_string_from);
Subroutine BEGIN: called CC=1 SCAR=0.0
308 0 ▶ my ($self, $code, %args) = @_;
Subroutine oauth_request: not called CC=1 SCAR=6.9
Pod documented
309  
310 0 my $state = random_string_from('abcdefghjklmnpqrstvwxyz0123456789', 32);
311  
312 0 my $uri = $self->endpoint(
313 'oauth',
314 client_id => $self->client_id,
315 scope => 'bot,channels:write',
316 state => $state,
317 %args,
318 );
319 0 $log->debugf("OAuth URI endpoint is %s", "$uri");
320 $code->($uri)->then(sub {
321 0 ▶ Future->done;
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 })
323 0 }
324  
325 =head2 token
326  
327 API token.
328  
329 =cut
330  
331 0 ▶sub token { shift->{token} }
Subroutine token: not called CC=1 SCAR=6.9
Pod documented
332  
333 0 ▶sub app_token { shift->{app_token} }
Subroutine app_token: not called CC=1 SCAR=6.9
Pod undocumented
334  
335 =head2 http
336  
337 Returns the HTTP instance used for communicating with the API.
338  
339 Currently autocreates a L<Net::Async::HTTP> instance.
340  
341 =cut
342  
343 sub http {
344 0 ▶ my ($self) = @_;
Subroutine http: not called CC=2 SCAR=17.9
Pod documented
345 0 ▶ $self->{http} ||= do {
Condition0%0/3
$self->{"http"} ||= do { require Net::Async::HTTP; $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "close_after_request", 1, "max_connections_per_host", 2, "pipeline", 0, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::Slack; TEAM\@cpan.org)")); $ua }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"http"} ||= do { require Net::Async::HTTP; $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "close_after_request", 1, "max_connections_per_host", 2, "pipeline", 0, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::Slack; TEAM\@cpan.org)")); $ua }
$self->{"http"} do { require Net::Async::HTTP; $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "close_after_request", 1, "max_connections_per_host", 2, "pipeline", 0, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::Slack; TEAM\@cpan.org)")); $ua }
Truth table0%0/3
A ||= B
A= $self->{"http"}B= do { require Net::Async::HTTP; $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "close_after_request", 1, "max_connections_per_host", 2, "pipeline", 0, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::Slack; TEAM\@cpan.org)")); $ua }
ABresult
1X1
011
000
346 0 require Net::Async::HTTP;
347 0 $self->add_child(
348 my $ua = Net::Async::HTTP->new(
349 fail_on_error => 1,
350 close_after_request => 1,
351 max_connections_per_host => 2,
352 pipeline => 0,
353 max_in_flight => 8,
354 decode_content => 1,
355 timeout => 30,
356 user_agent => 'Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::Slack; TEAM@cpan.org)',
357 )
358 );
359 0 $ua
360 }
361 }
362  
363 =head2 http_get
364  
365 Issues an HTTP GET request.
366  
367 =cut
368  
369 sub http_get {
370 0 ▶ my ($self, %args) = @_;
Subroutine http_get: not called CC=1 SCAR=6.9
Pod documented
371  
372 0 my $uri = delete $args{uri};
373 0 $log->tracef("GET %s { %s }", "$uri", \%args);
374 $self->http->GET(
375 $uri,
376 %args
377 )->then(sub {
378 0 ▶ my ($resp) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
379 0 ▶ return { } if $resp->code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->code == 204 0 0 0
380 0 ▶ return { } if 3 == ($resp->code / 100);
Branch: 1 branch
BranchTrueFalseTotal
if 3 == $resp->code / 100 0 0 0
381 try {
382 0 $log->tracef('HTTP response for %s was %s', "$uri", $resp->as_string("\n"));
383 0 return Future->done(decode_json_utf8($resp->content))
384 0 } catch {
385 0 $log->errorf("JSON decoding error %s from HTTP response %s", $@, $resp->as_string("\n"));
386 0 return Future->fail($@ => json => $resp);
387 }
388 })->else(sub {
389 0 ▶ my ($err, $src, $resp, $req) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
390 0 ▶ $src //= '';
Condition0%0/2
$src //= ""
l!l
00
MC/DC0%0/1
$src //= ""
$src
Truth table0%0/2
A
A= $src
Aresult
11
00
391 0 ▶ if($src eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($src eq 'http') { } 0 0 0
392 0 $log->errorf("HTTP error %s, request was %s with response %s", $err, $req->as_string("\n"), $resp->as_string("\n"));
393 } else {
394 0 ▶ $log->errorf("Other failure (%s): %s", $src // 'unknown', $err);
Condition0%0/2
$src // "unknown"
l!l
00
MC/DC0%0/1
$src // "unknown"
$src
Truth table0%0/2
A
A= $src
Aresult
11
00
395 }
396 0 Future->fail(@_);
397 })
398 0 }
399  
400 sub auth_headers {
401 0 ▶ my ($self) = @_;
Subroutine auth_headers: not called CC=2 SCAR=17.9
Pod undocumented
402 0 ▶ return {} unless $self->token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->token 0 0 0
403 return {
404 0 Authorization => 'Bearer ' . $self->token
405 }
406 }
407  
408 =head2 http_post
409  
410 Issues an HTTP POST request.
411  
412 =cut
413  
414 sub http_post {
415 0 ▶ my ($self, $uri, $content, %args) = @_;
Subroutine http_post: not called CC=3 SCAR=24.8
Pod documented
416  
417 0 $log->tracef("POST %s { %s } <= %s", "$uri", \%args, $content);
418  
419 0 ▶ $args{headers} ||= $self->auth_headers;
Condition0%0/3
$args{"headers"} ||= $self->auth_headers
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"headers"} ||= $self->auth_headers
$args{"headers"} $self->auth_headers
Truth table0%0/3
A ||= B
A= $args{"headers"}B= $self->auth_headers
ABresult
1X1
011
000
420 0 ▶ if(ref $content eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "HASH") 0 0 0
421 0 $content = encode_json_utf8($content);
422 0 $args{content_type} = 'application/json';
423 }
424 $self->http->POST(
425 $uri,
426 $content,
427 %args,
428 )->then(sub {
429 0 ▶ my ($resp) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
430 0 ▶ return { } if $resp->code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->code == 204 0 0 0
431 0 ▶ return { } if 3 == ($resp->code / 100);
Branch: 1 branch
BranchTrueFalseTotal
if 3 == $resp->code / 100 0 0 0
432 try {
433 0 $log->tracef('HTTP response for %s was %s', "$uri", $resp->as_string("\n"));
434 0 return Future->done(decode_json_utf8($resp->content))
435 0 } catch {
436 0 $log->errorf("JSON decoding error %s from HTTP response %s", $@, $resp->as_string("\n"));
437 0 return Future->fail($@ => json => $resp);
438 }
439 })->else(sub {
440 0 ▶ my ($err, $src, $resp, $req) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
441 0 ▶ $src //= '';
Condition0%0/2
$src //= ""
l!l
00
MC/DC0%0/1
$src //= ""
$src
Truth table0%0/2
A
A= $src
Aresult
11
00
442 0 ▶ if($src eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($src eq 'http') { } 0 0 0
443 0 $log->errorf("HTTP error %s, request was %s with response %s", $err, $req->as_string("\n"), $resp->as_string("\n"));
444 } else {
445 0 ▶ $log->errorf("Other failure (%s): %s", $src // 'unknown', $err);
Condition0%0/2
$src // "unknown"
l!l
00
MC/DC0%0/1
$src // "unknown"
$src
Truth table0%0/2
A
A= $src
Aresult
11
00
446 }
447 0 Future->fail(@_);
448 })
449 0 }
450  
451 0 ▶async sub socket {
Subroutine socket: not called CC=3 SCAR=24.8
Pod undocumented
452 0 my ($self) = @_;
453 0 ▶ my $uri = $self->endpoint(
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->endpoint("apps_connections_open") or die "no endpoint" 0 0 0
454 'apps_connections_open',
455 ) or die 'no endpoint';
456 0 my $res = await $self->http_post(
457 $uri,
458 [ ],
459 headers => {
460 Authorization => 'Bearer ' . $self->app_token
461 }
462 );
463 0 ▶ die 'failed to obtain socket-mode URL' unless $res->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"ok"} 0 0 0
464 0 return URI->new($res->{url});
465 1 }
466  
467 sub configure {
468 0 ▶ my ($self, %args) = @_;
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
469 0 for my $k (qw(client_id token app_token slack_host)) {
470 0 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 0 0 0
471 }
472 0 $self->next::method(%args);
473 }
474  
475 1 1;
476  
477 =head1 SEE ALSO
478  
479 =over 4
480  
481 =item * L<AnyEvent::SlackRTM> - low-level API wrapper around RTM
482  
483 =item * L<Mojo::SlackRTM> - another RTM-specific wrapper, this time based on Mojolicious
484  
485 =item * L<Slack::RTM::Bot> - more RTM support, this time via LWP and a subprocess/thread for handling the websocket part
486  
487 =item * L<WebService::Slack::WebApi> - Furl-based wrapper around the REST API
488  
489 =item * L<AnyEvent::SlackBot> - another AnyEvent RTM implementation
490  
491 =back
492  
493 =head1 AUTHOR
494  
495 Tom Molesworth <TEAM@cpan.org>
496  
497 =head1 LICENSE
498  
499 Copyright Tom Molesworth 2016-2021. Licensed under the same terms as Perl itself.
500  
« Slack/EventType.pm ↑ Summary Slack/Socket.pm »