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..4d65e86 100644 --- a/src/apis/Request/Request.php +++ b/src/apis/Request/Request.php @@ -109,4 +109,40 @@ 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; + + 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()); + } + + return json_decode($response->getBody()->getContents())->results; + } }