-
Notifications
You must be signed in to change notification settings - Fork 37
/
Feed.pm
210 lines (155 loc) · 4.44 KB
/
Feed.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package Facebook::Graph::Page::Feed;
use strict;
$Facebook::Graph::Page::Feed::VERSION = '1.0700';
use Any::Moose;
extends 'Facebook::Graph::Publish';
has page_id => (
is => 'rw',
predicate => 'has_page_id',
);
sub set_page_id {
my ($self, $page_id) = @_;
$self->object_name(''); # not sure it's a good way here
$self->set_published(1); # it's a default value on FB side;
$self->page_id($page_id);
return $self;
}
use subs qw(object_path);
sub object_path{
my $s = shift;
return $s->has_page_id ?
sprintf( '%s/feed', $s->page_id )
: die "You must set_page_id first";
}
has message => (
is => 'rw',
predicate => 'has_message',
);
sub set_message {
my ($self, $message) = @_;
$self->message($message);
return $self;
}
has link_uri => (
is => 'rw',
predicate => 'has_link_uri',
);
sub set_link_uri {
my ($self, $link_uri) = @_;
$self->link_uri($link_uri);
return $self;
}
has backdated_time => (
is => 'rw',
predicate => 'has_backdated_time',
);
sub set_backdated_time
{
my ($self, $backdated_time) = @_;
$self->backdated_time($backdated_time);
return $self;
}
has scheduled_publish_time => (
is => 'rw',
predicate => 'has_scheduled_publish_time'
);
sub set_scheduled_publish_time
{
my ($self, $scheduled_publish_time) = @_;
$self->scheduled_publish_time($scheduled_publish_time);
return $self;
}
has published => (
is => 'rw',
predicate => 'has_published',
reader => '_get_published',
writer => '_set_published',
);
sub published
{
my($self, $published) = @_;
if( scalar @_ > 1 )
{
$self->_set_published($published);
}
return $self->_get_published && $self->_get_published ne 'false';
}
sub set_published
{
my ($self, $published) = @_;
$self->published($published);
return $self;
}
around get_post_params => sub {
my ($orig, $self) = @_;
my $post = $orig->($self);
if ($self->has_message) {
push @$post, message => $self->message;
}
if ($self->has_link_uri) {
push @$post, link => $self->link_uri;
}
if ($self->has_backdated_time) {
push @$post, backdated_time => $self->backdated_time + 10*3600; # temporary fix for FB::Graph API
}
if (
$self->has_scheduled_publish_time
and (
not $self->has_published or not $self->published
)
) {
push @$post, scheduled_publish_time => $self->scheduled_publish_time;
}
if ($self->has_published) {
push @$post, published => $self->published ? 'true': 'false';
}
return $post;
};
no Any::Moose;
__PACKAGE__->meta->make_immutable;
=head1 NAME
Facebook::Graph::Page::Feed - Add a post to a Facebook page.
=head1 SYNOPSIS
my $fb = Facebook::Graph->new;
$fb->add_page_feed
->set_page_id($page_id)
->set_message('This is a test post')
->set_backdated_time(time-3600)
->set_scheduled_publish_time(time+3600)
->set_published('false')
->set_link_uri('http://mysite.com/testpage')
->publish;
=head1 DESCRIPTION
This module gives you quick and easy access to publish posts on FB pages.
Implements publishing protocol available here:
L<https://developers.facebook.com/docs/graph-api/reference/v2.0/page/feed/>
B<ATTENTION:> You must have the page access token to use this module.
It's available in C<$fb->fetch('me/accounts')>, but, you must have a user
token with C<manage_page> permission.
More information about tokens could be found here:
L<https://developers.facebook.com/docs/facebook-login/access-tokens>
=head1 METHODS
=head2 set_page_id ( page_id )
Specify a page id to post to. This is required field and must be set before
publishing.
=head2 set_message ( message )
Sets the description of the link.
=head3 message
A string of text.
=head2 set_link_uri ( uri )
Sets the URI to link to.
=head3 uri
A URI.
=head2 set_backdated_time ( time )
Set a date of your post, different from now. Just a backdate. Time should be a
timestamp, no fancy formats.
=head2 set_scheduled_publish_time( time )
Set a publishing time for scheduled posts. Time should be a desired timestamp of
post in future. Doesn't work with minutes from now, but works fine with hour+
=head2 publish ( )
Posts the data and returns a L<Facebook::Graph::Response> object. The response object should contain the id:
{"id":"1647395831_130068550371568"}
=head1 LEGAL
Facebook::Graph is Copyright 2010 - 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
Facebook::Graph::Page::Feed is Copyright 2014 Alexandr Evstigneev (L<http://evstigneev.com>) and is licensed under the same terms as Perl itself.
=cut