Recast.AI official SDK in PHP
This module is a PHP interface to the Recast.AI API. It allows you to make request to your bots
composer require recastai/sdk-php
<?php
use client\Client;
require 'client.php';
$client = new Client(YOUR_TOKEN, YOUR_LANGUAGE);
$res = $client->textRequest(YOUR_TEXT);
YOUR_INTENT = $res->intent();
// Do your code...
?>
This module contains 4 classes, as follows:
- Client is the client allowing you to make requests.
- Response contains the response from Recast.AI.
- Entity represents an entity found by Recast.AI in your user's input.
- RecastError is the error returned by the module.
Don't hesitate to dive into the code, it's commented ;)
The Client can be instanciated with a token and a language (both optional).
$client = new Client(YOUR_TOKEN, YOUR_LANGUAGE);
Your tokens:
Copy paste your request access token from your bot's settings.
Your language
$client = new Client(YOUR_TOKEN, 'en');
The language is a lowercase 639-1 isocode.
textRequest(text, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE, proxy: YOUR_URL_PROXY })
If your pass a token or a language in the options parameter, it will override your default client language or token. You can pass a proxy url in the options if needed.
$res = $client->textRequest(YOUR_TEXT);
// Do your code...¯
})
// With optional parameters
$options = array('language' => 'YOUR_LANGUAGE', 'token' => 'YOUR_TOKEN');
$res = $client->textRequest(YOUR_TEXT, $options);
// Do your code...
If a language is provided: the language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used.
If no language is provided: the language of the text is detected and is used for processing if your bot has expressions for it, else your bot's primary language is used for processing.
fileRequest(file, callback, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE, proxy: YOUR_PROXY_URL })
If your pass a token or a language in the options parameter, it will override your default client language or token. You can pass a proxy url in the options if needed.
file format: .wav
$res = $client->fileRequest('myFile.wav');
// Do your code...
})
$options = array('language' => 'en', 'token' => YOUR_TOKEN);
$res = $client->fileRequest('myFile.wav', $options);
// Do your code...
If a language is provided: Your bot's primary language is used for processing as we do not provide language detection for speech.
If no language is provided: The language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used
The Response is generated after a call to either fileRequest or textRequest.
Method | Params | Return |
---|---|---|
intent() | the first detected intent |
$res = $client->textRequest($text);
$lol = $res->intent();
if ($result->slug == 'weather') {
// Do your code...
}
Method | Params | Return |
---|---|---|
get(name) | name: String | the first Entity matched |
$res = $client->textRequest($text);
$result = $res->get('location');
Method | Params | Return |
---|---|---|
all(name) | name: String | all the Entities matched |
$res = $client->textRequest($text);
$lol = $res->all('location');
Each of the following methods corresponds to a Response attribute
Method | Params | Return |
---|---|---|
raw | String: the raw unparsed json response | |
source | String: the user input | |
intents | Array[object]: all the matched intents | |
sentences | Array[Sentence]: all the detected sentences | |
version | String: the version of the json | |
timestamp | String: the timestamp at the end of the processing | |
status | String: the status of the response | |
type | String: the type of the response |
The Entity is generated by the Sentence constructor.
Each of the following methods corresponds to a Response attribute
Attributes | Description |
---|---|
name | String: the name of the entity |
raw | String: the unparsed json value of the entity |
In addition to those methods, more attributes are generated depending of the nature of the entity. The full list can be found there: man.recast.ai
$res = $client->textRequest($text);
$result = $res->get('location');
var_dump($result->slug);
var_dump($result->raw);
The Recast.AI Error is thrown when receiving an non-200 response from Recast.AI.
As it inherits from Error, it implements the default Error methods.
You can view the whole API reference at man.recast.ai.
Bruno Gantelmi, bruno.gantelmi@recast.ai
You can follow us on Twitter at @recastai for updates and releases.
Copyright (c) [2016] Recast.AI
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.