forked from st-fresh/dialogflow-intercom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
111 lines (80 loc) · 3.61 KB
/
Copy pathindex.php
File metadata and controls
111 lines (80 loc) · 3.61 KB
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
<?php
/*
What you need to do:
1. Set up a webhook in Intercom to this script.
2. Get necessary access tokens from Dialogflow and Intercom
a) Access tokens for API from both platforms
b) The ID of the admin account under which you want the bot to reply from (can find that from the profile URL for example)
If you have any improvements, feel free to do them!
You also need to add your tokens inside the script. This is very hacky still ;)
*/
//Get Header JSON, returns all header json as object
$json = file_get_contents('php://input');
$obj = json_decode($json);
//Get user message & conversation id
$usermessage = strip_tags($obj->data->item->conversation_parts->conversation_parts['0']->body);
$conversationid = strip_tags($obj->data->item->id);
$deliveryattempts = $obj->delivery_attempts;
if ($deliveryattempts > 1 ) {
//Hacky way to stop the bot from replying multiple times
die();
}
if ($obj->topic == 'conversation.user.replied') {
echo buildReplyToUser($usermessage, $conversationid);
}
if ($obj->topic == 'conversation.user.created') {
//User message is different for user.created
$usermessage = strip_tags($obj->data->item->conversation_message->body);
echo buildReplyToUser($usermessage, $conversationid);
}
function getReplyFromAPIai($usermessage, $conversationid) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://api.api.ai/v1/query');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization:Bearer *********************',
'Accept: application/json',
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(array('v' => '20170712', 'lang' => 'en', 'sessionId' => $conversationid, 'query' => $usermessage)));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
$server_output_json = json_decode($server_output);
$reply = $server_output_json->result->speech;
curl_close ($ch);
return $reply;
}
//This breaks a longer response into smaller messages by looking for \n\n inside the message
function buildReplyToUser($usermessage, $conversationid) {
$reply = getReplyFromAPIai($usermessage, $conversationid);
$replyMessage = explode('\n\n', $reply);
foreach ($replyMessage as $message) {
replyToUser($conversationid, $message);
}
return 'OK';
}
function replyToUser($conversationid, $reply) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://api.intercom.io/conversations/' . $conversationid . '/reply');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization:Bearer ******************',
'Accept: application/json',
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(array('type' => 'admin', 'message_type' => 'comment', 'admin_id' => '***********', 'body' => $reply)));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") {
return 'OK';
} else {
return 'Issue with Intercom: ' . $server_output;
}
}
?>