-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFBOutgoingMessage.php
268 lines (250 loc) · 7.34 KB
/
FBOutgoingMessage.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
require_once(dirname(__FILE__).'/FBQuickReply.php');
require_once(dirname(__FILE__).'/FBButton.php');
require_once(dirname(__FILE__).'/FBElement.php');
/**
* This class builds an outgoing message that can be sent back to the user from the bot.
*/
class FBOutgoingMessage {
const SEND_API_URI = 'https://graph.facebook.com/v2.6/me/messages';
public static $quickRepliesSent = array();
private $obj;
/**
* Construct base object with recipient id
*/
public function __construct($recipient) {
// set up basic structure of FB Reply message
$this->obj = array(
'recipient' => array(
'id' => $recipient
),
'message' => array()
);
return $this;
}
/**
* If recipient id has to be set later.
*/
public function setRecipient($recipient) {
$this->obj['recipient']['id'] = $recipient;
return $this;
}
/**
* Set the text message to send.
* Note that the use of this will reset previous content sets or button adds.
*/
public function setText($text) {
$this->obj['message'] = array(
'text' => $text
);
return $this;
}
/**
* Set the image to send with an image url.
* Note that the use of this will reset previous content sets or button adds.
* Note that other similar type of messages available to use are "audio", "video", and "file",
* currently not implemented.
*/
public function setImage($imageUrl) {
$this->obj['message'] = array(
'attachment' => array(
'type' => 'image',
'payload' => array(
'url' => $imageUrl
)
)
);
return $this;
}
/**
* Adds a quick reply button to this message.
* Must be a valid quick reply object.
*/
public function addQuickReply($quickReply) {
if ($quickReply instanceof FBQuickReply) {
if (empty($this->obj['message']['quick_replies'])) {
$this->obj['message']['quick_replies'] = array();
}
$this->obj['message']['quick_replies'][] = $quickReply;
} else {
error_log('Adding invalid Quick Reply');
}
return $this;
}
/**
* Button Template is some text with some buttons.
* Note that button template texts are limited to 320 characters.
* Note that the use of this will reset previous content sets or button adds.
*/
public function setButtonTemplate($text, $buttons) {
foreach ($buttons as $button) {
if (!($button instanceof FBButton)) {
error_log('Adding invalid Button');
return $this;;
}
}
$this->obj['message'] = array(
'attachment' => array(
'type' => 'template',
'payload' => array(
'template_type' => 'button',
'text' => $text,
'buttons' => $buttons
)
)
);
return $this;
}
/**
* Button Template is some text with some buttons.
* Note that the use of this will reset previous content sets or button adds.
*/
public function setGenericTemplate($elements) {
foreach ($elements as $element) {
if (!($element instanceof FBGenericElement)) {
error_log('Adding invalid Generic Element');
return $this;
}
}
$this->obj['message'] = array(
'attachment' => array(
'type' => 'template',
'payload' => array(
'template_type' => 'generic',
'elements' => $elements
)
)
);
return $this;
}
/**
* Button Template is some text with some buttons.
* Note that the use of this will reset previous content sets or button adds.
*/
public function setListTemplate($elements, $buttons = array(), $top_element_style = 'large') {
// $top_element_style must be large or compact
// $elements max 4
// $buttons max 1
foreach ($elements as $element) {
if (!($element instanceof FBListElement)) {
error_log('Adding invalid List Element');
return $this;
}
}
foreach ($buttons as $button) {
if (!($button instanceof FBButton)) {
error_log('Adding invalid Button');
return $this;
}
}
$this->obj['message'] = array(
'attachment' => array(
'type' => 'template',
'payload' => array(
'template_type' => 'list',
'top_element_style' => $top_element_style,
'elements' => $elements,
'buttons' => $buttons
)
)
);
return $this;
}
/**
* Actually make send call to FB API
*/
public function send() {
if (FB_ACCESS_TOKEN == '') {
error_log('Error: You probably did not set up your FB access token.');
}
// check for any quick replies sent in this request
if (!empty($this->obj['message']) && !empty($this->obj['message']['quick_replies'])) {
self::$quickRepliesSent = $this->obj['message']['quick_replies'];
}
$output = self::makeCurl(self::SEND_API_URI, json_encode($this->obj));
$result = json_decode($output, true);
$ret = true;
if (!empty($result['error'])) {
error_log('Facebook error: '.$output);
$ret = $result;
}
return $ret;
}
/**
* Send an image as file attachment upload. This is a special API call by itself.
* Note that other similar type of messages available to use are "audio", "video", and "file",
* currently not implemented.
*/
public function sendImageFile($imageFileWithFullPath) {
if (FB_ACCESS_TOKEN == '') {
error_log('Error: You probably did not set up your FB access token.');
}
$filesize = getimagesize($imageFileWithFullPath);
$mimetype = $filesize['mime'];
// construct a special post body for this request
$postbody = array(
'recipient' => json_encode(array('id' => $this->obj['recipient']['id'])),
'message' => '{"attachment":{"type":"image", "payload":{}}}',
'filedata'=>'@'.$imageFileWithFullPath.';type='.$mimetype
);
$output = self::makeCurl(self::SEND_API_URI, $postbody, false);
$result = json_decode($output, true);
if (!empty($result['error'])) {
error_log('Facebook error: '.$output);
}
}
/**
* Send a "typing" action. This is a special API call by itself.
*/
public function sendTypingOn() {
if (FB_ACCESS_TOKEN == '') {
error_log('Error: You probably did not set up your FB access token.');
}
// construct a special post body for this request
$postbody = array(
'recipient' => json_encode(array('id' => $this->obj['recipient']['id'])),
'sender_action' => 'typing_on'
);
$output = self::makeCurl(self::SEND_API_URI, $postbody, false);
$result = json_decode($output, true);
if (!empty($result['error'])) {
error_log('Facebook error: '.$output);
}
}
/**
* Send a "mark_seen" action. This is a special API call by itself.
*/
public function sendMarkSeen() {
if (FB_ACCESS_TOKEN == '') {
error_log('Error: You probably did not set up your FB access token.');
}
// construct a special post body for this request
$postbody = array(
'recipient' => json_encode(array('id' => $this->obj['recipient']['id'])),
'sender_action' => 'mark_seen'
);
$output = self::makeCurl(self::SEND_API_URI, $postbody, false);
$result = json_decode($output, true);
if (!empty($result['error'])) {
error_log('Facebook error: '.$output);
}
}
/**
* Utility method to make the curl call
*/
private static function makeCurl($uri, $postbody, $includeJsonHeader = true) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri.'?access_token='.FB_ACCESS_TOKEN);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
if ($includeJsonHeader) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}