Slack.pm

filter: Statementstmt 34.9% 93 / 266 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 43.5% 27 / 62 Podpod 74.0% 20 / 27 totaltotal 28.6% 140 / 489 CC 54 SCAR 31.2
CC
54
Cov
25%
send_message
90.0
files_upload
72.0
http_get_paged
72.0
CRAP 22.6
« Slack/Message.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.015';
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 0 ▶sub rtm {
Subroutine rtm: not called CC=2 SCAR=17.9
Pod documented
76 0 my ($self, %args) = @_;
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 => $self->endpoint(
83 'rtm_connect',
84 ),
85 0 ▶ )->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 my $result = shift;
87 0 ▶ return Future->done(URI->new($result->{url})) if exists $result->{url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"url"} 0 0 0
88 0 return Future->fail('invalid URL');
89 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 my ($uri) = @_;
91 0 $self->add_child(
92 my $rtm = Net::Async::Slack::RTM->new(
93 slack => $self,
94 wss_uri => $uri,
95 )
96 );
97 0 ▶ $rtm->connect->transform(done => sub { $rtm })
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 })
99 0 ▶}
Condition0%0/3
$self->{"rtm"} //= $self->http_get("uri", $self->endpoint("rtm_connect"))->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", $self->endpoint("rtm_connect"))->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", $self->endpoint("rtm_connect"))->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", $self->endpoint("rtm_connect"))->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
100  
101 0 ▶async sub socket_mode {
Subroutine socket_mode: not called CC=1 SCAR=6.9
Pod undocumented
102 0 my ($self, %args) = @_;
103 0 my ($uri) = await $self->socket;
104 0 $self->add_child(
105 my $socket = Net::Async::Slack::Socket->new(
106 slack => $self,
107 wss_uri => $uri,
108 )
109 );
110 0 await $socket->connect;
111 0 return $socket;
112 1 }
113  
114 =head2 send_message
115  
116 Send a message to a user or channel.
117  
118 Supports the following named parameters:
119  
120 =over 4
121  
122 =item * channel - who to send the message to, can be a channel ID or C<< #channel >> name, or user ID
123  
124 =item * text - the message, see L<https://api.slack.com/docs/message-formatting> for details
125  
126 =item * attachments - more advanced messages, see L<https://api.slack.com/docs/message-attachments>
127  
128 =item * parse - whether to parse content and convert things like links
129  
130 =back
131  
132 and the following named boolean parameters:
133  
134 =over 4
135  
136 =item * link_names - convert C<< @user >> and C<< #channel >> to links
137  
138 =item * unfurl_links - show preview for URLs
139  
140 =item * unfurl_media - show preview for things that look like media links
141  
142 =item * as_user - send as user
143  
144 =item * reply_broadcast - send to all users when replying to a thread
145  
146 =back
147  
148 Returns a L<Future>, although the content of the response is subject to change.
149  
150 =cut
151  
152 0 ▶async sub send_message {
Subroutine send_message: not called CC=9 SCAR=45.0
Pod documented
153 0 my ($self, %args) = @_;
154 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
155 0 my @content;
156 0 push @content, token => $self->token;
157 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
158 0 ▶ push @content, text => $args{text} if defined $args{text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"text"} 0 0 0
159 0 ▶ push @content, attachments => encode_json_text($args{attachments}) if $args{attachments};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"attachments"} 0 0 0
160 0 ▶ push @content, blocks => encode_json_text($args{blocks}) if $args{blocks};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"blocks"} 0 0 0
161 0 push @content, $_ => $args{$_} for grep exists $args{$_}, qw(parse link_names unfurl_links unfurl_media as_user reply_broadcast thread_ts);
162 0 my ($data) = await $self->http_post(
163 $self->endpoint(
164 'chat_post_message',
165 ),
166 \@content,
167 );
168 0 ▶ Future::Exception->throw('send failed', slack => $data) unless $data->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"ok"} 0 0 0
169 return Net::Async::Slack::Message->new(
170 slack => $self,
171 channel => $data->{channel},
172 thread_ts => $data->{ts},
173 0 );
174 1 }
175  
176 =head2 files_upload
177  
178 Upload file(s) to a channel or thread.
179  
180 Supports the following named parameters:
181  
182 =over 4
183  
184 =item * channel - who to send the message to, can be a channel ID or C<< #channel >> name, or user ID
185  
186 =item * text - the message, see L<https://api.slack.com/docs/message-formatting> for details
187  
188 =item * attachments - more advanced messages, see L<https://api.slack.com/docs/message-attachments>
189  
190 =item * parse - whether to parse content and convert things like links
191  
192 =back
193  
194 and the following named boolean parameters:
195  
196 =over 4
197  
198 =item * link_names - convert C<< @user >> and C<< #channel >> to links
199  
200 =item * unfurl_links - show preview for URLs
201  
202 =item * unfurl_media - show preview for things that look like media links
203  
204 =item * as_user - send as user
205  
206 =item * reply_broadcast - send to all users when replying to a thread
207  
208 =back
209  
210 Returns a L<Future>, although the content of the response is subject to change.
211  
212 =cut
213  
214 0 ▶async sub files_upload {
Subroutine files_upload: not called CC=8 SCAR=42.8
Pod documented
215 0 my ($self, %args) = @_;
216 0 ▶ die 'You need to pass file name and content' unless length($args{filename} // '') and defined($args{content});
Branch: 1 branch
BranchTrueFalseTotal
unless length($args{"filename"} // "") and defined $args{"content"} 0 0 0
Condition0%0/5
length($args{"filename"} // "") and defined $args{"content"}
!ll&&!rl&&r
000
$args{"filename"} // ""
l!l
00
MC/DC0%0/2
length($args{"filename"} // "") and defined $args{"content"}
$args{"filename"} defined $args{"content"}
Truth table0%0/3
A and B
A= $args{"filename"}B= defined $args{"content"}
ABresult
0X0
100
111
217 0 my @content;
218 0 ▶ push @content, channels => $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
219 0 ▶ push @content, initial_comment => $args{text} if defined $args{text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"text"} 0 0 0
220 0 push @content, $_ => $args{$_} for grep exists $args{$_}, qw(filetype thread_ts title);
221 0 push @content, file => [ undef, $args{filename}, Content => $args{content} ];
222 0 my ($data) = await $self->http_post(
223 $self->endpoint(
224 'files_upload',
225 ),
226 \@content,
227 content_type => 'form-data',
228 );
229 0 ▶ Future::Exception->throw('send failed', slack => $data) unless $data->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"ok"} 0 0 0
230 0 return $data;
231 1 }
232  
233 =head2 conversations_info
234  
235 Provide information about a channel.
236  
237 Takes the following named parameters:
238  
239 =over 4
240  
241 =item * C<channel> - the channel ID to look up
242  
243 =back
244  
245 and returns a L<Future> which will resolve to a hashref containing
246 C<< { channel => { name => '...' } } >>.
247  
248 =cut
249  
250 0 ▶sub conversations_info {
Subroutine conversations_info: not called CC=2 SCAR=17.9
Pod documented
251 0 my ($self, %args) = @_;
252 0 my @content;
253 0 push @content, token => $self->token;
254 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
255 0 return $self->http_post(
256 $self->endpoint(
257 'conversations_info',
258 ),
259 \@content,
260 )
261 }
262  
263 0 ▶sub conversations_invite {
Subroutine conversations_invite: not called CC=3 SCAR=24.8
Pod documented
264 0 my ($self, %args) = @_;
265 0 ▶ my $chan = $args{channel} or die 'need a channel';
Branch: 1 branch
BranchTrueFalseTotal
my $chan = $args{"channel"} or die "need a channel" 0 0 0
266 0 ▶ my @users = ref($args{users}) ? $args{users}->@* : $args{users};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'users'} ? : 0 0 0
267 0 return $self->http_post(
268 $self->endpoint(
269 'conversations_invite',
270 ),
271 {
272 channel => $chan,
273 users => join(',', @users),
274 }
275 )
276 }
277  
278 0 ▶async sub users_list {
Subroutine users_list: not called CC=1 SCAR=6.9
Pod documented
279 0 my ($self, %args) = @_;
280 0 return await $self->http_get_paged(
281 key => 'members',
282 uri => $self->endpoint(
283 'users_list',
284 %args
285 ),
286 )
287 1 }
288 0 ▶async sub conversations_list {
Subroutine conversations_list: not called CC=1 SCAR=6.9
Pod documented
289 0 my ($self, %args) = @_;
290 0 return await $self->http_get(
291 uri => $self->endpoint(
292 'conversations_list',
293 %args
294 ),
295 )
296 1 }
297 0 ▶async sub conversations_history {
Subroutine conversations_history: not called CC=1 SCAR=6.9
Pod documented
298 0 my ($self, %args) = @_;
299 0 return await $self->http_get(
300 uri => $self->endpoint(
301 'conversations_history',
302 %args
303 ),
304 )
305 1 }
306  
307 =head2 join_channel
308  
309 Attempt to join the given channel.
310  
311 Takes the following named parameters:
312  
313 =over 4
314  
315 =item * C<channel> - the channel ID or name to join
316  
317 =back
318  
319 =cut
320  
321 0 ▶sub join_channel {
Subroutine join_channel: not called CC=2 SCAR=17.9
Pod documented
322 0 my ($self, %args) = @_;
323 0 ▶ die 'You need to pass a channel name' unless $args{channel};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"channel"} 0 0 0
324 0 my @content;
325 0 push @content, token => $self->token;
326 0 push @content, channel => $args{channel};
327 0 $self->http_post(
328 $self->endpoint(
329 'conversations_join',
330 ),
331 \@content,
332 )
333 }
334  
335 0 ▶async sub users_profile_get {
Subroutine users_profile_get: not called CC=1 SCAR=6.9
Pod documented
336 0 my ($self, %args) = @_;
337 0 return await $self->http_get(
338 uri => $self->endpoint(
339 'users_profile_get',
340 %args
341 ),
342 )
343 1 }
344  
345 0 ▶async sub workflows_update_step {
Subroutine workflows_update_step: not called CC=1 SCAR=6.9
Pod documented
346 0 my ($self, %args) = @_;
347 0 return await $self->http_post(
348 $self->endpoint(
349 'workflows_update_step',
350 ),
351 \%args,
352 )
353 1 }
354  
355 =head1 METHODS - Internal
356  
357 =head2 endpoints
358  
359 Returns the hashref of API endpoints, loading them on first call from the C<share/endpoints.json> file.
360  
361 =cut
362  
363 0 ▶sub endpoints {
Subroutine endpoints: not called CC=3 SCAR=24.8
Pod documented
364 0 my ($self) = @_;
365 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
366 0 my $path = Path::Tiny::path(__DIR__)->parent(3)->child('share/endpoints.json');
367 0 ▶ $path = Path::Tiny::path(
Branch: 1 branch
BranchTrueFalseTotal
unless $path->exists 0 0 0
368 File::ShareDir::dist_file(
369 'Net-Async-Slack',
370 'endpoints.json'
371 )
372 ) unless $path->exists;
373 0 $log->tracef('Loading endpoints from %s', $path);
374 0 decode_json_text($path->slurp_utf8)
375 };
376 }
377  
378 0 ▶sub slack_host { shift->{slack_host} }
Subroutine slack_host: not called CC=1 SCAR=6.9
Pod undocumented
379  
380 =head2 endpoint
381  
382 Processes the given endpoint as a template, using the named parameters
383 passed to the method.
384  
385 =cut
386  
387 0 ▶sub endpoint {
Subroutine endpoint: not called CC=2 SCAR=17.9
Pod documented
388 0 my ($self, $endpoint, %args) = @_;
389 0 my $uri = URI::Template->new($self->endpoints->{$endpoint})->process(%args);
390 0 ▶ $uri->host($self->slack_host) if $self->slack_host;
Branch: 1 branch
BranchTrueFalseTotal
if $self->slack_host 0 0 0
391 0 $uri
392 }
393  
394 0 ▶sub oauth {
Subroutine oauth: not called CC=2 SCAR=17.9
Pod undocumented
395 0 my ($self) = @_;
396 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
397 realm => 'Slack',
398 consumer_key => $self->key,
399 consumer_secret => $self->secret,
400 token => $self->token,
401 token_secret => $self->token_secret,
402 )
403 }
404  
405 0 ▶sub client_id { shift->{client_id} }
Subroutine client_id: not called CC=1 SCAR=6.9
Pod documented
406  
407 =head2 oauth_request
408  
409 =cut
410  
411 0 ▶sub oauth_request {
Subroutine oauth_request: not called CC=1 SCAR=1.2
Pod documented
412 1 ▶ use Bytes::Random::Secure qw(random_string_from);
Subroutine BEGIN: called CC=1 SCAR=0.0
413 1 ▶ use namespace::clean qw(random_string_from);
Subroutine BEGIN: called CC=1 SCAR=0.0
414 0 my ($self, $code, %args) = @_;
415  
416 0 my $state = random_string_from('abcdefghjklmnpqrstvwxyz0123456789', 32);
417  
418 0 my $uri = $self->endpoint(
419 'oauth',
420 client_id => $self->client_id,
421 scope => 'bot,channels:write',
422 state => $state,
423 %args,
424 );
425 0 $log->debugf("OAuth URI endpoint is %s", "$uri");
426 0 ▶ $code->($uri)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 0 Future->done;
428 })
429 0 }
430  
431 =head2 token
432  
433 API token.
434  
435 =cut
436  
437 0 ▶sub token { shift->{token} }
Subroutine token: not called CC=1 SCAR=6.9
Pod documented
438  
439 0 ▶sub app_token { shift->{app_token} }
Subroutine app_token: not called CC=1 SCAR=6.9
Pod undocumented
440  
441 =head2 http
442  
443 Returns the HTTP instance used for communicating with the API.
444  
445 Currently autocreates a L<Net::Async::HTTP> instance.
446  
447 =cut
448  
449 0 ▶sub http {
Subroutine http: not called CC=2 SCAR=17.9
Pod documented
450 0 my ($self) = @_;
451 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", 0, "max_connections_per_host", 4, "pipeline", 1, "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", 0, "max_connections_per_host", 4, "pipeline", 1, "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", 0, "max_connections_per_host", 4, "pipeline", 1, "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", 0, "max_connections_per_host", 4, "pipeline", 1, "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
452 0 require Net::Async::HTTP;
453 0 $self->add_child(
454 my $ua = Net::Async::HTTP->new(
455 fail_on_error => 1,
456 close_after_request => 0,
457 max_connections_per_host => 4,
458 pipeline => 1,
459 max_in_flight => 8,
460 decode_content => 1,
461 timeout => 30,
462 user_agent => 'Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::Slack; TEAM@cpan.org)',
463 )
464 );
465 0 $ua
466 }
467 }
468  
469 =head2 http_get
470  
471 Issues an HTTP GET request.
472  
473 =cut
474  
475 0 ▶sub http_get {
Subroutine http_get: not called CC=3 SCAR=24.8
Pod documented
476 0 my ($self, @args) = @_;
477 0 ▶ my %args = (@args == 1) ? (uri => @args) : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 0 0
478  
479 0 my $uri = delete $args{uri};
480 0 $log->tracef("GET %s { %s }", "$uri", \%args);
481 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
482 $self->http->GET(
483 $uri,
484 %args
485 0 ▶ )->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
486 0 my ($resp) = @_;
487 0 ▶ return { } if $resp->code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->code == 204 0 0 0
488 0 ▶ return { } if 3 == ($resp->code / 100);
Branch: 1 branch
BranchTrueFalseTotal
if 3 == $resp->code / 100 0 0 0
489 try {
490 0 $log->tracef('HTTP response for %s was %s', "$uri", $resp->as_string("\n"));
491 0 return Future->done(decode_json_utf8($resp->content))
492 0 } catch {
493 0 $log->errorf("JSON decoding error %s from HTTP response %s", $@, $resp->as_string("\n"));
494 0 return Future->fail($@ => json => $resp);
495 }
496 0 ▶ })->else(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
497 0 my ($err, $src, $resp, $req) = @_;
498 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
499 0 ▶ if($src eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($src eq 'http') { } 0 0 0
500 0 $log->errorf("HTTP error %s, request was %s with response %s", $err, $req->as_string("\n"), $resp->as_string("\n"));
501 } else {
502 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
503 }
504 0 Future->fail(@_);
505 })
506 0 }
507  
508 0 ▶async sub http_get_paged {
Subroutine http_get_paged: not called CC=8 SCAR=42.8
Pod undocumented
509 0 my ($self, %args) = @_;
510 my $key = delete $args{key}
511 0 ▶ or die 'need a hash key to find the results array in the response';
Branch: 1 branch
BranchTrueFalseTotal
my $key = delete $args{"key"} or die "need a hash key to find the results array in the response" 0 0 0
512 0 my $uri = delete $args{uri};
513 0 ▶ $uri = URI->new($uri) unless ref($uri);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri 0 0 0
514 0 ▶ $uri->query_param(limit => 500) unless $uri->query_param('limit');
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->query_param("limit") 0 0 0
515 0 my $data;
516 my $found;
517 0 my $offset;
518 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$found and $offset and do { do { my $res = <custom op await>; die $res unless $res->{"ok"}; $offset = $res->{"offset"}; $uri->query_param("offset", $offset); $found = 0 + @{$res->{$key}}; if ($data) { push @$data, @{$res->{$key}}; } else { $data = $res->{$key}; } } } 0 0 0
Condition0%0/3
$found and $offset
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $offset
$found $offset
Truth table0%0/3
A and B
A= $foundB= $offset
ABresult
0X0
100
111
519 0 my $res = await $self->http_get(uri => $uri, %args);
520 0 ▶ die $res unless $res->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"ok"} 0 0 0
521 0 $offset = $res->{offset};
522 0 $uri->query_param(offset => $offset);
523 0 $found = 0 + $res->{$key}->@*;
524 0 ▶ if($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) { } 0 0 0
525 0 push $data->@*, $res->{$key}->@*;
526 } else {
527 0 $data = $res->{$key};
528 }
529 } while $found and $offset;
530 0 return $data;
531 1 }
532  
533 0 ▶sub auth_headers {
Subroutine auth_headers: not called CC=2 SCAR=17.9
Pod undocumented
534 0 my ($self) = @_;
535 0 ▶ return {} unless $self->token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->token 0 0 0
536 return {
537 0 Authorization => 'Bearer ' . $self->token
538 }
539 }
540  
541 =head2 http_post
542  
543 Issues an HTTP POST request.
544  
545 =cut
546  
547 0 ▶sub http_post {
Subroutine http_post: not called CC=3 SCAR=24.8
Pod documented
548 0 my ($self, $uri, $content, %args) = @_;
549  
550 0 $log->tracef("POST %s { %s } <= %s", "$uri", \%args, $content);
551  
552 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
553 0 ▶ if(ref $content eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "HASH") 0 0 0
554 0 $content = encode_json_utf8($content);
555 0 $args{content_type} = 'application/json; charset=utf-8';
556 }
557 $self->http->POST(
558 $uri,
559 $content,
560 %args,
561 0 ▶ )->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
562 0 my ($resp) = @_;
563 0 ▶ return { } if $resp->code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->code == 204 0 0 0
564 0 ▶ return { } if 3 == ($resp->code / 100);
Branch: 1 branch
BranchTrueFalseTotal
if 3 == $resp->code / 100 0 0 0
565 try {
566 0 $log->tracef('HTTP response for %s was %s', "$uri", $resp->as_string("\n"));
567 0 return Future->done(decode_json_utf8($resp->content))
568 0 } catch {
569 0 $log->errorf("JSON decoding error %s from HTTP response %s", $@, $resp->as_string("\n"));
570 0 return Future->fail($@ => json => $resp);
571 }
572 0 ▶ })->else(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
573 0 my ($err, $src, $resp, $req) = @_;
574 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
575 0 ▶ if($src eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($src eq 'http') { } 0 0 0
576 0 $log->errorf("HTTP error %s, request was %s with response %s", $err, $req->as_string("\n"), $resp->as_string("\n"));
577 } else {
578 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
579 }
580 0 Future->fail(@_);
581 })
582 0 }
583  
584 0 ▶async sub socket {
Subroutine socket: not called CC=4 SCAR=30.0
Pod undocumented
585 0 my ($self) = @_;
586 0 my $target_uri = do {
587 0 ▶ my $uri = $self->endpoint(
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->endpoint("apps_connections_open") or die "no endpoint" 0 0 0
588 'apps_connections_open',
589 ) or die 'no endpoint';
590 0 my $res = await $self->http_post(
591 $uri,
592 [ ],
593 headers => {
594 Authorization => 'Bearer ' . $self->app_token
595 }
596 );
597 0 ▶ die 'failed to obtain socket-mode URL' unless $res->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"ok"} 0 0 0
598 0 URI->new($res->{url});
599 };
600 0 ▶ $target_uri->query_param(debug_reconnects => 'true') if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
601 0 return $target_uri;
602 1 }
603  
604 0 ▶sub configure {
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
605 0 my ($self, %args) = @_;
606 0 for my $k (qw(client_id token app_token slack_host debug)) {
607 0 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 0 0 0
608 }
609 0 $self->next::method(%args);
610 }
611  
612 1 1;
613  
614 =head1 SEE ALSO
615  
616 =over 4
617  
618 =item * L<AnyEvent::SlackRTM> - low-level API wrapper around RTM
619  
620 =item * L<Mojo::SlackRTM> - another RTM-specific wrapper, this time based on Mojolicious
621  
622 =item * L<Slack::RTM::Bot> - more RTM support, this time via LWP and a subprocess/thread for handling the websocket part
623  
624 =item * L<WebService::Slack::WebApi> - Furl-based wrapper around the REST API
625  
626 =item * L<AnyEvent::SlackBot> - another AnyEvent RTM implementation
627  
628 =back
629  
630 =head1 AUTHOR
631  
632 Tom Molesworth <TEAM@cpan.org>
633  
634 =head1 LICENSE
635  
636 Copyright Tom Molesworth 2016-2024. Licensed under the same terms as Perl itself.
637  
« Slack/Message.pm ↑ Summary Slack/Socket.pm »