This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marian ANDRE
committed
Apr 25, 2017
1 parent
2a2f10f
commit 6f522d7
Showing
5 changed files
with
616 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace RecastAI\apis\Connect; | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
/** | ||
* Class Connect | ||
* @package RecastAI | ||
*/ | ||
class Connect | ||
{ | ||
/** | ||
* Connect constructor. | ||
* @param null $token | ||
* @param null $language | ||
*/ | ||
public function __construct($token = null, $language = null) | ||
{ | ||
$this->token = $token; | ||
$this->language = $language; | ||
} | ||
|
||
public function handleMessage($body, callable $callback) { | ||
if (is_callable($callback)) { | ||
$callback(new \RecastAI\apis\Resources\Message($this->token, $body)); | ||
} | ||
} | ||
|
||
public function sendMessage($messages, $conversationId) { | ||
$headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $this->token]; | ||
$body = json_encode(['messages' => $messages]); | ||
|
||
$client = new \GuzzleHttp\Client(); | ||
|
||
try { | ||
$response = $client->request('POST', str_replace(":conversation_id", $conversationId, \RecastAI\Constants::MESSAGE_ENDPOINT), [ | ||
'headers' => $headers, | ||
'body' => $body | ||
]); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); | ||
} | ||
|
||
return json_decode($response->getBody()->getContents()); | ||
} | ||
|
||
public function broadcastMessage($messages) { | ||
$headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $this->token]; | ||
$body = json_encode(['messages' => $messages]); | ||
|
||
$client = new \GuzzleHttp\Client(); | ||
|
||
try { | ||
$response = $client->request('POST', \RecastAI\Constants::CONVERSATION_ENDPOINT, [ | ||
'headers' => $headers, | ||
'body' => $body | ||
]); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); | ||
} | ||
|
||
return json_decode($response->getBody()->getContents()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?php | ||
|
||
namespace RecastAI\apis\Resources; | ||
|
||
/** | ||
* Class Conversation | ||
* @package RecastAI | ||
*/ | ||
class Conversation | ||
{ | ||
|
||
public function __construct($token, $responseBody) | ||
{ | ||
$this->token = $token; | ||
|
||
$this->raw = $responseBody; | ||
$this->uuid = $responseBody->uuid; | ||
$this->source = $responseBody->source; | ||
$this->replies = $responseBody->replies; | ||
$this->action = $responseBody->action; | ||
$this->next_actions = $responseBody->next_actions; | ||
$this->memory = $responseBody->memory; | ||
$this->entities = $responseBody->entities; | ||
$this->sentiment = $responseBody->sentiment; | ||
$this->intents = $responseBody->intents; | ||
$this->conversation_token = $responseBody->conversation_token; | ||
$this->language = $responseBody->language; | ||
$this->processing_language = $responseBody->processing_language; | ||
$this->version = $responseBody->version; | ||
$this->timestamp = $responseBody->timestamp; | ||
$this->status = $responseBody->status; | ||
} | ||
|
||
/** | ||
* Returns the first reply if there is one | ||
* @return {String}: this first reply or null | ||
*/ | ||
public function reply() | ||
{ | ||
return (count($this->replies) > 0 ? $this->replies[0] : null); | ||
} | ||
|
||
/** | ||
* Returns a concatenation of the replies | ||
* @return {String}: the concatenation of the replies | ||
*/ | ||
public function replies() | ||
{ | ||
return ($this->replies); | ||
} | ||
|
||
/** | ||
* Returns a concatenation of the replies | ||
* @return {String}: the concatenation of the replies | ||
*/ | ||
public function joinedReplies($separator = ' ') | ||
{ | ||
return ($this->replies ? join($separator, $this->replies) : null); | ||
} | ||
|
||
/** | ||
* Returns all the action whose name matches the parameter | ||
* @return {Array}: returns an array of action, or null | ||
*/ | ||
public function action() | ||
{ | ||
return ($this->action ? $this->action : null); | ||
} | ||
|
||
/** | ||
* Returns the first nextActions whose name matches the parameter | ||
* @return {Array}: returns an array of first nextActions, or null | ||
*/ | ||
public function nextAction() | ||
{ | ||
return (count($this->nextAction) > 0 ? $this->nextAction[0] : null); | ||
} | ||
|
||
/** | ||
* Returns all nextActions | ||
* @return {Array}: returns an array of nextActions, or [] | ||
*/ | ||
public function nextActions() | ||
{ | ||
return ($this->nextActions ? $this->nextActions : []); | ||
} | ||
|
||
/** | ||
* SENTIMENT HELPERS | ||
* Returns whether or not the response sentiment corresponds to the checked one | ||
* @return {boolean}: true or false | ||
*/ | ||
|
||
public function isPositive() | ||
{ | ||
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_POSITIVE); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isNeutral() | ||
{ | ||
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_NEUTRAL); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isNegative() | ||
{ | ||
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_NEGATIVE); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isVPositive() | ||
{ | ||
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VPOSITIVE); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isVNegative() | ||
{ | ||
return ($this->sentiment === \RecastAI\Constants::SENTIMENT_VNEGATIVE); | ||
} | ||
|
||
/** | ||
* Returns the memory matching the alias | ||
* or all the memory if no alias provided | ||
* @return {object}: the memory | ||
*/ | ||
public function getMemory($name = null) | ||
{ | ||
if ($name === null) { | ||
return ($this->memory); | ||
} else if (array_key_exists($name, $this->memory)) { | ||
return ($this->memory->$name); | ||
} else { | ||
return (null); | ||
} | ||
} | ||
|
||
/** | ||
* Merge the conversation memory with the one in parameter | ||
* Returns the memory updated | ||
* @return {object}: the memory updated | ||
*/ | ||
public function setMemory($memory) | ||
{ | ||
$headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $this->token]; | ||
$body = json_encode([ | ||
'conversation_token' => $this->conversation_token, | ||
'memory' => $memory | ||
]); | ||
|
||
$client = new \GuzzleHttp\Client(); | ||
|
||
try { | ||
$response = $client->request('PUT', \RecastAI\Constants::CONVERSE_ENDPOINT, [ | ||
'headers' => $headers, | ||
'body' => $body | ||
]); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); | ||
} | ||
|
||
$responseBody = json_decode($response->getBody()->getContents())->results; | ||
|
||
return $responseBody->memory; | ||
} | ||
|
||
/** | ||
* Reset the memory of the conversation | ||
* @return {object}: the updated memory | ||
*/ | ||
public function resetMemory($alias = null) | ||
{ | ||
$headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $this->token]; | ||
|
||
if ($alias === null) { | ||
$body = json_encode(['conversation_token' => $this->conversation_token]); | ||
} else { | ||
$body = json_encode(['conversation_token' => $this->conversation_token, 'memory' => (object)[$alias => null]]); | ||
} | ||
|
||
$client = new \GuzzleHttp\Client(); | ||
|
||
try { | ||
$response = $client->request('PUT', \RecastAI\Constants::CONVERSE_ENDPOINT, [ | ||
'headers' => $headers, | ||
'body' => $body | ||
]); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); | ||
} | ||
|
||
$responseBody = json_decode($response->getBody()->getContents())->results; | ||
|
||
return $responseBody->memory; | ||
} | ||
|
||
/** | ||
* Reset the conversation | ||
* @return {object}: the updated memory | ||
*/ | ||
public function resetConversation() | ||
{ | ||
$headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $this->token]; | ||
|
||
$body = json_encode(['conversation_token' => $this->conversation_token]); | ||
|
||
$client = new \GuzzleHttp\Client(); | ||
|
||
try { | ||
$response = $client->request('DELETE', \RecastAI\Constants::CONVERSE_ENDPOINT, [ | ||
'headers' => $headers, | ||
'body' => $body | ||
]); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); | ||
} | ||
|
||
$responseBody = json_decode($response->getBody()->getContents())->results; | ||
|
||
return $responseBody->memory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace RecastAI\apis\Resources; | ||
|
||
/** | ||
* Class Entity | ||
* @package RecastAI | ||
*/ | ||
Class Entity | ||
{ | ||
/** | ||
* Entity constructor. | ||
* @param $name | ||
* @param $data | ||
*/ | ||
public function __construct($name, $data) | ||
{ | ||
$this->name = $name; | ||
$this->raw = $data; | ||
foreach ($data as $key => $value) { | ||
$this->$key = $value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace RecastAI\apis\Resources; | ||
|
||
/** | ||
* Class Conversation | ||
* @package RecastAI | ||
*/ | ||
class Message | ||
{ | ||
|
||
public function __construct($token, $body) | ||
{ | ||
$this->token = $token; | ||
|
||
$response = json_decode($body); | ||
|
||
$this->content = $response->message->attachment->content; | ||
$this->type = $response->message->attachment->type; | ||
$this->conversationId = $response->message->conversation; | ||
$this->recastToken = $token; | ||
$this->chatId = $response->chatId; | ||
$this->senderId = $response->senderId; | ||
$this->attachment = $response->message->attachment; | ||
|
||
$this->_messageStack = []; | ||
} | ||
|
||
/** | ||
* Add a reply in message Stack | ||
* @return {Array}: the message stack | ||
*/ | ||
public function addReply($replies) | ||
{ | ||
$this->_messageStack = array_merge($this->_messageStack, $replies); | ||
|
||
return $this->_messageStack; | ||
} | ||
|
||
/** | ||
* Send reply to bot into a conversation | ||
* @return {object}: the memory updated | ||
*/ | ||
public function reply($replies = []) | ||
{ | ||
$headers = ['Content-Type' => 'application/json', 'Authorization' => "Token " . $this->token]; | ||
$body = json_encode(['messages' => array_merge($this->_messageStack, $replies)]); | ||
|
||
$client = new \GuzzleHttp\Client(); | ||
|
||
try { | ||
$response = $client->request('POST', str_replace(":conversation_id", $this->conversationId, \RecastAI\Constants::MESSAGE_ENDPOINT), [ | ||
'headers' => $headers, | ||
'body' => $body | ||
]); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Error: API is not accessible: ' . $e->getMessage()); | ||
} | ||
|
||
return json_decode($response->getBody()->getContents()); | ||
} | ||
} |
Oops, something went wrong.