|
| 1 | +# Recast.AI - SDK PHP |
| 2 | + |
| 3 | +[logo]: https://github.com/RecastAI/SDK-NodeJs/blob/master/misc/logo-inline.png "Recast.AI" |
| 4 | + |
| 5 | +![alt text][logo] |
| 6 | + |
| 7 | +Recast.AI official SDK in PHP |
| 8 | + |
| 9 | +## Synospis |
| 10 | + |
| 11 | +This module is a PHP interface to the [Recast.AI](https://recast.ai) API. It allows you to make request to your bots |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require recastai/sdk-php |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | + |
| 22 | +```php |
| 23 | +<?php |
| 24 | +use client\Client; |
| 25 | + |
| 26 | +require 'client.php'; |
| 27 | + |
| 28 | +$client = new Client(YOUR_TOKEN, YOUR_LANGUAGE); |
| 29 | + |
| 30 | +$res = $client->textRequest(YOUR_TEXT); |
| 31 | +YOUR_INTENT = $res->intent(); |
| 32 | +// Do your code... |
| 33 | + |
| 34 | +?> |
| 35 | +``` |
| 36 | + |
| 37 | +## Specs |
| 38 | + |
| 39 | +### Classes |
| 40 | + |
| 41 | +This module contains 4 classes, as follows: |
| 42 | + |
| 43 | +* Client is the client allowing you to make requests. |
| 44 | +* Response contains the response from [Recast.AI](https://recast.ai). |
| 45 | +* Entity represents an entity found by Recast.AI in your user's input. |
| 46 | +* RecastError is the error returned by the module. |
| 47 | + |
| 48 | +Don't hesitate to dive into the code, it's commented ;) |
| 49 | + |
| 50 | +## class Client |
| 51 | + |
| 52 | +The Client can be instanciated with a token and a language (both optional). |
| 53 | + |
| 54 | +```php |
| 55 | +$client = new Client(YOUR_TOKEN, YOUR_LANGUAGE); |
| 56 | +``` |
| 57 | + |
| 58 | +__Your tokens:__ |
| 59 | + |
| 60 | +[token]: https://github.com/RecastAI/SDK-NodeJs/blob/master/misc/recast-ai-tokens.png "Tokens" |
| 61 | + |
| 62 | +![alt text][token] |
| 63 | + |
| 64 | +*Copy paste your request access token from your bot's settings.* |
| 65 | + |
| 66 | +__Your language__ |
| 67 | + |
| 68 | +```php |
| 69 | +$client = new Client(YOUR_TOKEN, 'en'); |
| 70 | +``` |
| 71 | +*The language is a lowercase 639-1 isocode.* |
| 72 | + |
| 73 | +## Text Request |
| 74 | + |
| 75 | +textRequest(text, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE, proxy: YOUR_URL_PROXY }) |
| 76 | + |
| 77 | +If your pass a token or a language in the options parameter, it will override your default client language or token. |
| 78 | +You can pass a proxy url in the options if needed. |
| 79 | + |
| 80 | +```php |
| 81 | +$res = $client->textRequest(YOUR_TEXT); |
| 82 | +// Do your code...¯ |
| 83 | + |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +```php |
| 88 | +// With optional parameters |
| 89 | + |
| 90 | +$options = array('language' => 'YOUR_LANGUAGE', 'token' => 'YOUR_TOKEN'); |
| 91 | + |
| 92 | +$res = $client->textRequest(YOUR_TEXT, $options); |
| 93 | + |
| 94 | +// Do your code... |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +__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. |
| 99 | + |
| 100 | +__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. |
| 101 | + |
| 102 | +## File Request |
| 103 | + |
| 104 | +fileRequest(file, callback, options = { token: YOUR_TOKEN, language: YOUR_LANGUAGE, proxy: YOUR_PROXY_URL }) |
| 105 | + |
| 106 | +If your pass a token or a language in the options parameter, it will override your default client language or token. |
| 107 | +You can pass a proxy url in the options if needed. |
| 108 | + |
| 109 | +__file format: .wav__ |
| 110 | + |
| 111 | +```php |
| 112 | +$res = $client->fileRequest('myFile.wav'); |
| 113 | + |
| 114 | +// Do your code... |
| 115 | + |
| 116 | +}) |
| 117 | +``` |
| 118 | + |
| 119 | +```php |
| 120 | +$options = array('language' => 'en', 'token' => YOUR_TOKEN); |
| 121 | + |
| 122 | +$res = $client->fileRequest('myFile.wav', $options); |
| 123 | + // Do your code... |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +__If a language is provided:__ |
| 128 | +Your bot's primary language is used for processing as we do not provide language detection for speech. |
| 129 | + |
| 130 | +__If no language is provided:__ |
| 131 | +The language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used |
| 132 | + |
| 133 | +## class Response |
| 134 | + |
| 135 | +The Response is generated after a call to either fileRequest or textRequest. |
| 136 | + |
| 137 | +### Get the first detected intent |
| 138 | + |
| 139 | +| Method | Params | Return | |
| 140 | +| ------------- |:------:| :-------------------------| |
| 141 | +| intent() | | the first detected intent | |
| 142 | + |
| 143 | +```php |
| 144 | +$res = $client->textRequest($text); |
| 145 | +$lol = $res->intent(); |
| 146 | + |
| 147 | +if ($result->slug == 'weather') { |
| 148 | + // Do your code... |
| 149 | +} |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | +### Get one entity |
| 154 | + |
| 155 | +| Method | Params | Return | |
| 156 | +| ------------- |:-------------:| :-------------------------| |
| 157 | +| get(name) | name: String | the first Entity matched | |
| 158 | + |
| 159 | + |
| 160 | +```php |
| 161 | +$res = $client->textRequest($text); |
| 162 | + |
| 163 | +$result = $res->get('location'); |
| 164 | + |
| 165 | +``` |
| 166 | + |
| 167 | +### Get all entities matching name |
| 168 | + |
| 169 | +| Method | Params | Return | |
| 170 | +| ------------- |:-------------:| :-------------------------| |
| 171 | +| all(name) | name: String | all the Entities matched | |
| 172 | + |
| 173 | + |
| 174 | +```php |
| 175 | +$res = $client->textRequest($text); |
| 176 | + |
| 177 | +$lol = $res->all('location'); |
| 178 | + |
| 179 | +``` |
| 180 | + |
| 181 | +### Getters |
| 182 | + |
| 183 | +Each of the following methods corresponds to a Response attribute |
| 184 | + |
| 185 | +| Method | Params | Return | |
| 186 | +| ----------- |:------:| :---------------------------------------------------| |
| 187 | +| raw | | String: the raw unparsed json response | |
| 188 | +| source | | String: the user input | |
| 189 | +| intents | | Array[object]: all the matched intents | |
| 190 | +| sentences | | Array[Sentence]: all the detected sentences | |
| 191 | +| version | | String: the version of the json | |
| 192 | +| timestamp | | String: the timestamp at the end of the processing | |
| 193 | +| status | | String: the status of the response | |
| 194 | +| type | | String: the type of the response | |
| 195 | + |
| 196 | + |
| 197 | +## class Entity |
| 198 | + |
| 199 | +The Entity is generated by the Sentence constructor. |
| 200 | + |
| 201 | +### Getters |
| 202 | + |
| 203 | +Each of the following methods corresponds to a Response attribute |
| 204 | + |
| 205 | +| Attributes | Description | |
| 206 | +| ----------- |:--------------------------------------------------------------| |
| 207 | +| name | String: the name of the entity | |
| 208 | +| raw | String: the unparsed json value of the entity | |
| 209 | + |
| 210 | +In addition to those methods, more attributes are generated depending of the nature of the entity. |
| 211 | +The full list can be found there: [man.recast.ai](https://man.recast.ai/#list-of-entities) |
| 212 | + |
| 213 | +```php |
| 214 | +$res = $client->textRequest($text); |
| 215 | + |
| 216 | +$result = $res->get('location'); |
| 217 | + |
| 218 | +var_dump($result->slug); |
| 219 | +var_dump($result->raw); |
| 220 | +``` |
| 221 | + |
| 222 | +## class RecastError |
| 223 | + |
| 224 | +The Recast.AI Error is thrown when receiving an non-200 response from Recast.AI. |
| 225 | + |
| 226 | +As it inherits from Error, it implements the default Error methods. |
| 227 | + |
| 228 | +## More |
| 229 | + |
| 230 | +You can view the whole API reference at [man.recast.ai](https://man.recast.ai). |
| 231 | + |
| 232 | +## Author |
| 233 | + |
| 234 | +Bruno Gantelmi, bruno.gantelmi@recast.ai |
| 235 | + |
| 236 | +You can follow us on Twitter at [@recastai](https://twitter.com/recastai) for updates and releases. |
| 237 | + |
| 238 | +## License |
| 239 | + |
| 240 | +Copyright (c) [2016] [Recast.AI](https://recast.ai) |
| 241 | + |
| 242 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 243 | +of this software and associated documentation files (the "Software"), to deal |
| 244 | +in the Software without restriction, including without limitation the rights |
| 245 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 246 | +copies of the Software, and to permit persons to whom the Software is |
| 247 | +furnished to do so, subject to the following conditions: |
| 248 | + |
| 249 | +The above copyright notice and this permission notice shall be included in all |
| 250 | +copies or substantial portions of the Software. |
| 251 | + |
| 252 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 253 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 254 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 255 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 256 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 257 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 258 | +SOFTWARE. |
0 commit comments