| 1 | package Bot::ChatBots::Telegram::Sender; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Ouch; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | { our $VERSION = '0.014'; } | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use WWW::Telegram::BotAPI (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | with 'Bot::ChatBots::Role::Sender'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has start_loop => ( | ||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||
| 16 | 4 | ▶ | default => sub { return 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has telegram => ( | ||||||||||||||||||||||||||||
| 20 | is => 'rw', | ||||||||||||||||||||||||||||
| 21 | lazy => 1, | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 24 | 0 | my $tg = WWW::Telegram::BotAPI->new( | |||||||||||||||||||||||||||
| 25 | token => $self->token, | ||||||||||||||||||||||||||||
| 26 | async => 1, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | 0 | return $tg; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 4 | ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 4 | has token => ( | |||||||||||||||||||||||||||
| 33 | is => 'ro', | ||||||||||||||||||||||||||||
| 34 | required => 1, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # copied from Bot::ChatBot::Role::UserAgent | ||||||||||||||||||||||||||||
| 38 | 1 | ▶ | sub may_start_loop { | ||||||||||||||||||||||||||
|
Subroutine
may_start_loop: called CC=6 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | 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]
| |||||||||||||||||||||||||||||
| 41 | my $start_loop = | ||||||||||||||||||||||||||||
| 42 | exists($args{start_loop}) | ||||||||||||||||||||||||||||
| 43 | ? $args{start_loop} | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | : $self->start_loop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | Mojo::IOLoop->start if $start_loop && (!Mojo::IOLoop->is_running); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$start_loop and not "Mojo::IOLoop"->is_running
MC/DC0%0/2
$start_loop and not "Mojo::IOLoop"->is_running
$start_loop
not "Mojo::IOLoop"->is_running
Truth table33%1/3
A and not(B)
A= $start_loopB= not "Mojo::IOLoop"->is_running
| |||||||||||||||||||||||||||||
| 46 | 1 | return $self; | |||||||||||||||||||||||||||
| 47 | } ## end sub may_start_loop | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | ▶ | sub send_message { | ||||||||||||||||||||||||||
|
Subroutine
send_message: called CC=17 SCAR=31.6
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 5 | my ($self, $message) = splice @_, 0, 2; | |||||||||||||||||||||||||||
| 51 | 5 | ▶ | ouch 500, 'no output to send' unless defined $message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my %args = (@_ && ref($_[0])) ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table66%2/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # message normalization | ||||||||||||||||||||||||||||
| 56 | 5 | ▶ | $message = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | ref($message) | ||||||||||||||||||||||||||||
| 58 | ? {%$message} | ||||||||||||||||||||||||||||
| 59 | : {text => $message, telegram_method => 'sendMessage'}; | ||||||||||||||||||||||||||||
| 60 | 5 | ▶ | my $method = delete($message->{telegram_method}) // do { | ||||||||||||||||||||||||||
|
Condition66%2/3
delete $message->{"telegram_method"} // do {
state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"};
my $name = delete local $$message{"method"} // "send";
$method_for->{$name} or ouch(500, $self->name . ": unsupported method $name")
}
MC/DC0%0/2
delete $message->{"telegram_method"} // do {
state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"};
my $name = delete local $$message{"method"} // "send";
$method_for->{$name} or ouch(500, $self->name . ": unsupported method $name")
}
delete $message->{"telegram_method"}
do {
state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"};
my $name = delete local $$message{"method"} // "send";
$method_for->{$name} or ouch(500, $self->name . ": unsupported method $name")
}
Truth table66%2/3
A // B
A= delete $message->{"telegram_method"}B= do {
state $method_for = {"send", "sendMessage", "sendMessage", "sendMessage"};
my $name = delete local $$message{"method"} // "send";
$method_for->{$name} or ouch(500, $self->name . ": unsupported method $name")
}
| |||||||||||||||||||||||||||||
| 61 | 1 | state $method_for = { | |||||||||||||||||||||||||||
| 62 | send => 'sendMessage', | ||||||||||||||||||||||||||||
| 63 | sendMessage => 'sendMessage', | ||||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | my $name = delete(local $message->{method}) // 'send'; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete local $$message{"method"} // "send"
MC/DC0%0/1
delete local $$message{"method"} // "send"
delete local $$message{"method"}
Truth table50%1/2
A
A= delete local $$message{"method"}
| |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $method_for->{$name} | ||||||||||||||||||||||||||
|
Condition33%1/3
$method_for->{$name} || ouch(500, $self->name . ": unsupported method $name")
MC/DC0%0/2
$method_for->{$name} || ouch(500, $self->name . ": unsupported method $name")
$method_for->{$name}
ouch(500, $self->name . ": unsupported method $name")
Truth table33%1/3
A || B
A= $method_for->{$name}B= ouch(500, $self->name . ": unsupported method $name")
| |||||||||||||||||||||||||||||
| 68 | or ouch 500, $self->name . ": unsupported method $name"; | ||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 5 | ▶ | if (($method eq 'sendMessage') && (!exists $message->{chat_id})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$method eq "sendMessage" and not exists $message->{"chat_id"}
MC/DC100%2/2
$method eq "sendMessage" and not exists $message->{"chat_id"}
$method eq "sendMessage"
not exists $message->{"chat_id"}
Truth table100%3/3
A and not(B)
A= $method eq "sendMessage"B= not exists $message->{"chat_id"}
| |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if (defined $args{record}) { # take from $record | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 73 | 1 | $message->{chat_id} = $args{record}{channel}{id}; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | elsif ($self->has_recipient) { | ||||||||||||||||||||||||||||
| 76 | 1 | $message->{chat_id} = $self->recipient; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { # no more ways to figure it out | ||||||||||||||||||||||||||||
| 79 | 1 | ouch 500, 'no chat identifier for message'; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } ## end if (!exists $message->...) | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 4 | my @callback; | |||||||||||||||||||||||||||
| 84 | 4 | ▶ | if ($args{callback}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | 1 | @callback = $args{callback}; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | elsif ($self->can('callback')) { | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $has_callback = $self->can('has_callback') // sub { return 1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$self->can("has_callback") // sub {
return 1;
}
MC/DC0%0/2
$self->can("has_callback") // sub {
return 1;
}
$self->can("has_callback")
sub {
return 1;
}
Truth table0%0/3
A // B
A= $self->can("has_callback")B= sub {
return 1;
}
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | @callback = $self->callback if $has_callback->($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 4 | my $res = $self->telegram->api_request($method => $message, @callback); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | $self->may_start_loop(%args) if @callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | return $res; | |||||||||||||||||||||||||||
| 97 | } ## end sub send_message | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | 1; | |||||||||||||||||||||||||||