| 1 | package Telegram::Bot::Brain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | $Telegram::Bot::Brain::VERSION = '0.023'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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/encode_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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # base class for building telegram robots with Mojolicious | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | has longpoll_time => 60; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | has ua => sub { Mojo::UserAgent->new->inactivity_timeout(shift->longpoll_time + 15) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | has token => sub { croak "you need to supply your own token"; }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | has tasks => sub { [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | has listeners => sub { [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | has log => sub { Log::Any->get_logger }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | sub add_repeating_task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_repeating_task: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $seconds = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $task = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | my $repeater = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # Perform operation every $seconds seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $last_check = time(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | Mojo::IOLoop->recurring(0.1 => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $loop = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $now = time(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | return unless ($now - $last_check) >= $seconds; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $last_check = $now; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $task->($self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # keep a copy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | push @{ $self->tasks }, $repeater; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # kick it off | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $repeater->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | sub add_listener { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_listener: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | my $coderef = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | push @{ $self->listeners }, $coderef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | sub init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | die "init was not overridden!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | sub think { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
think: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $self->init(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $self->_add_getUpdates_handler; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | sub getMe { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getMe: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $token = $self->token || croak "no token?"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->token || croak('no token?')
MC/DC0%0/2
$self->token || croak('no token?')
$self->token
croak('no token?')
Truth table0%0/3
A || B
A= $self->tokenB= croak('no token?')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $url = "https://api.telegram.org/bot${token}/getMe"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $api_response = $self->_post_request($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return Telegram::Bot::Object::User->create_from_hash($api_response, $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | sub sendMessage { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sendMessage: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $send_args = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | croak "no chat_id supplied" unless $args->{chat_id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $send_args->{chat_id} = $args->{chat_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | croak "no text supplied" unless $args->{text}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $send_args->{text} = $args->{text}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # these are optional, send if they are supplied | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $send_args->{parse_mode} = $args->{parse_mode} if exists $args->{parse_mode}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $send_args->{disable_web_page_preview} = $args->{disable_web_page_preview} if exists $args->{disable_web_page_preview}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $send_args->{disable_notification} = $args->{disable_notification} if exists $args->{disable_notification}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $send_args->{reply_to_message_id} = $args->{reply_to_message_id} if exists $args->{reply_to_message_id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # check reply_markup is the right kind | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (exists $args->{reply_markup}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $reply_markup = $args->{reply_markup}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | die "bad reply_markup supplied" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
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"
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"
ref $reply_markup ne "Telegram::Bot::Object::InlineKeyboardMarkup" and ref $reply_markup ne "Telegram::Bot::Object::ReplyKeyboardMarkup"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | if ( ref($reply_markup) ne 'Telegram::Bot::Object::InlineKeyboardMarkup' && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ref($reply_markup) ne 'Telegram::Bot::Object::ReplyKeyboardMarkup' && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | ref($reply_markup) ne 'Telegram::Bot::Object::ReplyKeyboardRemove' && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | ref($reply_markup) ne 'Telegram::Bot::Object::ForceReply' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $send_args->{reply_markup} = encode_json($reply_markup->as_hashref); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $token = $self->token || croak "no token?"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->token || croak('no token?')
MC/DC0%0/2
$self->token || croak('no token?')
$self->token
croak('no token?')
Truth table0%0/3
A || B
A= $self->tokenB= croak('no token?')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $url = "https://api.telegram.org/bot${token}/sendMessage"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $api_response = $self->_post_request($url, $send_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | return Telegram::Bot::Object::Message->create_from_hash($api_response, $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | sub forwardMessage { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
forwardMessage: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $send_args = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | croak "no chat_id supplied" unless $args->{chat_id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $send_args->{chat_id} = $args->{chat_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | croak "no from_chat_id supplied" unless $args->{from_chat_id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $send_args->{from_chat_id} = $args->{from_chat_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | croak "no message_id supplied" unless $args->{message_id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $send_args->{message_id} = $args->{message_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # these are optional, send if they are supplied | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | $send_args->{disable_notification} = $args->{disable_notification} if exists $args->{disable_notification}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $token = $self->token || croak "no token?"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->token || croak('no token?')
MC/DC0%0/2
$self->token || croak('no token?')
$self->token
croak('no token?')
Truth table0%0/3
A || B
A= $self->tokenB= croak('no token?')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my $url = "https://api.telegram.org/bot${token}/forwardMessage"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | my $api_response = $self->_post_request($url, $send_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return Telegram::Bot::Object::Message->create_from_hash($api_response, $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | sub sendPhoto { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sendPhoto: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my $send_args = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | croak "no chat_id supplied" unless $args->{chat_id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $send_args->{chat_id} = $args->{chat_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # photo can be a string (which might be either a URL for telegram servers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # to fetch, or a file_id string) or a file on disk to upload - we need | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # to handle that last case here as it changes the way we create the HTTP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | croak "no photo supplied" unless $args->{photo}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if (-e $args->{photo}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | $send_args->{photo} = { photo => { file => $args->{photo} } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $send_args->{photo} = $args->{photo}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $token = $self->token || croak "no token?"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->token || croak('no token?')
MC/DC0%0/2
$self->token || croak('no token?')
$self->token
croak('no token?')
Truth table0%0/3
A || B
A= $self->tokenB= croak('no token?')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my $url = "https://api.telegram.org/bot${token}/sendPhoto"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | my $api_response = $self->_post_request($url, $send_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | return Telegram::Bot::Object::Message->create_from_hash($api_response, $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | sub _add_getUpdates_handler { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_getUpdates_handler: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $http_active = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my $last_update_id = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my $token = $self->token; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | Mojo::IOLoop->recurring(0.1 => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # do nothing if our previous longpoll is still going | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | return if $http_active; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my $offset = $last_update_id + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $updateURL = "https://api.telegram.org/bot${token}/getUpdates?offset=${offset}&timeout=60"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $http_active = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | $self->ua->get($updateURL => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my ($ua, $tx) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my $res = $tx->res->json; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $items = $res->{result}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | foreach my $item (@$items) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | $last_update_id = $item->{update_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $self->_process_message($item); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $http_active = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # process a message which arrived via getUpdates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | sub _process_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_message: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 6 | my $item = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 6 | my $update_id = $item->{update_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # There can be several types of responses. But only one response. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # https://core.telegram.org/bots/api#update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | my $update; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 6 | ▶ | $update = Telegram::Bot::Object::Message->create_from_hash($item->{message}, $self) if $item->{message}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 6 | ▶ | $update = Telegram::Bot::Object::Message->create_from_hash($item->{edited_message}, $self) if $item->{edited_message}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 6 | ▶ | $update = Telegram::Bot::Object::Message->create_from_hash($item->{channel_post}, $self) if $item->{channel_post}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 6 | ▶ | $update = Telegram::Bot::Object::Message->create_from_hash($item->{edited_channel_post}, $self) if $item->{edited_channel_post}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # if we got to this point without creating a response, it must be a type we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # don't handle yet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 6 | ▶ | if (! $update) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | warn "Telegram::Bot::Brain does not know how to handle this update: " . Dumper($item); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 5 | foreach my $listener (@{ $self->listeners }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # call the listener code, supplying ourself and the update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | $listener->($self, $update); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | sub _post_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_post_request: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my $url = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | my $form_args = shift || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | my $res = $self->ua->post($url, form => $form_args)->result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if ($res->is_success) { return $res->json->{result}; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | elsif ($res->is_error) { die "Failed to post: " . $res->json->{description}; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | else { die "Not sure what went wrong"; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||