| 1 | package Langertha::Raider::Hall::Telegram; | ||||||||||||||||||||||||||||||
| 2 | 73 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Internal multi-bot Telegram long-poll adapter for the raider hall | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 73 | ▶ | use Moose; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 73 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 73 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 73 | ▶ | use URI; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 73 | ▶ | use HTTP::Request::Common (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 73 | ▶ | use IO::Async::Timer::Countdown; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 73 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 73 | ▶ | use Net::Async::HTTP; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | 73 | ▶ | use Langertha::Raider::ConnectCheck qw( connect_error ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 73 | has hall => ( | |||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||||
| 18 | isa => 'Langertha::Raider::Hall', | ||||||||||||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||||||||||||
| 20 | weak_ref => 1, | ||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | has _workers => ( | ||||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||||
| 25 | 19 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 26 | 73 | ); | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 73 | has _history_dir => ( | |||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||||||||||||
| 31 | builder => '_build_history_dir', | ||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||
| 34 | 8 | ▶ | sub _build_history_dir { | ||||||||||||||||||||||||||||
|
Subroutine
_build_history_dir: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 35 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 36 | 8 | my $d = $self->hall->state_dir->child('telegram'); | |||||||||||||||||||||||||||||
| 37 | 8 | ▶ | $d->mkpath unless -d $d; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 38 | 8 | $d; | |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub setup_bots { | ||||||||||||||||||||||||||||
|
Subroutine
setup_bots: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 42 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $conf = $self->hall->config->{telegram} // {}; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$self->hall->config->{"telegram"} // {}
MC/DC0%0/1
$self->hall->config->{"telegram"} // {}
$self->hall->config->{"telegram"}
Truth table0%0/2
A
A= $self->hall->config->{"telegram"}
| |||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $bots = $conf->{bots} // {}; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$conf->{"bots"} // {}
MC/DC0%0/1
$conf->{"bots"} // {}
$conf->{"bots"}
Truth table0%0/2
A
A= $conf->{"bots"}
| |||||||||||||||||||||||||||||||
| 45 | 0 | for my $name (keys %$bots) { | |||||||||||||||||||||||||||||
| 46 | 0 | $self->_start_bot($name, $bots->{$name}); | |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub _start_bot { | ||||||||||||||||||||||||||||
|
Subroutine
_start_bot: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||
| 51 | 1 | my ($self, $name, $bot_conf) = @_; | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | my $token = $bot_conf->{token} // return; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$bot_conf->{"token"} // (return)
MC/DC0%0/1
$bot_conf->{"token"} // (return)
$bot_conf->{"token"}
Truth table50%1/2
A
A= $bot_conf->{"token"}
| |||||||||||||||||||||||||||||||
| 53 | 1 | $self->_check_bot_config($name, $bot_conf); | |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | my $allowlist = $bot_conf->{allowlist} // []; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$bot_conf->{"allowlist"} // []
MC/DC0%0/1
$bot_conf->{"allowlist"} // []
$bot_conf->{"allowlist"}
Truth table50%1/2
A
A= $bot_conf->{"allowlist"}
| |||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | my $allowed_chats = $bot_conf->{allowed_chats} // []; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$bot_conf->{"allowed_chats"} // []
MC/DC0%0/1
$bot_conf->{"allowed_chats"} // []
$bot_conf->{"allowed_chats"}
Truth table50%1/2
A
A= $bot_conf->{"allowed_chats"}
| |||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my $routing = $bot_conf->{routing} // {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$bot_conf->{"routing"} // {}
MC/DC0%0/1
$bot_conf->{"routing"} // {}
$bot_conf->{"routing"}
Truth table50%1/2
A
A= $bot_conf->{"routing"}
| |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | 1 | my $ua = Net::Async::HTTP->new( | |||||||||||||||||||||||||||||
| 59 | max_connections_per_host => 1, | ||||||||||||||||||||||||||||||
| 60 | timeout => 60, | ||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||
| 62 | 1 | $self->hall->loop->add($ua); | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 1 | $self->_workers->{$name} = { | |||||||||||||||||||||||||||||
| 65 | token => $token, | ||||||||||||||||||||||||||||||
| 66 | allowlist => $allowlist, | ||||||||||||||||||||||||||||||
| 67 | allowed_chats => $allowed_chats, | ||||||||||||||||||||||||||||||
| 68 | routing => $routing, | ||||||||||||||||||||||||||||||
| 69 | ua => $ua, | ||||||||||||||||||||||||||||||
| 70 | offset => 0, | ||||||||||||||||||||||||||||||
| 71 | active => 1, | ||||||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 1 | $self->_poll($name); | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | 4 | ▶ | sub _check_bot_config { | ||||||||||||||||||||||||||||
|
Subroutine
_check_bot_config: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||
| 78 | 4 | my ($self, $name, $bot_conf) = @_; | |||||||||||||||||||||||||||||
| 79 | 4 | ▶ | my @allowlist = @{ $bot_conf->{allowlist} // [] }; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$bot_conf->{"allowlist"} // []
MC/DC100%1/1
$bot_conf->{"allowlist"} // []
$bot_conf->{"allowlist"}
Truth table100%2/2
A
A= $bot_conf->{"allowlist"}
| |||||||||||||||||||||||||||||||
| 80 | 4 | my @problems; | |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | push @problems, 'empty allowlist, every message will be rejected' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | unless @allowlist; | ||||||||||||||||||||||||||||||
| 83 | # Telegram group/channel ids are negative, user ids positive. | ||||||||||||||||||||||||||||||
| 84 | push @problems, 'allowlist entry '.$_.' is a chat id, not a user id;' | ||||||||||||||||||||||||||||||
| 85 | .' list it under allowed_chats and put the members\' user ids in allowlist' | ||||||||||||||||||||||||||||||
| 86 | 4 | for grep { /^-/ } @allowlist; | |||||||||||||||||||||||||||||
| 87 | 4 | for my $problem (@problems) { | |||||||||||||||||||||||||||||
| 88 | 2 | warn 'Telegram bot '.$name.': '.$problem."\n"; | |||||||||||||||||||||||||||||
| 89 | 2 | $self->hall->_emit('telegram.config_warning', { bot => $name, warning => $problem }); | |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | 18 | ▶ | sub _reject_reason { | ||||||||||||||||||||||||||||
|
Subroutine
_reject_reason: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||
| 94 | 18 | my ($self, $worker, $msg) = @_; | |||||||||||||||||||||||||||||
| 95 | 18 | ▶ | my $from_id = $msg->{from}{id} // return 'no_sender'; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$msg->{"from"}{"id"} // (return "no_sender")
MC/DC100%1/1
$msg->{"from"}{"id"} // (return "no_sender")
$msg->{"from"}{"id"}
Truth table100%2/2
A
A= $msg->{"from"}{"id"}
| |||||||||||||||||||||||||||||||
| 96 | 17 | my $chat_id = $msg->{chat}{id}; | |||||||||||||||||||||||||||||
| 97 | return 'sender_not_allowed' | ||||||||||||||||||||||||||||||
| 98 | 17 | ▶ | unless grep { $_ eq $from_id } @{ $worker->{allowlist} // [] }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$worker->{"allowlist"} // []
MC/DC100%1/1
$worker->{"allowlist"} // []
$worker->{"allowlist"}
Truth table100%2/2
A
A= $worker->{"allowlist"}
| |||||||||||||||||||||||||||||||
| 99 | 12 | ▶ | return if $chat_id eq $from_id; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 100 | return 'chat_not_allowed' | ||||||||||||||||||||||||||||||
| 101 | 7 | ▶ | unless grep { $_ eq $chat_id } @{ $worker->{allowed_chats} // [] }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$worker->{"allowed_chats"} // []
MC/DC100%1/1
$worker->{"allowed_chats"} // []
$worker->{"allowed_chats"}
Truth table100%2/2
A
A= $worker->{"allowed_chats"}
| |||||||||||||||||||||||||||||||
| 102 | 6 | return; | |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | # Why no request can reach the Bot API, or undef. A module that fails to | ||||||||||||||||||||||||||||||
| 106 | # load while Net::Async::HTTP connects keeps the host's only connection | ||||||||||||||||||||||||||||||
| 107 | # slot taken, and every later request waits forever (karr k107). | ||||||||||||||||||||||||||||||
| 108 | 4 | ▶ | sub _connect_error { connect_error('https://api.telegram.org') } | ||||||||||||||||||||||||||||
|
Subroutine
_connect_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | sub _poll { | ||||||||||||||||||||||||||||
|
Subroutine
_poll: called CC=5 SCAR=28.5
| |||||||||||||||||||||||||||||||
| 111 | 1 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | my $worker = $self->_workers->{$name} or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | return unless $worker->{active}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||
| 115 | # A module that does not load now will not later: stop polling, loudly. | ||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | if (my $error = $self->_connect_error) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 117 | 1 | $worker->{active} = 0; | |||||||||||||||||||||||||||||
| 118 | 1 | warn 'Telegram bot '.$name.': '.$error."\n"; | |||||||||||||||||||||||||||||
| 119 | 1 | $self->hall->_emit('telegram.poll_error', { bot => $name, error => $error }); | |||||||||||||||||||||||||||||
| 120 | 1 | return; | |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||
| 123 | 0 | my $ua = $worker->{ua}; | |||||||||||||||||||||||||||||
| 124 | 0 | my $token = $worker->{token}; | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | 0 | my $uri = URI->new("https://api.telegram.org/bot$token/getUpdates"); | |||||||||||||||||||||||||||||
| 127 | 0 | my %q = (timeout => 30); | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $q{offset} = $worker->{offset} if $worker->{offset}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 129 | 0 | $uri->query_form(%q); | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | 0 | my $f = $ua->GET($uri); | |||||||||||||||||||||||||||||
| 132 | 0 | $worker->{poll_future} = $f; | |||||||||||||||||||||||||||||
| 133 | 0 | weaken(my $weak = $self); | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $f->on_done(sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $self = $weak or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 136 | 0 | delete $worker->{poll_future}; | |||||||||||||||||||||||||||||
| 137 | 0 | my ($resp) = @_; | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $updates = eval { | ||||||||||||||||||||||||||||
|
Condition0%0/2
eval { // []
MC/DC0%0/1
eval {
do {
"JSON::MaybeXS"->new->decode($resp->decoded_content)->{"result"} // []
}
} // []
eval {
do {
"JSON::MaybeXS"->new->decode($resp->decoded_content)->{"result"} // []
}
}
Truth table0%0/2
A
A= eval {
do {
"JSON::MaybeXS"->new->decode($resp->decoded_content)->{"result"} // []
}
}
| |||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | JSON::MaybeXS->new->decode($resp->decoded_content)->{result} // [] | ||||||||||||||||||||||||||||
|
Condition0%0/2
"JSON::MaybeXS"->new->decode($resp->decoded_content)->{"result"} // []
MC/DC0%0/1
"JSON::MaybeXS"->new->decode($resp->decoded_content)->{"result"} // []
"JSON::MaybeXS"->new->decode($resp->decoded_content)->{"result"}
Truth table0%0/2
A
A= "JSON::MaybeXS"->new->decode($resp->decoded_content)->{"result"}
| |||||||||||||||||||||||||||||||
| 140 | } // []; | ||||||||||||||||||||||||||||||
| 141 | 0 | for my $update (@$updates) { | |||||||||||||||||||||||||||||
| 142 | 0 | $self->_handle_update($name, $update); | |||||||||||||||||||||||||||||
| 143 | 0 | $worker->{offset} = $update->{update_id} + 1; | |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $self->hall->loop->later(sub { $self->_poll($name) }); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 146 | 0 | }); | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $f->on_fail(sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my $self = $weak or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 149 | 0 | delete $worker->{poll_future}; | |||||||||||||||||||||||||||||
| 150 | 0 | $self->hall->_emit('telegram.poll_error', { bot => $name, error => "$_[0]" }); | |||||||||||||||||||||||||||||
| 151 | # Back off a bit on failure so we don't hot-loop against a dead network. | ||||||||||||||||||||||||||||||
| 152 | my $timer = IO::Async::Timer::Countdown->new( | ||||||||||||||||||||||||||||||
| 153 | delay => 5, | ||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | on_expire => sub { $self->_poll($name) }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 155 | 0 | ); | |||||||||||||||||||||||||||||
| 156 | 0 | $self->hall->loop->add($timer); | |||||||||||||||||||||||||||||
| 157 | 0 | $timer->start; | |||||||||||||||||||||||||||||
| 158 | 0 | }); | |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | 18 | ▶ | sub _handle_update { | ||||||||||||||||||||||||||||
|
Subroutine
_handle_update: called CC=16 SCAR=28.9
| |||||||||||||||||||||||||||||||
| 162 | 18 | my ($self, $bot_name, $update) = @_; | |||||||||||||||||||||||||||||
| 163 | 18 | ▶ | my $worker = $self->_workers->{$bot_name} or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 164 | 18 | ▶ | my $msg = $update->{message} // $update->{edited_message} // return; | ||||||||||||||||||||||||||||
|
Condition40%2/5
($update->{"message"} // $update->{"edited_message"}) // (return)
$update->{"message"} // $update->{"edited_message"}
MC/DC0%0/2
($update->{"message"} // $update->{"edited_message"}) // (return)
$update->{"message"}
$update->{"edited_message"}
Truth table33%1/3
A // B
A= $update->{"message"}B= $update->{"edited_message"}
| |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | 18 | ▶ | my $chat_id = $msg->{chat}{id} // return; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$msg->{"chat"}{"id"} // (return)
MC/DC0%0/1
$msg->{"chat"}{"id"} // (return)
$msg->{"chat"}{"id"}
Truth table50%1/2
A
A= $msg->{"chat"}{"id"}
| |||||||||||||||||||||||||||||||
| 167 | 18 | ▶ | my $text = $msg->{text} // ''; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$msg->{"text"} // ""
MC/DC0%0/1
$msg->{"text"} // ""
$msg->{"text"}
Truth table50%1/2
A
A= $msg->{"text"}
| |||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | 18 | ▶ | if (my $reason = $self->_reject_reason($worker, $msg)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 170 | $self->hall->_emit('telegram.rejected', { | ||||||||||||||||||||||||||||||
| 171 | bot => $bot_name, | ||||||||||||||||||||||||||||||
| 172 | chat_id => $chat_id, | ||||||||||||||||||||||||||||||
| 173 | from_id => $msg->{from}{id}, | ||||||||||||||||||||||||||||||
| 174 | reason => $reason, | ||||||||||||||||||||||||||||||
| 175 | update_id => $update->{update_id}, | ||||||||||||||||||||||||||||||
| 176 | 7 | }); | |||||||||||||||||||||||||||||
| 177 | 7 | return; | |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | 11 | my $routing = $worker->{routing}; | |||||||||||||||||||||||||||||
| 181 | 11 | ▶ | my $target_raider = $routing->{$chat_id} // $routing->{'*'} // undef; | ||||||||||||||||||||||||||||
|
Condition40%2/5
($routing->{$chat_id} // $routing->{"*"}) // undef
$routing->{$chat_id} // $routing->{"*"}
MC/DC0%0/2
($routing->{$chat_id} // $routing->{"*"}) // undef
$routing->{$chat_id}
$routing->{"*"}
Truth table33%1/3
A // B
A= $routing->{$chat_id}B= $routing->{"*"}
| |||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||
| 183 | $self->hall->_emit('telegram.in', { | ||||||||||||||||||||||||||||||
| 184 | bot => $bot_name, | ||||||||||||||||||||||||||||||
| 185 | chat_id => $chat_id, | ||||||||||||||||||||||||||||||
| 186 | defined $msg->{message_thread_id} ? ( message_thread_id => $msg->{message_thread_id} ) : (), | ||||||||||||||||||||||||||||||
| 187 | text => $text, | ||||||||||||||||||||||||||||||
| 188 | first_name => $msg->{from}{first_name} // '', | ||||||||||||||||||||||||||||||
| 189 | username => $msg->{from}{username} // '', | ||||||||||||||||||||||||||||||
| 190 | update_id => $update->{update_id}, | ||||||||||||||||||||||||||||||
| 191 | 11 | ▶ | }); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$msg->{"from"}{"first_name"} // ""
$msg->{"from"}{"username"} // ""
MC/DC100%1/1
$msg->{"from"}{"first_name"} // ""
$msg->{"from"}{"first_name"}
MC/DC100%1/1
$msg->{"from"}{"username"} // ""
$msg->{"from"}{"username"}
Truth table100%2/2
A
A= $msg->{"from"}{"first_name"}
Truth table100%2/2
A
A= $msg->{"from"}{"username"}
| |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||
| 193 | 11 | $self->_save_history($bot_name, $chat_id, $update); | |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||
| 195 | # One session per chat (per forum topic), continued by every message. | ||||||||||||||||||||||||||||||
| 196 | 11 | my $thread = $msg->{message_thread_id}; | |||||||||||||||||||||||||||||
| 197 | 11 | ▶ | my %target = ( | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 198 | bot => $bot_name, | ||||||||||||||||||||||||||||||
| 199 | chat_id => $chat_id, | ||||||||||||||||||||||||||||||
| 200 | defined $thread ? ( message_thread_id => $thread ) : (), | ||||||||||||||||||||||||||||||
| 201 | ); | ||||||||||||||||||||||||||||||
| 202 | 11 | ▶ | my $binding = join(':', 'telegram', $bot_name, $chat_id, defined $thread ? $thread : ()); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||
| 204 | 11 | ▶ | return $self->_start_new_session($binding, %target) if $self->_is_new_command($text); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||
| 206 | 9 | ▶ | if ($target_raider) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 207 | 9 | $self->hall->spawn( | |||||||||||||||||||||||||||||
| 208 | name => $target_raider, | ||||||||||||||||||||||||||||||
| 209 | mission => $text, | ||||||||||||||||||||||||||||||
| 210 | telegram => \%target, | ||||||||||||||||||||||||||||||
| 211 | binding => $binding, | ||||||||||||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||
| 216 | # /new, or /new@BOTNAME in a group. | ||||||||||||||||||||||||||||||
| 217 | 11 | ▶ | sub _is_new_command { | ||||||||||||||||||||||||||||
|
Subroutine
_is_new_command: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 218 | 11 | my ($self, $text) = @_; | |||||||||||||||||||||||||||||
| 219 | 11 | return $text =~ m{\A\s*/new(?:\@\w+)?\s*\z}; | |||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||
| 222 | # /new: the chat's next message starts a new session. Not a mission; | ||||||||||||||||||||||||||||||
| 223 | # answered with a short confirmation into the same chat and topic. | ||||||||||||||||||||||||||||||
| 224 | 2 | ▶ | sub _start_new_session { | ||||||||||||||||||||||||||||
|
Subroutine
_start_new_session: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 225 | 2 | my ($self, $binding, %target) = @_; | |||||||||||||||||||||||||||||
| 226 | 2 | $self->hall->reset_session($binding); | |||||||||||||||||||||||||||||
| 227 | 2 | $self->send_message(%target, text => 'Started a new session.'); | |||||||||||||||||||||||||||||
| 228 | 2 | return; | |||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||
| 231 | 11 | ▶ | sub _save_history { | ||||||||||||||||||||||||||||
|
Subroutine
_save_history: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 232 | 11 | my ($self, $bot_name, $chat_id, $update) = @_; | |||||||||||||||||||||||||||||
| 233 | 11 | my $dir = $self->_history_dir->child($bot_name); | |||||||||||||||||||||||||||||
| 234 | 11 | ▶ | $dir->mkpath unless -d $dir; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 235 | 11 | my $file = $dir->child("$chat_id.json"); | |||||||||||||||||||||||||||||
| 236 | 11 | ▶ | my $history = eval { JSON::MaybeXS->new->decode($file->slurp_utf8) } // []; | ||||||||||||||||||||||||||||
|
Condition100%2/2
eval { // []
MC/DC100%1/1
eval {
do {
"JSON::MaybeXS"->new->decode($file->slurp_utf8)
}
} // []
eval {
do {
"JSON::MaybeXS"->new->decode($file->slurp_utf8)
}
}
Truth table100%2/2
A
A= eval {
do {
"JSON::MaybeXS"->new->decode($file->slurp_utf8)
}
}
| |||||||||||||||||||||||||||||||
| 237 | 11 | push @$history, $update; | |||||||||||||||||||||||||||||
| 238 | 11 | $file->spew_utf8(JSON::MaybeXS->new->encode($history)); | |||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||
| 241 | 3 | ▶ | sub send_message { | ||||||||||||||||||||||||||||
|
Subroutine
send_message: called CC=7 SCAR=20.5
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 242 | 3 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||
| 243 | 3 | ▶ | my $bot_name = $args{bot} // return { error => 'bot name required' }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"bot"} // (return {"error", "bot name required"})
MC/DC0%0/1
$args{"bot"} // (return {"error", "bot name required"})
$args{"bot"}
Truth table50%1/2
A
A= $args{"bot"}
| |||||||||||||||||||||||||||||||
| 244 | 3 | ▶ | my $chat_id = $args{chat_id} // return { error => 'chat_id required' }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"chat_id"} // (return {"error", "chat_id required"})
MC/DC0%0/1
$args{"chat_id"} // (return {"error", "chat_id required"})
$args{"chat_id"}
Truth table50%1/2
A
A= $args{"chat_id"}
| |||||||||||||||||||||||||||||||
| 245 | 3 | ▶ | my $text = $args{text} // return { error => 'text required' }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"text"} // (return {"error", "text required"})
MC/DC0%0/1
$args{"text"} // (return {"error", "text required"})
$args{"text"}
Truth table50%1/2
A
A= $args{"text"}
| |||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||
| 247 | 3 | ▶ | my $worker = $self->_workers->{$bot_name} or return { error => "bot $bot_name not running" }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 248 | 3 | my $token = $worker->{token}; | |||||||||||||||||||||||||||||
| 249 | 3 | my $ua = $worker->{ua}; | |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||
| 251 | 3 | ▶ | if (my $error = $self->_connect_error) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 252 | 1 | return { error => $error }; | |||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||
| 255 | 2 | my $uri = URI->new("https://api.telegram.org/bot$token/sendMessage"); | |||||||||||||||||||||||||||||
| 256 | my $req = HTTP::Request::Common::POST($uri, [ | ||||||||||||||||||||||||||||||
| 257 | chat_id => $chat_id, | ||||||||||||||||||||||||||||||
| 258 | 2 | ▶ | defined $args{message_thread_id} ? ( message_thread_id => $args{message_thread_id} ) : (), | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 259 | text => $text, | ||||||||||||||||||||||||||||||
| 260 | parse_mode => 'Markdown', | ||||||||||||||||||||||||||||||
| 261 | ]); | ||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||
| 263 | # Fire-and-forget: return the Future so callers can await if they want. | ||||||||||||||||||||||||||||||
| 264 | 2 | my $f = $ua->do_request(request => $req); | |||||||||||||||||||||||||||||
| 265 | 2 | my $id = time . '-' . int(rand(1_000_000)); | |||||||||||||||||||||||||||||
| 266 | 2 | $worker->{send_futures}{$id} = $f; | |||||||||||||||||||||||||||||
| 267 | 2 | weaken(my $weak = $self); | |||||||||||||||||||||||||||||
| 268 | 2 | ▶ | $f->on_done(sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 269 | 2 | delete $worker->{send_futures}{$id}; | |||||||||||||||||||||||||||||
| 270 | 2 | }); | |||||||||||||||||||||||||||||
| 271 | 0 | ▶ | $f->on_fail(sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | my $self = $weak or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 273 | 0 | delete $worker->{send_futures}{$id}; | |||||||||||||||||||||||||||||
| 274 | 0 | $self->hall->_emit('telegram.send_error', { | |||||||||||||||||||||||||||||
| 275 | bot => $bot_name, chat_id => $chat_id, error => "$_[0]", | ||||||||||||||||||||||||||||||
| 276 | }); | ||||||||||||||||||||||||||||||
| 277 | 2 | }); | |||||||||||||||||||||||||||||
| 278 | 2 | return { ok => 1, future => $f }; | |||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||
| 281 | 1 | ▶ | sub stop { | ||||||||||||||||||||||||||||
|
Subroutine
stop: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 282 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 283 | 1 | $_->{active} = 0 for values %{$self->_workers}; | |||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||
| 286 | 73 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||
| 288 | 73 | 1; | |||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||
| 290 | __END__ | ||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||
| 292 | =pod | ||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||
| 294 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||
| 296 | =head1 NAME | ||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||
| 298 | Langertha::Raider::Hall::Telegram - Internal multi-bot Telegram long-poll adapter for the raider hall | ||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||
| 300 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||
| 302 | version 0.503 | ||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||
| 304 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||
| 306 | B<Internal module.> Its interface may change without notice. | ||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||
| 308 | Long-polls every Telegram bot configured for a L<Langertha::Raider::Hall>, | ||||||||||||||||||||||||||||||
| 309 | routes incoming messages to raiders and sends replies. | ||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||
| 311 | Access is fail-closed and checked per message: | ||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||
| 313 | telegram: | ||||||||||||||||||||||||||||||
| 314 | bots: | ||||||||||||||||||||||||||||||
| 315 | ops: | ||||||||||||||||||||||||||||||
| 316 | token: '...' | ||||||||||||||||||||||||||||||
| 317 | allowlist: [1234567890] # Telegram user ids (message.from.id) | ||||||||||||||||||||||||||||||
| 318 | allowed_chats: [-1001234567890] # optional: group chats to answer in | ||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||
| 320 | A message is accepted only when its sender (C<from.id>) is in C<allowlist> | ||||||||||||||||||||||||||||||
| 321 | B<and> its chat is either the private chat with that sender or listed in | ||||||||||||||||||||||||||||||
| 322 | C<allowed_chats>. An empty or missing C<allowlist> accepts nobody. Rejected | ||||||||||||||||||||||||||||||
| 323 | messages emit a C<telegram.rejected> event and are neither stored nor routed. | ||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||
| 325 | A routed message runs in the session bound to its chat -- | ||||||||||||||||||||||||||||||
| 326 | C<telegram:BOT:CHAT_ID>, with C<:THREAD> for a forum topic -- so the | ||||||||||||||||||||||||||||||
| 327 | conversation continues across messages (see | ||||||||||||||||||||||||||||||
| 328 | L<Langertha::Raider::Hall/session_bindings>). The raider's | ||||||||||||||||||||||||||||||
| 329 | C<telegram_reply> answers into the same chat and forum topic. | ||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||
| 331 | C</new> (or C</new@BOTNAME>) from an accepted sender is not a mission: the | ||||||||||||||||||||||||||||||
| 332 | chat's (topic's) next message starts a new session, and the bot confirms | ||||||||||||||||||||||||||||||
| 333 | with a short message. The old journal stays. | ||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||
| 335 | When a module the https connection to the Bot API needs does not load | ||||||||||||||||||||||||||||||
| 336 | (L<IO::Async::SSL> and what it needs, see L<Langertha::Raider::ConnectCheck>), | ||||||||||||||||||||||||||||||
| 337 | a bot does not poll: the hall warns and emits C<telegram.poll_error> with the | ||||||||||||||||||||||||||||||
| 338 | module's name, and a reply is an C<error> result. Nothing is sent, so | ||||||||||||||||||||||||||||||
| 339 | nothing waits for a connection that cannot open. | ||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||
| 341 | =head1 SUPPORT | ||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||
| 343 | =head2 Issues | ||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||
| 345 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||
| 346 | L<https://github.com/Getty/langertha-raider/issues>. | ||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||
| 348 | =head2 IRC | ||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||
| 350 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||
| 352 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||
| 354 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||
| 356 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||
| 358 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||
| 360 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||
| 362 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||
| 364 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||
| 365 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||
| 367 | =cut | ||||||||||||||||||||||||||||||