Bot/Brain.pm

filter: Statementstmt 24.7% 78 / 315 Branchbran 10.1% 14 / 138 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 38.3% 18 / 47 Podpod 52.6% 10 / 19 totaltotal 18.9% 120 / 635 CC 108 SCAR 41.7
CC
108
Cov
17%
sendMessage
240.0
editMessageText
182.0
answerInlineQuery
90.0
CRAP 64.7
« Bot/Object/Base.pm ↑ Summary Bot/Object/InlineQuery.pm »
1 package Telegram::Bot::Brain;
2 2 $Telegram::Bot::Brain::VERSION = '0.029';
3 # ABSTRACT: A base class to make your very own Telegram bot
4  
5  
6 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Mojo::JSON qw/to_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Telegram::Bot::Object::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Telegram::Bot::Object::InlineQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Telegram::Bot::Object::CallbackQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Telegram::Bot::Object::ChatJoinRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Telegram::Bot::Object::ChatMemberUpdated;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Telegram::Bot::Object::ChatMember;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # base class for building telegram robots with Mojolicious
26 2 has longpoll_time => 60;
27 0 ▶has ua => sub { Mojo::UserAgent->new->inactivity_timeout(shift->longpoll_time + 15) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
28 0 ▶has token => sub { croak "you need to supply your own token"; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
29  
30 0 ▶has tasks => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
31 1 ▶has listeners => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 0 ▶has log => sub { Log::Any->get_logger };
Subroutine __ANON__: not called CC=1 SCAR=1.2
34  
35  
36 0 ▶sub add_repeating_task {
Subroutine add_repeating_task: not called CC=1 SCAR=6.9
Pod documented
37 0 my $self = shift;
38 0 my $seconds = shift;
39 0 my $task = shift;
40  
41 0 ▶ my $repeater = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
42  
43 # Perform operation every $seconds seconds
44 0 my $last_check = time();
45 0 ▶ Mojo::IOLoop->recurring(0.1 => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
46 0 my $loop = shift;
47 0 my $now = time();
48 0 ▶ return unless ($now - $last_check) >= $seconds;
Branch: 1 branch
BranchTrueFalseTotal
unless $now - $last_check >= $seconds 0 0 0
49 0 $last_check = $now;
50 0 $task->($self);
51 0 });
52 0 };
53  
54 # keep a copy
55 0 push @{ $self->tasks }, $repeater;
56  
57 # kick it off
58 0 $repeater->();
59 }
60  
61  
62 2 ▶sub add_listener {
Subroutine add_listener: called CC=1 SCAR=0.0
Pod documented
63 2 my $self = shift;
64 2 my $coderef = shift;
65  
66 2 push @{ $self->listeners }, $coderef;
67 }
68  
69 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
70 0 die "init was not overridden!";
71 }
72  
73  
74 0 ▶sub think {
Subroutine think: not called CC=2 SCAR=17.9
Pod documented
75 0 my $self = shift;
76 0 $self->init();
77  
78 0 $self->_add_getUpdates_handler;
79 0 ▶ Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
80 }
81  
82  
83  
84 0 ▶sub getMe {
Subroutine getMe: not called CC=2 SCAR=17.9
Pod documented
85 0 my $self = shift;
86 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
87  
88 0 my $url = "https://api.telegram.org/bot${token}/getMe";
89 0 my $api_response = $self->_post_request($url);
90  
91 0 return Telegram::Bot::Object::User->create_from_hash($api_response, $self);
92 }
93  
94  
95 0 ▶sub sendMessage {
Subroutine sendMessage: not called CC=15 SCAR=54.8
Pod documented
96 0 my $self = shift;
97 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
98  
99 0 my $send_args = {};
100 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
101 0 $send_args->{chat_id} = $args->{chat_id};
102  
103 0 ▶ croak "no text supplied" unless $args->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"text"} 0 0 0
104 0 $send_args->{text} = $args->{text};
105  
106 # these are optional, send if they are supplied
107 0 ▶ $send_args->{parse_mode} = $args->{parse_mode} if exists $args->{parse_mode};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"parse_mode"} 0 0 0
108 0 ▶ $send_args->{disable_web_page_preview} = $args->{disable_web_page_preview} if exists $args->{disable_web_page_preview};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"disable_web_page_preview"} 0 0 0
109 0 ▶ $send_args->{disable_notification} = $args->{disable_notification} if exists $args->{disable_notification};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"disable_notification"} 0 0 0
110 $send_args->{reply_parameters} = to_json($args->{reply_parameters}->as_hashref)
111 0 ▶ if exists $args->{reply_parameters};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"reply_parameters"} 0 0 0
112 # deprecated reply_to_message_id
113 0 ▶ if (exists $args->{reply_to_message_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"reply_to_message_id"}) 0 0 0
114 0 $send_args->{reply_parameters} = to_json({message_id => $args->{reply_to_message_id}});
115 }
116 # $send_args->{reply_to_message_id} = $args->{reply_to_message_id} if exists $args->{reply_to_message_id};
117  
118 # check reply_markup is the right kind
119 0 ▶ if (exists $args->{reply_markup}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"reply_markup"}) 0 0 0
120 0 my $reply_markup = $args->{reply_markup};
121 0 ▶ die "bad reply_markup supplied"
Branch: 1 branch
BranchTrueFalseTotal
if ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" and ref $reply_markup ne "Telegram::Bot::Object::ForceReply" 0 0 0
Condition0%0/9
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" and ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
!ll&&!rl&&r
000
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove"
!ll&&!rl&&r
000
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup"
!ll&&!rl&&r
000
MC/DC0%0/4
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" and ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
Truth table0%0/5
A and B and C and D
A= ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup"B= ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup"C= ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove"D= ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
122 if ( ref($reply_markup) ne 'Telegram::Bot::Object::InlineKeyboardMarkup' &&
123 ref($reply_markup) ne 'Telegram::Bot::Object::ReplyKeyboardMarkup' &&
124 ref($reply_markup) ne 'Telegram::Bot::Object::ReplyKeyboardRemove' &&
125 ref($reply_markup) ne 'Telegram::Bot::Object::ForceReply' );
126 0 $send_args->{reply_markup} = to_json($reply_markup->as_hashref);
127 }
128  
129 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
130 0 my $url = "https://api.telegram.org/bot${token}/sendMessage";
131 0 my $api_response = $self->_post_request($url, $send_args);
132  
133 0 return Telegram::Bot::Object::Message->create_from_hash($api_response, $self);
134 }
135  
136  
137 0 ▶sub forwardMessage {
Subroutine forwardMessage: not called CC=7 SCAR=40.3
Pod documented
138 0 my $self = shift;
139 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
140 0 my $send_args = {};
141 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
142 0 $send_args->{chat_id} = $args->{chat_id};
143  
144 0 ▶ croak "no from_chat_id supplied" unless $args->{from_chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"from_chat_id"} 0 0 0
145 0 $send_args->{from_chat_id} = $args->{from_chat_id};
146  
147 0 ▶ croak "no message_id supplied" unless $args->{message_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"message_id"} 0 0 0
148 0 $send_args->{message_id} = $args->{message_id};
149  
150 # these are optional, send if they are supplied
151 0 ▶ $send_args->{disable_notification} = $args->{disable_notification} if exists $args->{disable_notification};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"disable_notification"} 0 0 0
152  
153 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
154 0 my $url = "https://api.telegram.org/bot${token}/forwardMessage";
155 0 my $api_response = $self->_post_request($url, $send_args);
156  
157 0 return Telegram::Bot::Object::Message->create_from_hash($api_response, $self);
158 }
159  
160  
161 0 ▶sub deleteMessage {
Subroutine deleteMessage: not called CC=4 SCAR=30.0
Pod documented
162 0 my $self = shift;
163 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
164 0 my $send_args = {};
165 0 ▶ croak "no message_id supplied" unless $args->{message_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"message_id"} 0 0 0
166 0 $send_args->{message_id} = $args->{message_id};
167
168 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
169 0 my $url = "https://api.telegram.org/bot${token}/deleteMessage";
170 0 my $api_response = $self->_post_request($url, $send_args);
171
172 0 return $api_response;
173 }
174  
175 0 ▶sub editMessageText {
Subroutine editMessageText: not called CC=13 SCAR=52.0
Pod undocumented
176 0 my $self = shift;
177 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
178 0 my $send_args = {};
179 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
180 0 $send_args->{chat_id} = $args->{chat_id};
181 0 ▶ croak "no message_id supplied" unless $args->{message_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"message_id"} 0 0 0
182 0 $send_args->{message_id} = $args->{message_id};
183  
184 # these are optional, send if they are supplied
185 0 ▶ $send_args->{text} = $args->{text} if exists $args->{text};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"text"} 0 0 0
186 0 ▶ $send_args->{parse_mode} = $args->{parse_mode} if exists $args->{parse_mode};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"parse_mode"} 0 0 0
187 0 ▶ $send_args->{disable_web_page_preview} = $args->{disable_web_page_preview} if exists $args->{disable_web_page_preview};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"disable_web_page_preview"} 0 0 0
188  
189 0 ▶ if (exists $args->{reply_markup}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"reply_markup"}) 0 0 0
190 0 my $reply_markup = $args->{reply_markup};
191 0 ▶ die "bad reply_markup supplied"
Branch: 1 branch
BranchTrueFalseTotal
if ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" and ref $reply_markup ne "Telegram::Bot::Object::ForceReply" 0 0 0
Condition0%0/9
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" and ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
!ll&&!rl&&r
000
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove"
!ll&&!rl&&r
000
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup"
!ll&&!rl&&r
000
MC/DC0%0/4
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" and ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup" ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove" ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
Truth table0%0/5
A and B and C and D
A= ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup"B= ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup"C= ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardRemove"D= ref $reply_markup ne "Telegram::Bot::Object::ForceReply"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
192 if ( ref($reply_markup) ne 'Telegram::Bot::Object::InlineKeyboardMarkup' &&
193 ref($reply_markup) ne 'Telegram::Bot::Object::ReplyKeyboardMarkup' &&
194 ref($reply_markup) ne 'Telegram::Bot::Object::ReplyKeyboardRemove' &&
195 ref($reply_markup) ne 'Telegram::Bot::Object::ForceReply' );
196 0 $send_args->{reply_markup} = to_json($reply_markup->as_hashref);
197 }
198 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
199 0 my $url = "https://api.telegram.org/bot${token}/editMessageText";
200 0 my $api_response = $self->_post_request($url, $send_args);
201  
202 0 return Telegram::Bot::Object::Message->create_from_hash($api_response, $self);
203  
204 }
205  
206  
207 0 ▶sub sendPhoto {
Subroutine sendPhoto: not called CC=6 SCAR=37.4
Pod documented
208 0 my $self = shift;
209 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
210  
211 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
212  
213 # photo can be a string (which might be either a URL for telegram servers
214 # to fetch, or a file_id string) or a file on disk to upload - we need
215 # to handle that last case here as it changes the way we create the HTTP
216 # request
217 0 ▶ croak "no photo supplied" unless $args->{photo};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"photo"} 0 0 0
218 0 ▶ if (-e $args->{photo}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $args->{"photo"}) 0 0 0
219 0 $args->{photo} = { file => $args->{photo} };
220 }
221  
222 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
223 0 my $url = "https://api.telegram.org/bot${token}/sendPhoto";
224 0 my $api_response = $self->_post_request($url, $args);
225  
226 0 return Telegram::Bot::Object::Message->create_from_hash($api_response, $self);
227 }
228  
229  
230 0 ▶sub sendDocument {
Subroutine sendDocument: not called CC=6 SCAR=37.4
Pod documented
231 0 my $self = shift;
232 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
233 0 my $send_args = {};
234  
235 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
236 0 $send_args->{chat_id} = $args->{chat_id};
237  
238 # document can be a string (which might be either a URL for telegram servers
239 # to fetch, or a file_id string) or a file on disk to upload - we need
240 # to handle that last case here as it changes the way we create the HTTP
241 # request
242 0 ▶ croak "no document supplied" unless $args->{document};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"document"} 0 0 0
243 0 ▶ if (-e $args->{document}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $args->{'document'}) { } 0 0 0
244 0 $send_args->{document} = { document => { file => $args->{document} } };
245 }
246 else {
247 0 $send_args->{document} = $args->{document};
248 }
249  
250 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
251 0 my $url = "https://api.telegram.org/bot${token}/sendDocument";
252 0 my $api_response = $self->_post_request($url, $send_args);
253  
254 0 return Telegram::Bot::Object::Message->create_from_hash($api_response, $self);
255 }
256  
257  
258 0 ▶sub answerInlineQuery {
Subroutine answerInlineQuery: not called CC=9 SCAR=45.0
Pod documented
259 0 my $self = shift;
260 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
261 0 my $send_args = {};
262  
263 0 ▶ croak "no inline_query_id supplied" unless $args->{inline_query_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"inline_query_id"} 0 0 0
264 0 $send_args->{inline_query_id} = $args->{inline_query_id};
265  
266 0 ▶ croak "no results supplied" unless $args->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"results"} 0 0 0
267 0 $send_args->{results} = $args->{results};
268  
269 # these are optional, send if they are supplied
270 0 ▶ $send_args->{cache_time} = $args->{cache_time} if exists $args->{cache_time};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"cache_time"} 0 0 0
271 0 ▶ $send_args->{is_personal} = $args->{is_personal} if exists $args->{is_personal};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"is_personal"} 0 0 0
272 0 ▶ $send_args->{next_offset} = $args->{next_offset} if exists $args->{next_offset};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"next_offset"} 0 0 0
273 0 ▶ $send_args->{button} = $args->{button} if exists $args->{button};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"button"} 0 0 0
274  
275 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
276 0 my $url = "https://api.telegram.org/bot${token}/answerInlineQuery";
277 0 my $api_response = $self->_post_request($url, $send_args);
278  
279 0 return $api_response;
280 }
281  
282 0 ▶sub setMyCommands {
Subroutine setMyCommands: not called CC=6 SCAR=37.4
Pod undocumented
283 0 my $self = shift;
284 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
285
286 0 my $send_args = {};
287 0 ▶ croak "no commands supplied" unless $args->{commands};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"commands"} 0 0 0
288 0 $send_args->{commands} = to_json $args->{commands};
289 0 ▶ $send_args->{language_code} = $args->{language_code} if ($args->{language_code}//'') ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"language_code"} // "") ne "" 0 0 0
Condition0%0/2
$args->{"language_code"} // ""
l!l
00
MC/DC0%0/1
$args->{"language_code"} // ""
$args->{"language_code"}
Truth table0%0/2
A
A= $args->{"language_code"}
Aresult
11
00
290 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
291 0 my $url = "https://api.telegram.org/bot${token}/setMyCommands";
292 0 return $self->_post_request($url, $send_args);
293 }
294  
295 0 ▶sub answerCallbackQuery {
Subroutine answerCallbackQuery: not called CC=8 SCAR=42.8
Pod undocumented
296 0 my $self = shift;
297 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
298  
299 0 my $answer_args = {};
300 0 ▶ croak "no callback_query_id supplied" unless $args->{callback_query_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"callback_query_id"} 0 0 0
301 0 $answer_args->{callback_query_id} = $args->{callback_query_id};
302  
303 # optional args
304 0 ▶ $answer_args->{text} = $args->{text} if exists $args->{text};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"text"} 0 0 0
305 0 ▶ $answer_args->{show_alert} = $args->{show_alert} if exists $args->{show_alert};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"show_alert"} 0 0 0
306 0 ▶ $answer_args->{url} = $args->{url} if exists $args->{url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"url"} 0 0 0
307 0 ▶ $answer_args->{cache_time} = $args->{cache_time} if exists $args->{cache_time};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"cache_time"} 0 0 0
308 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
309 0 my $url = "https://api.telegram.org/bot${token}/answerCallbackQuery";
310 0 my $api_response = $self->_post_request($url, $answer_args);
311  
312 0 return 1;
313 }
314  
315 0 ▶sub getChatMember {
Subroutine getChatMember: not called CC=4 SCAR=30.0
Pod undocumented
316 0 my $self = shift;
317 0 my $chat_id = shift;
318 0 my $user_id = shift;
319  
320 0 my $gcm_args = {};
321 0 ▶ croak "no chat_id supplied to getChatMember" unless $chat_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $chat_id 0 0 0
322 0 ▶ croak "no user_id supplied to getChatMember" unless $user_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $user_id 0 0 0
323 0 $gcm_args->{user_id} = $user_id;
324 0 $gcm_args->{chat_id} = $chat_id;
325  
326 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
327 0 my $url = "https://api.telegram.org/bot${token}/getChatMember";
328 0 my $api_response = $self->_post_request($url, $gcm_args);
329  
330 0 return Telegram::Bot::Object::ChatMember->create_from_hash($api_response, $self);
331 }
332  
333 0 ▶sub approveChatJoinRequest {
Subroutine approveChatJoinRequest: not called CC=5 SCAR=34.0
Pod undocumented
334 0 my $self = shift;
335 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
336  
337 0 my $answer_args = {};
338 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
339 0 $answer_args->{chat_id} = $args->{chat_id};
340 0 ▶ croak "no user_id supplied" unless $args->{user_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"user_id"} 0 0 0
341 0 $answer_args->{user_id} = $args->{user_id};
342  
343 # no optional args
344  
345 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
346 0 my $url = "https://api.telegram.org/bot${token}/approveChatJoinRequest";
347 0 my $api_response = $self->_post_request($url, $answer_args);
348  
349 0 return 1;
350 }
351  
352 0 ▶sub declineChatJoinRequest {
Subroutine declineChatJoinRequest: not called CC=5 SCAR=34.0
Pod undocumented
353 0 my $self = shift;
354 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
355  
356 0 my $answer_args = {};
357 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
358 0 $answer_args->{chat_id} = $args->{chat_id};
359 0 ▶ croak "no user_id supplied" unless $args->{user_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"user_id"} 0 0 0
360 0 $answer_args->{user_id} = $args->{user_id};
361  
362 # no optional args
363  
364 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
365 0 my $url = "https://api.telegram.org/bot${token}/declineChatJoinRequest";
366 0 my $api_response = $self->_post_request($url, $answer_args);
367  
368 0 return 1;
369 }
370  
371 0 ▶sub banChatMember {
Subroutine banChatMember: not called CC=7 SCAR=40.3
Pod undocumented
372 0 my $self = shift;
373 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
374  
375 0 my $answer_args = {};
376 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
377 0 $answer_args->{chat_id} = $args->{chat_id};
378 0 ▶ croak "no user_id supplied" unless $args->{user_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"user_id"} 0 0 0
379 0 $answer_args->{user_id} = $args->{user_id};
380  
381 # optional args
382 0 ▶ $answer_args->{until_date} = $args->{until_date} if exists $args->{until_date};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"until_date"} 0 0 0
383 0 ▶ $answer_args->{revoke_messages} = $args->{revoke_messages} if exists $args->{revoke_messages};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"revoke_messages"} 0 0 0
384
385 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
386 0 my $url = "https://api.telegram.org/bot${token}/banChatMember";
387 0 my $api_response = $self->_post_request($url, $answer_args);
388  
389 0 return 1;
390 }
391  
392 0 ▶sub unbanChatMember {
Subroutine unbanChatMember: not called CC=6 SCAR=37.4
Pod undocumented
393 0 my $self = shift;
394 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
395  
396 0 my $answer_args = {};
397 0 ▶ croak "no chat_id supplied" unless $args->{chat_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"chat_id"} 0 0 0
398 0 $answer_args->{chat_id} = $args->{chat_id};
399 0 ▶ croak "no user_id supplied" unless $args->{user_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"user_id"} 0 0 0
400 0 $answer_args->{user_id} = $args->{user_id};
401  
402 # optional args
403 0 ▶ $answer_args->{only_if_banned} = $args->{only_if_banned} if exists $args->{only_if_banned};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"only_if_banned"} 0 0 0
404  
405 0 ▶ my $token = $self->token || croak "no token?";
Condition0%0/2
$self->token || croak('no token?')
l!l
00
MC/DC0%0/1
$self->token || croak('no token?')
$self->token
Truth table0%0/2
A
A= $self->token
Aresult
11
00
406 0 my $url = "https://api.telegram.org/bot${token}/unbanChatMember";
407 0 my $api_response = $self->_post_request($url, $answer_args);
408  
409 0 return 1;
410 }
411  
412 0 ▶sub _add_getUpdates_handler {
Subroutine _add_getUpdates_handler: not called CC=1 SCAR=6.9
413 0 my $self = shift;
414  
415 0 my $http_active = 0;
416 0 my $last_update_id = -1;
417 0 my $token = $self->token;
418  
419 0 ▶ Mojo::IOLoop->recurring(0.1 => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
420 # do nothing if our previous longpoll is still going
421 0 ▶ return if $http_active;
Branch: 1 branch
BranchTrueFalseTotal
if $http_active 0 0 0
422  
423 0 my $offset = $last_update_id + 1;
424 0 my $updateURL = "https://api.telegram.org/bot${token}/getUpdates?offset=${offset}&timeout=60";
425 0 $http_active = 1;
426  
427 0 ▶ $self->ua->get_p($updateURL)->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
428 0 my ($tx) = @_;
429 0 my $res = $tx->res->json;
430 0 my $items = $res->{result};
431 0 foreach my $item (@$items) {
432 0 $last_update_id = $item->{update_id};
433 0 $self->_process_message($item);
434 }
435  
436 0 $http_active = 0;
437 0 ▶ })->catch(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
438 0 my ($error) = @_;
439 0 warn "Connection error: $error";
440
441 0 $http_active = 0;
442 0 });
443 0 });
444 }
445  
446 # process a message which arrived via getUpdates
447 6 ▶sub _process_message {
Subroutine _process_message: called CC=13 SCAR=26.5
448 6 my $self = shift;
449 6 my $item = shift;
450  
451 6 my $update_id = $item->{update_id};
452 # There can be several types of responses. But only one response.
453 # https://core.telegram.org/bots/api#update
454 6 my $update;
455 6 ▶ $update = Telegram::Bot::Object::Message->create_from_hash($item->{message}, $self) if $item->{message};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"message"} 4 2 6
456 6 ▶ $update = Telegram::Bot::Object::Message->create_from_hash($item->{edited_message}, $self) if $item->{edited_message};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"edited_message"} 1 5 6
457 6 ▶ $update = Telegram::Bot::Object::Message->create_from_hash($item->{channel_post}, $self) if $item->{channel_post};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"channel_post"} 0 6 6
458 6 ▶ $update = Telegram::Bot::Object::Message->create_from_hash($item->{edited_channel_post}, $self) if $item->{edited_channel_post};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"edited_channel_post"} 0 6 6
459 6 ▶ $update = Telegram::Bot::Object::InlineQuery->create_from_hash($item->{inline_query}, $self) if $item->{inline_query};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"inline_query"} 0 6 6
460 6 ▶ $update = Telegram::Bot::Object::Message->create_from_hash($item->{my_chat_member}, $self) if $item->{my_chat_member}; # after v0.025
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"my_chat_member"} 0 6 6
461 6 ▶ $update = Telegram::Bot::Object::CallbackQuery->create_from_hash($item->{callback_query}, $self) if $item->{callback_query};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"callback_query"} 0 6 6
462 6 ▶ $update = Telegram::Bot::Object::ChatJoinRequest->create_from_hash($item->{chat_join_request}, $self) if $item->{chat_join_request};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"chat_join_request"} 0 6 6
463 6 ▶ $update = Telegram::Bot::Object::ChatMemberUpdated->create_from_hash($item->{chat_member}, $self) if $item->{chat_member};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"chat_member"} 0 6 6
464 6 ▶ $update = Telegram::Bot::Object::ChatMemberUpdated->create_from_hash($item->{my_chat_member}, $self) if $item->{my_chat_member};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"my_chat_member"} 0 6 6
465 # chat_member => someone else
466 # my_chat_member => actually us
467  
468 # if we got to this point without creating a response, it must be a type we
469 # don't handle yet
470 6 ▶ if (! $update) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($update) 1 5 6
471 1 warn "Do not know how to handle this update: " . Dumper($item);
472 1 return;
473 }
474  
475 5 foreach my $listener (@{ $self->listeners }) {
476 # call the listener code, supplying ourself and the update
477 8 $listener->($self, $update);
478 }
479 }
480  
481  
482 0 ▶sub _post_request {
Subroutine _post_request: not called CC=4 SCAR=30.0
483 0 my $self = shift;
484 0 my $url = shift;
485 0 ▶ my $form_args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
486  
487 0 my $res = $self->ua->post($url, form => $form_args)->result;
488 0 ▶ if ($res->is_success) { return $res->json->{result}; }
Branch: 2 branches
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
elsif ($res->is_error) { } 0 0 0
489 0 elsif ($res->is_error) { warn "Failed to post: " . $res->json->{description} . "\n" . Data::Dumper::Dumper($form_args); }
490 0 else { die "Not sure what went wrong"; }
491 }
492  
493  
494 2 1;
495  
496 __END__
« Bot/Object/Base.pm ↑ Summary Bot/Object/InlineQuery.pm »