Recast.AI - SDK PHP
, (*1)
Recast.AI official SDK in PHP, (*2)
Synospis
This module is a PHP interface to the Recast.AI API. It allows you to make request to your bots, (*3)
Installation
composer require recastai/sdk-php
Usage
textRequest(YOUR_TEXT);
YOUR_INTENT = $res->intent();
// Do your code...
?>
Specs
Classes
This module contains 4 classes, as follows:, (*4)
- 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 ;), (*5)
class Client
The Client can be instanciated with a token and a language (both optional)., (*6)
$client = new Client(YOUR_TOKEN, YOUR_LANGUAGE);
Your tokens:, (*7)
, (*8)
Copy paste your request access token from your bot's settings., (*9)
Your language, (*10)
$client = new Client(YOUR_TOKEN, 'en');
The language is a lowercase 639-1 isocode., (*11)
Text Request
textRequest(text, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE, proxy: YOUR_URL_PROXY }), (*12)
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., (*13)
$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., (*14)
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., (*15)
File Request
fileRequest(file, callback, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE, proxy: YOUR_PROXY_URL }), (*16)
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., (*17)
file format: .wav, (*18)
$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., (*19)
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, (*20)
class Response
The Response is generated after a call to either fileRequest or textRequest., (*21)
Get the first detected intent
| Method |
Params |
Return |
| intent() |
the first detected intent |
$res = $client->textRequest($text);
$lol = $res->intent();
if ($result->slug == 'weather') {
// Do your code...
}
Get one entity
| Method |
Params |
Return |
| get(name) |
name: String |
the first Entity matched |
$res = $client->textRequest($text);
$result = $res->get('location');
Get all entities matching name
| Method |
Params |
Return |
| all(name) |
name: String |
all the Entities matched |
$res = $client->textRequest($text);
$lol = $res->all('location');
Getters
Each of the following methods corresponds to a Response attribute, (*22)
| 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 |
class Entity
The Entity is generated by the Sentence constructor., (*23)
Getters
Each of the following methods corresponds to a Response attribute, (*24)
| 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, (*25)
$res = $client->textRequest($text);
$result = $res->get('location');
var_dump($result->slug);
var_dump($result->raw);
class RecastError
The Recast.AI Error is thrown when receiving an non-200 response from Recast.AI., (*26)
As it inherits from Error, it implements the default Error methods., (*27)
More
You can view the whole API reference at man.recast.ai., (*28)
Author
Bruno Gantelmi, bruno.gantelmi@recast.ai, (*29)
You can follow us on Twitter at @recastai for updates and releases., (*30)
License
Copyright (c) [2016] Recast.AI, (*31)
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:, (*32)
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software., (*33)
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., (*34)