forked from FBDevCLagos/thefacebot-steps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompleted_index.php
98 lines (85 loc) · 2.58 KB
/
completed_index.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
<?php
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
$VERIFY_TOKEN = '<YOUR_VERIFY_TOKEN>';
$PAGE_ACCESS_TOKEN = '<YOUR_PAGE_ACCESS_TOKEN>';
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === $VERIFY_TOKEN) {
//If the Verify token matches, return the challenge.
echo $challenge;
}else {
print "Hello World!";
$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 message", $PAGE_ACCESS_TOKEN);
}
}
}
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;
}
function build_image_message_payload($sender, $image_url){
// Build the json payload data
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text": "",
"attachment":{
"type":"image",
"payload":{
"url": "' . $image_url . '"
}
}
}
}';
return $jsonData;
}
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;
}