From 975edfc6a2eb6b9d1682a053e022247dba6e662b Mon Sep 17 00:00:00 2001 From: Nathan Grasset Date: Mon, 18 Dec 2017 18:47:52 +0100 Subject: [PATCH 1/4] feat (dialog) add method to use /dialog endpoint --- src/Constants.php | 1 + src/apis/Request/Request.php | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/Constants.php b/src/Constants.php index 454f686..4dd6894 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -6,6 +6,7 @@ class Constants { const REQUEST_ENDPOINT = "https://api.recast.ai/v2/request"; const CONVERSE_ENDPOINT = "https://api.recast.ai/v2/converse"; + const DIALOG_ENDPOINT = "https://api.recast.ai/build/v1/dialog"; const CONVERSATION_ENDPOINT = "https://api.recast.ai/connect/v1/messages"; const MESSAGE_ENDPOINT = "https://api.recast.ai/connect/v1/conversations/:conversation_id/messages"; diff --git a/src/apis/Request/Request.php b/src/apis/Request/Request.php index e20e13b..01d64e5 100644 --- a/src/apis/Request/Request.php +++ b/src/apis/Request/Request.php @@ -109,4 +109,43 @@ public function converseText($text, $options = []) { return new \RecastAI\apis\Resources\Conversation($token, $responseBody); } + + public function dialogText($text, $conversation_id, $options = []) { + $token = array_key_exists('token', $options) ? $options['token'] : $this->token; + $language = array_key_exists('language', $options) ? $options['language'] : $this->language; + $memory = array_key_exists('memory', $options) ? $options['memory'] : NULL; + + if (count($token) < 1) { + throw new \Exception('Error: Parameter token is missing'); + } + + if (count($conversation_id) < 1) { + throw new \Exception('Error: Parameter conversation_id is missing'); + } + + $headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $token]; + $body = json_encode([ + "message" => [ + "type" => "text", + "content" => $text + ], + "language" => $language, + "conversation_id" => $conversation_id + ]); + + $client = new \GuzzleHttp\Client(); + + try { + $response = $client->request('POST', \RecastAI\Constants::DIALOG_ENDPOINT, [ + 'headers' => $headers, + 'body' => $body + ]); + } catch (\Exception $e) { + throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); + } + + $responseBody = json_decode($response->getBody()->getContents()); + + return new \RecastAI\apis\Resources\Conversation($token, $responseBody); + } } From 6bfe0803d31149b5bd6cd128c76624a22324f9bc Mon Sep 17 00:00:00 2001 From: Nathan Grasset Date: Mon, 18 Dec 2017 18:49:16 +0100 Subject: [PATCH 2/4] fix (dialog) return raw json body --- src/apis/Request/Request.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/apis/Request/Request.php b/src/apis/Request/Request.php index 01d64e5..0d9329c 100644 --- a/src/apis/Request/Request.php +++ b/src/apis/Request/Request.php @@ -144,8 +144,6 @@ public function dialogText($text, $conversation_id, $options = []) { throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); } - $responseBody = json_decode($response->getBody()->getContents()); - - return new \RecastAI\apis\Resources\Conversation($token, $responseBody); + return json_decode($response->getBody()->getContents()); } } From 54db53b5d792cd4d373b8aec484c005c3cbeb14a Mon Sep 17 00:00:00 2001 From: Nathan Grasset Date: Mon, 18 Dec 2017 18:49:56 +0100 Subject: [PATCH 3/4] fix (dialog) remove unused variable --- src/apis/Request/Request.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/apis/Request/Request.php b/src/apis/Request/Request.php index 0d9329c..f143cef 100644 --- a/src/apis/Request/Request.php +++ b/src/apis/Request/Request.php @@ -113,7 +113,6 @@ public function converseText($text, $options = []) { public function dialogText($text, $conversation_id, $options = []) { $token = array_key_exists('token', $options) ? $options['token'] : $this->token; $language = array_key_exists('language', $options) ? $options['language'] : $this->language; - $memory = array_key_exists('memory', $options) ? $options['memory'] : NULL; if (count($token) < 1) { throw new \Exception('Error: Parameter token is missing'); From 2fe0ca5e3ca23fc5429411ad4ffb390a655ad572 Mon Sep 17 00:00:00 2001 From: Nathan Grasset Date: Wed, 20 Dec 2017 16:06:11 +0100 Subject: [PATCH 4/4] fix (dialog) return only results field after a post to /dialog --- src/apis/Request/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apis/Request/Request.php b/src/apis/Request/Request.php index f143cef..4d65e86 100644 --- a/src/apis/Request/Request.php +++ b/src/apis/Request/Request.php @@ -143,6 +143,6 @@ public function dialogText($text, $conversation_id, $options = []) { throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); } - return json_decode($response->getBody()->getContents()); + return json_decode($response->getBody()->getContents())->results; } }