Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

feat (dialog) add method to use /dialog endpoint #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
36 changes: 36 additions & 0 deletions src/apis/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}