-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.php
115 lines (100 loc) · 3.88 KB
/
api.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
<?php
header("Content-Type: application/json; charset=UTF-8");
require_once ("vendor/autoload.php");
require_once ("database.php");
use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;
if (!isset($_POST)){
die();
}
$request = json_decode(file_get_contents('php://input'), true);
// Get user data
$ownerId = $_GET['id'];
$userManyChatApiKey = $userJSONKey = "";
$conn = mysqli_connect($database_server, $database_username, $database_password, $database_name);
$sql = "SELECT * FROM tblusers WHERE UserId='$ownerId'";
$result = mysqli_query($conn, $sql);
if ($result){
$row = mysqli_fetch_assoc($result);
$userManyChatApiKey = $row['UserManyChatApiKey'];
$userJSONKey = "keys/" . $row['UserJSONKey'];
}
mysqli_close($conn);
// Get project id
$jsonData = file_get_contents($userJSONKey);
$jsonArray = json_decode($jsonData, true);
$projectId = $jsonArray['project_id'];
// Decode manychat request
$userId = $request["id"];
$userResponse = $request["last_input_text"];
// Send data to dialogflow
$botReply = dialogflowSmallTalk($userJSONKey, $projectId, $userResponse, $userId);
// Send data to manychat
sendResponseToManychat(makeManyChatTextResponse($botReply, $userId), $userManyChatApiKey);
function makeManyChatTextResponse($text, $userId){
$response = array(
"subscriber_id" => $userId,
"data" => array(
"version" => "v2",
"content" => array(
"messages" => array(
array(
"type" => "text",
"text" => $text
)
)
)
)
);
return $response;
}
function sendResponseToManychat($data, $apiKey){
$manyChatApiKey = "Bearer " . $apiKey;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.manychat.com/fb/sending/sendContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data, true),
CURLOPT_HTTPHEADER => array(
"Authorization: " . $manyChatApiKey,
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "Error :" . $err;
} else {
echo $response;
}
}
function dialogflowSmallTalk($apiKey, $projectId, $text, $sessionId, $languageCode = 'en-US'){
$credentials = array('credentials' => $apiKey);
$sessionsClient = new SessionsClient($credentials);
$session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid());
// create text input
$textInput = new TextInput();
$textInput->setText($text);
$textInput->setLanguageCode($languageCode);
// create query input
$queryInput = new QueryInput();
$queryInput->setText($textInput);
// get response and relevant info
$response = $sessionsClient->detectIntent($session, $queryInput);
$queryResult = $response->getQueryResult();
$queryText = $queryResult->getQueryText();
$intent = $queryResult->getIntent();
$confidence = $queryResult->getIntentDetectionConfidence();
$fulfilmentText = $queryResult->getFulfillmentText();
$sessionsClient->close();
return $fulfilmentText;
}
?>