| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Bot::ChatBots::Telegram::WebHook; |
|
2
|
2
|
|
|
2
|
|
1893
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
49
|
|
|
3
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
121
|
|
|
4
|
|
|
|
|
|
|
{ our $VERSION = '0.012'; } |
|
5
|
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use Ouch; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
10
|
|
|
7
|
2
|
|
|
2
|
|
456
|
use Log::Any qw< $log >; |
|
|
2
|
|
|
|
|
6389
|
|
|
|
2
|
|
|
|
|
8
|
|
|
8
|
2
|
|
|
2
|
|
1843
|
use Data::Dumper; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
86
|
|
|
9
|
2
|
|
|
2
|
|
24
|
use Mojo::URL; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
15
|
|
|
10
|
2
|
|
|
2
|
|
48
|
use Mojo::Path; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
11
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
466
|
use Moo; |
|
|
2
|
|
|
|
|
6839
|
|
|
|
2
|
|
|
|
|
8
|
|
|
13
|
2
|
|
|
2
|
|
1837
|
use namespace::clean; |
|
|
2
|
|
|
|
|
8853
|
|
|
|
2
|
|
|
|
|
11
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
with 'Bot::ChatBots::Telegram::Role::Source'; # has normalize_record |
|
16
|
|
|
|
|
|
|
with 'Bot::ChatBots::Role::WebHook'; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has auto_register => (is => 'ro', default => 0, init_arg => 'register'); |
|
19
|
|
|
|
|
|
|
has auto_unregister => (is => 'rw', default => 0, init_arg => 'unregister'); |
|
20
|
|
|
|
|
|
|
has certificate => (is => 'rw', default => undef); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub BUILD { |
|
23
|
1
|
|
|
1
|
1
|
4034
|
my $self = shift; |
|
24
|
1
|
|
|
|
|
3
|
$self->install_route; |
|
25
|
1
|
50
|
|
|
|
384
|
$self->register if $self->auto_register; |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub DEMOLISH { |
|
29
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
30
|
0
|
0
|
|
|
|
0
|
$self->unregister if $self->auto_unregister; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub parse_request { |
|
34
|
2
|
|
|
2
|
1
|
17671
|
my ($self, $req) = @_; |
|
35
|
2
|
|
|
|
|
11
|
return $req->json; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _set_http_response { |
|
39
|
0
|
|
|
0
|
|
|
my ($record, $outcome) = @_; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $message = $outcome->{sent_response} = { |
|
42
|
|
|
|
|
|
|
method => 'sendMessage', |
|
43
|
|
|
|
|
|
|
chat_id => $record->{channel}{id}, |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
ref($outcome->{response}) eq 'HASH' |
|
46
|
0
|
|
|
|
|
|
? (%{$outcome->{response}}) # shallow copy suffices |
|
47
|
|
|
|
|
|
|
: (text => $outcome->{response}) |
|
48
|
0
|
0
|
|
|
|
|
}; |
|
49
|
0
|
|
|
|
|
|
$record->{source}{refs}{controller}->render(json => $message); |
|
50
|
0
|
|
|
|
|
|
$record->{source}{flags}{rendered} = 1; |
|
51
|
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
around process => sub { |
|
56
|
|
|
|
|
|
|
my ($orig, $self, $record) = @_; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$record->{source}{refs}{sender} = $self->sender; |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $outcome = $orig->($self, $record); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
if (ref($outcome) eq 'HASH') { |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# check setting the proper HTTP Response |
|
65
|
|
|
|
|
|
|
# $record and $outcome might be the same, but the flag is |
|
66
|
|
|
|
|
|
|
# namely supported in $record |
|
67
|
|
|
|
|
|
|
_set_http_response($record, $outcome) |
|
68
|
|
|
|
|
|
|
if defined($outcome->{response}) |
|
69
|
|
|
|
|
|
|
&& (!$record->{source}{flags}{rendered}); |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# check using the sender. We use $outcome as $record here, because |
|
72
|
|
|
|
|
|
|
# $outcome is what we are going to pass on eventually |
|
73
|
|
|
|
|
|
|
$self->sender->send_message($outcome->{send_response}, record => $outcome) |
|
74
|
|
|
|
|
|
|
if defined $outcome->{send_response}; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
return $outcome; |
|
78
|
|
|
|
|
|
|
}; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub register { |
|
81
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
82
|
0
|
0
|
0
|
|
|
|
my $args = (@_ && ref($_[0])) ? $_[0] : {@_}; |
|
83
|
|
|
|
|
|
|
|
|
84
|
0
|
|
0
|
|
|
|
my $app = $args->{app} // $self->app; |
|
85
|
0
|
|
0
|
|
|
|
my $token = $args->{token} // $self->token; |
|
86
|
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $wh_url; |
|
88
|
0
|
0
|
0
|
|
|
|
if (my $url = $args->{url} // $self->url) { |
|
89
|
0
|
|
|
|
|
|
$wh_url = Mojo::URL->new($url); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
else { |
|
92
|
0
|
|
0
|
|
|
|
my $path = $args->{path} // $self->path; |
|
93
|
0
|
|
|
|
|
|
$path = Mojo::Path->new($path); |
|
94
|
|
|
|
|
|
|
|
|
95
|
0
|
|
0
|
|
|
|
my $c = $args->{controller} // $app->build_controller; |
|
96
|
0
|
|
|
|
|
|
$wh_url = $c->url_for($path); |
|
97
|
|
|
|
|
|
|
} ## end else [ if (my $url = $args->{...})] |
|
98
|
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my $wh_url_string = $wh_url->to_abs->to_string; |
|
100
|
0
|
|
|
|
|
|
my $form = {url => $wh_url_string}; |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
0
|
0
|
|
|
|
if (my $certificate = $args->{certificate} // $self->certificate) { |
|
103
|
0
|
0
|
|
|
|
|
$certificate = {content => $certificate} unless ref $certificate; |
|
104
|
0
|
|
|
|
|
|
$form->{certificate} = $certificate; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
$log->info("registering bot URI $wh_url_string"); |
|
108
|
0
|
|
0
|
|
|
|
$self->_register($args->{token} // $self->token, $form); |
|
109
|
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
return $self; |
|
111
|
|
|
|
|
|
|
} ## end sub register |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub unregister { |
|
114
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
115
|
0
|
0
|
0
|
|
|
|
my $args = (@_ && ref($_[0])) ? $_[0] : {@_}; |
|
116
|
0
|
|
0
|
|
|
|
$self->_register($args->{token} // $self->token); |
|
117
|
0
|
|
|
|
|
|
return $self; |
|
118
|
|
|
|
|
|
|
} ## end sub unregister |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _register { |
|
121
|
0
|
|
|
0
|
|
|
my ($self, $token, $form) = @_; |
|
122
|
0
|
|
|
|
|
|
require WWW::Telegram::BotAPI; |
|
123
|
0
|
|
0
|
|
|
|
my $outcome = WWW::Telegram::BotAPI->new(token => $token) |
|
124
|
|
|
|
|
|
|
->setWebhook($form // {url => ''}); |
|
125
|
0
|
0
|
0
|
|
|
|
$log->info($outcome->{description} // 'unknown result') if $log; |
|
126
|
0
|
|
|
|
|
|
return; |
|
127
|
|
|
|
|
|
|
} ## end sub _register |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |