| 1 | package Bot::ChatBots::Telegram::LongPoll; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | { our $VERSION = '0.012'; } | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Ouch; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Log::Any qw< $log >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Mojo::IOLoop (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use IO::Socket::SSL (); # just to be sure to complain loudly in case | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use List::Util qw< max >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | with 'Bot::ChatBots::Telegram::Role::Source'; # normalize_record, token | |||||||||||||||||||||||||||
| 17 | 2 | with 'Bot::ChatBots::Role::Source'; # processor, typename | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has connect_timeout => ( | ||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | 1 | ▶ | default => sub { return 20 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | has interval => ( | ||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | default => sub { return 0.1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has max_redirects => ( | ||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | default => sub { return 5 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has _start => ( | ||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | default => sub { return 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 2 | init_arg => 'start', | |||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | has update_timeout => ( | ||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | default => sub { return 300 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 2 | ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub BUILD { | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | $self->start if $self->_start; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub class_custom_pairs { | ||||||||||||||||||||||||||||
| 51 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
class_custom_pairs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 1 | return (token => $self->token); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub parse_response { | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my ($self, $res, $threshold_id) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_response: called CC=5 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | my $data = $res->json // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$res->json // {}
MC/DC0%0/1
$res->json // {}
$res->json
Truth table50%1/2
A
A= $res->json
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if (!$data->{ok}) { # boolean flag from Telegram API | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | $log->error('getUpdates error: ' . | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $data->{description} // 'unknown error'); | ||||||||||||||||||||||||||
|
Condition0%0/2
'getUpdates error: ' . $data->{"description"} // "unknown error"
MC/DC0%0/1
'getUpdates error: ' . $data->{"description"} // "unknown error"
'getUpdates error: ' . $data->{"description"}
Truth table0%0/2
A
A= 'getUpdates error: ' . $data->{"description"}
| |||||||||||||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return grep { $_->{update_id} >= $threshold_id } @{$data->{result}//[]}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"result"} // []
MC/DC0%0/1
$data->{"result"} // []
$data->{"result"}
Truth table50%1/2
A
A= $data->{"result"}
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub poller { | ||||||||||||||||||||||||||||
| 68 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
poller: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | my $args = (@_ && ref($_[0])) ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | my $update_timeout = $self->update_timeout; | |||||||||||||||||||||||||||
| 72 | 1 | my %query = ( | |||||||||||||||||||||||||||
| 73 | offset => 0, | ||||||||||||||||||||||||||||
| 74 | telegram_method => 'getUpdates', | ||||||||||||||||||||||||||||
| 75 | timeout => $update_timeout, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | my $sender = $self->sender; | |||||||||||||||||||||||||||
| 79 | 1 | $sender->telegram->agent->connect_timeout($self->connect_timeout) | |||||||||||||||||||||||||||
| 80 | ->inactivity_timeout($update_timeout + 5) | ||||||||||||||||||||||||||||
| 81 | ->max_redirects($self->max_redirects); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # this flag tells us whether we're in a call already, avoiding | ||||||||||||||||||||||||||||
| 84 | # duplicates. It is set before sending a request, and reset when the | ||||||||||||||||||||||||||||
| 85 | # response is managed | ||||||||||||||||||||||||||||
| 86 | 1 | my $is_busy; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | my $on_data = sub { | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my ($ua, $tx) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=22.2
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | my @updates; | |||||||||||||||||||||||||||
| 92 | try { | ||||||||||||||||||||||||||||
| 93 | 1 | ▶ | @updates = $self->parse_response($tx->res, $query{offset}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | catch { | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $log->error(bleep $_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | die $_ if $self->should_rethrow($args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | }; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | my @retval = $self->process_updates( | |||||||||||||||||||||||||||
| 101 | refs => { | ||||||||||||||||||||||||||||
| 102 | sender => $sender, | ||||||||||||||||||||||||||||
| 103 | tx => $tx, | ||||||||||||||||||||||||||||
| 104 | ua => $ua, | ||||||||||||||||||||||||||||
| 105 | }, | ||||||||||||||||||||||||||||
| 106 | source_pairs => { | ||||||||||||||||||||||||||||
| 107 | query => \%query, | ||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | updates => \@updates, | ||||||||||||||||||||||||||||
| 110 | %$args, # may override it all! | ||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | for my $item (@retval) { | |||||||||||||||||||||||||||
| 114 | 1 | ▶ | next unless defined $item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | defined(my $record = $item->{record}) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 1 | ▶ | defined(my $outcome = $item->{outcome}) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | defined(my $message = $outcome->{send_response}) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $sender->send_message($message, record => $record); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # if we get here, somehow me managed to get past this call... Get | ||||||||||||||||||||||||||||
| 122 | # ready for the next one. Just to be on the safe side, we will | ||||||||||||||||||||||||||||
| 123 | # advance $query{offset} anyway | ||||||||||||||||||||||||||||
| 124 | 1 | ▶ | $query{offset} = 1 + max map { $_->{update_id} } @updates | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | if @updates; | ||||||||||||||||||||||||||||
| 126 | 1 | $is_busy = 0; | |||||||||||||||||||||||||||
| 127 | 1 | }; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | return sub { | ||||||||||||||||||||||||||||
| 130 | 1 | ▶ | return if $is_busy; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | $is_busy = 1; # $on_data below will reset $is_busy when ready | |||||||||||||||||||||||||||
| 132 | 1 | $sender->send_message(\%query, callback => $on_data); | |||||||||||||||||||||||||||
| 133 | 1 | }; | |||||||||||||||||||||||||||
| 134 | } ## end sub callback | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | around process => sub { | ||||||||||||||||||||||||||||
| 137 | 1 | ▶ | my ($orig, $self, $record) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 1 | my $outcome = $orig->($self, $record); | |||||||||||||||||||||||||||
| 139 | 1 | $record->{source}{query}{offset} = $record->{update}{update_id} + 1; | |||||||||||||||||||||||||||
| 140 | 1 | return $outcome; | |||||||||||||||||||||||||||
| 141 | 2 | }; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub start { | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 0 | Mojo::IOLoop->recurring($self->interval, $self->poller(@_)); | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | return $self; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | 1; | |||||||||||||||||||||||||||