forked from 0sc/thefacebot-steps
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstep8.txt
83 lines (67 loc) · 2.44 KB
/
step8.txt
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
//Step 8
//Part A ******************************************************************
//Paste part A into index.php
$input = json_decode(file_get_contents('php://input'), true);
// Get the Senders Page Scoped ID
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
// Get the message text sent
$message = $input['entry'][0]['messaging'][0]['message']['text'];
if(!empty($message)){
if($message == 'image'){
send_image_message($sender,'http://thecatapi.com/api/images/get?format=src&type=gif', $PAGE_ACCESS_TOKEN);
}else{
send_text_message($sender, "Thanks for you're message", $PAGE_ACCESS_TOKEN);
}
}
// END OF PART A *********************************************************
//Part B *****************************************************************
//Paste part B into index.php
function send_message($access_token, $payload) {
// Send/Recieve API
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=' . $access_token;
// Initiate the curl
$ch = curl_init($url);
// Set the curl to POST
curl_setopt($ch, CURLOPT_POST, 1);
// Add the json payload
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the header type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// SSL Settings
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Send the request
$result = curl_exec($ch);
//Return the result
return $result;
}
function build_text_message_payload($sender, $message){
// Build the json payload data
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text": "'. $message .'"
}
}';
return $jsonData;
}
function send_text_message($sender, $message, $access_token){
$jsonData = build_text_message_payload($sender, $message);
$result = send_message($access_token, $jsonData);
return $result;
}
// TODO: Step 9 - Complete this function
// https://developers.facebook.com/docs/messenger-platform/send-api-reference/image-attachment
function build_image_message_payload($sender, $image_url){
// Build the json payload data
return '';
}
function send_image_message($sender, $image_url, $access_token){
$jsonData = build_image_message_payload($sender, $image_url);
$result = send_message($access_token, $jsonData);
return $result;
}
// END OF PART B *******************************************************