line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::ChatBots::Messenger::WebHook; |
2
|
2
|
|
|
2
|
|
2708
|
use strict; |
|
2
|
|
|
|
|
18
|
|
|
2
|
|
|
|
|
74
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
135
|
|
4
|
|
|
|
|
|
|
{ our $VERSION = '0.004'; } |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
17
|
use Ouch; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
176
|
|
7
|
2
|
|
|
2
|
|
1079
|
use Log::Any qw< $log >; |
|
2
|
|
|
|
|
12752
|
|
|
2
|
|
|
|
|
22
|
|
8
|
2
|
|
|
2
|
|
3252
|
use Data::Dumper; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
163
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
792
|
use Moo; |
|
2
|
|
|
|
|
13341
|
|
|
2
|
|
|
|
|
31
|
|
11
|
2
|
|
|
2
|
|
3358
|
use namespace::clean; |
|
2
|
|
|
|
|
22286
|
|
|
2
|
|
|
|
|
19
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
with 'Bot::ChatBots::Role::WebHook'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has no_routes => ( |
16
|
|
|
|
|
|
|
is => 'ro', |
17
|
|
|
|
|
|
|
default => sub { return 0 }, |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has verify_token => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
lazy => 1, |
23
|
|
|
|
|
|
|
default => sub { ouch 500, 'you MUST set verify_token by yourself' }, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub BUILD { |
27
|
1
|
|
|
1
|
1
|
14
|
my $self = shift; |
28
|
1
|
50
|
|
|
|
19
|
if (!$self->no_routes) { |
29
|
1
|
|
|
|
|
9
|
$self->install_route; # the main POST one |
30
|
1
|
|
|
|
|
533
|
$self->install_get_route; # the FB back-authentication route |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} ## end sub BUILD |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub install_get_route { |
35
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
36
|
1
|
50
|
33
|
|
|
9
|
my $args = (@_ && ref($_[0])) ? $_[0] : {@_}; |
37
|
1
|
|
33
|
|
|
15
|
my $r = $args->{routes} // $self->app->routes; |
38
|
1
|
|
33
|
|
|
48
|
my $p = $args->{path} // $self->path; |
39
|
|
|
|
|
|
|
return $r->get( |
40
|
|
|
|
|
|
|
$p => sub { |
41
|
3
|
|
|
3
|
|
29878
|
my $c = shift; |
42
|
3
|
|
|
|
|
17
|
my $qp = $c->req->query_params; |
43
|
3
|
|
100
|
|
|
152
|
my $hmode = $qp->param('hub.mode') // ''; |
44
|
3
|
|
100
|
|
|
713
|
my $hvt = $qp->param('hub.verify_token') // ''; |
45
|
3
|
100
|
100
|
|
|
281
|
if (($hmode eq 'subscribe') && ($hvt eq $self->verify_token)) { |
46
|
1
|
|
|
|
|
25
|
$log->info('received correct challenge request'); |
47
|
1
|
|
50
|
|
|
42
|
my $challenge = $qp->param('hub.challenge') // ''; |
48
|
1
|
|
|
|
|
28
|
$c->render(text => $challenge); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else { |
51
|
2
|
|
|
|
|
41
|
$log->error('GET request not accepted'); |
52
|
2
|
|
|
|
|
25
|
$c->rendered(403); |
53
|
|
|
|
|
|
|
} |
54
|
3
|
|
|
|
|
1232
|
return; |
55
|
|
|
|
|
|
|
} |
56
|
1
|
|
|
|
|
23
|
); |
57
|
|
|
|
|
|
|
} ## end sub install_get_route |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub normalize_record { |
60
|
2
|
|
|
2
|
1
|
458
|
my ($self, $record) = @_; |
61
|
2
|
50
|
|
|
|
9
|
my $update = $record->{update} or ouch 500, 'no update found!'; |
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
6
|
$record->{source}{technology} = 'messenger'; |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
4
|
$record->{type} = 'message'; |
66
|
2
|
|
|
|
|
6
|
$record->{payload} = $record->{update}{message}; |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
5
|
$record->{sender} = $record->{update}{sender}; |
69
|
|
|
|
|
|
|
|
70
|
2
|
|
|
|
|
4
|
my $chan = $record->{channel} = {%{$record->{update}{sender}}}; |
|
2
|
|
|
|
|
12
|
|
71
|
2
|
|
|
|
|
5
|
$chan->{fqid} = $chan->{id}; |
72
|
|
|
|
|
|
|
|
73
|
2
|
|
|
|
|
8
|
return $record; |
74
|
|
|
|
|
|
|
} ## end sub normalize_record |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub parse_request { |
77
|
1
|
|
|
1
|
1
|
8465
|
my ($self, $req) = @_; |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
10
|
my $data = $req->json; |
80
|
1
|
50
|
|
|
|
816
|
return unless $data->{object} eq 'page'; |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
2
|
local $Data::Dumper::Indent = 1; |
83
|
1
|
|
|
|
|
2
|
my @updates; |
84
|
1
|
|
|
|
|
2
|
for my $entry (@{$data->{entry}}) { |
|
1
|
|
|
|
|
4
|
|
85
|
1
|
|
|
|
|
4
|
my $page_id = $entry->{id}; |
86
|
1
|
|
|
|
|
1
|
my $event_time = $entry->{time}; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
EVENT: |
89
|
1
|
|
|
|
|
2
|
for my $event (@{$entry->{messaging}}) { |
|
1
|
|
|
|
|
4
|
|
90
|
2
|
50
|
|
|
|
6
|
if (exists $event->{message}) { |
91
|
2
|
|
|
|
|
4
|
push @updates, $event; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
0
|
$log->warn('unknown event: ' . Dumper($event)); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} ## end EVENT: for my $event (@{$entry...}) |
97
|
|
|
|
|
|
|
} ## end for my $entry (@{$data->...}) |
98
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
6
|
return @updates; |
100
|
|
|
|
|
|
|
} ## end sub parse_request |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |