line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Facebook::Messenger::Client; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4525
|
use Moose; |
|
1
|
|
|
|
|
336519
|
|
|
1
|
|
|
|
|
4
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.0'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
5339
|
use Mojo::URL; |
|
1
|
|
|
|
|
81283
|
|
|
1
|
|
|
|
|
10
|
|
8
|
1
|
|
|
1
|
|
505
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
129393
|
|
|
1
|
|
|
|
|
8
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
463
|
use Facebook::Messenger::Client::Text; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
355
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $GRAPH_URL = 'https://graph.facebook.com/v2.6/'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has 'access_token' => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
isa => 'Str', |
17
|
|
|
|
|
|
|
lazy => 1, |
18
|
|
|
|
|
|
|
default => sub { $ENV{GRAPH_ACCESS_TOKEN} } |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has 'ua' => ( |
22
|
|
|
|
|
|
|
is => 'ro', |
23
|
|
|
|
|
|
|
isa => 'Mojo::UserAgent', |
24
|
|
|
|
|
|
|
lazy => 1, |
25
|
|
|
|
|
|
|
default => sub { Mojo::UserAgent->new() } |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub send { |
29
|
0
|
|
|
0
|
1
|
|
my ( $self, $recipient, $message ) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
|
die( 'No token defined!' ) |
32
|
|
|
|
|
|
|
unless( $self->access_token() ); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# NOTE: Please be careful when changing the value to the path |
35
|
|
|
|
|
|
|
# attribute. Consult the documentation for Mojo::Path before doing that. |
36
|
0
|
|
|
|
|
|
my $url = Mojo::URL->new( $GRAPH_URL ) |
37
|
|
|
|
|
|
|
->path( 'me/messages' ) |
38
|
|
|
|
|
|
|
->query( access_token => $self->access_token() ); |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
my $payload = { |
41
|
|
|
|
|
|
|
recipient => { id => $recipient }, |
42
|
|
|
|
|
|
|
message => $message->pack(), |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $tx = $self->ua() |
46
|
|
|
|
|
|
|
->post( $url, json => $payload ); |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $response = $tx->success(); |
49
|
0
|
0
|
|
|
|
|
unless( $response ) { |
50
|
0
|
|
|
|
|
|
my $error = $tx->error(); |
51
|
0
|
|
|
|
|
|
die( sprintf( 'Error: %s', $error->{message} ) ) |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
return $response->json(); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub send_text { |
58
|
0
|
|
|
0
|
1
|
|
my ( $self, $recipient, $text ) = @_; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $message = Facebook::Messenger::Client::Text->new( |
61
|
|
|
|
|
|
|
{ text => $text } |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return $self->send( $recipient, $message ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub get_user { |
68
|
0
|
|
|
0
|
1
|
|
my ( $self, $id ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $url = Mojo::URL->new( $GRAPH_URL ) |
71
|
|
|
|
|
|
|
->path( $id ) |
72
|
|
|
|
|
|
|
->query( |
73
|
|
|
|
|
|
|
access_token => $self->access_token(), |
74
|
|
|
|
|
|
|
fields => 'first_name,last_name,locale,timezone,gender' |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $tx = $self->ua()->get( $url ); |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
my $response = $tx->success(); |
80
|
0
|
0
|
|
|
|
|
unless( $response ) { |
81
|
0
|
|
|
|
|
|
my $error = $tx->error(); |
82
|
0
|
|
|
|
|
|
die( sprintf( 'Error: %s', $error->{message} ) ) |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
return $response->json(); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__PACKAGE__->meta()->make_immutable(); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
__END__ |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=pod |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 NAME |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Facebook::Messenger::Client - Messenger Send API |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
use Facebook::Messenger::Client; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
my $client = Facebook::Messenger::Client->new( |
105
|
|
|
|
|
|
|
access_token => 'blabla' |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$client->send_text( 0123456789, 'Some message...' ); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $hashref = $client->get_user( 01234567890 ); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 DESCRIPTION |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
If you want to build a Facebook chatbot you will need to use the Send API to send |
115
|
|
|
|
|
|
|
back messages to a recipient. This is a basic implementation of the Messenger Send API |
116
|
|
|
|
|
|
|
that allows (for the moment) to send text messages. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 access_token |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
The access token provided by Facebook. This can also be set via the B<GRAPH_ACCESS_TOKEN> |
123
|
|
|
|
|
|
|
environment variable. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 METHODS |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 send |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $hashref = $client->send( $recipient_id, $model ); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Generic send function. This will that a L<Facebook::Messenger::Client::Model> instance |
132
|
|
|
|
|
|
|
and send it to the server. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 send_text |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $hashref = $client->send_text( $recipient_id, 'The message goes here ...' ); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 get_user |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
$hashref = $client->get_user( $user_id ); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 AUTHOR |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Tudor Marghidanu <tudor@marghidanu.com> |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 SEE ALSO |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=over 4 |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
L<Moose> |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L<Mojolicious> |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=back |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 LICENSE |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |