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

Commit 13d3662

Browse files
author
Bruno
committed
Merge branch 'develop'
2 parents 94053bf + 277cf94 commit 13d3662

15 files changed

+379
-316
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ composer.phar
44
.DS_Store
55
node_modules
66
file.wav
7+
test.php

README.md

Lines changed: 11 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,229 +1,35 @@
11
# Recast.AI - SDK PHP
22

3-
[logo]: https://github.com/RecastAI/SDK-NodeJs/blob/master/misc/logo-inline.png "Recast.AI"
3+
[logo]: https://github.com/RecastAI/SDK-PHP/blob/develop/misc/logo-inline.png "Recast.AI"
44

55
![alt text][logo]
66

77
Recast.AI official SDK in PHP
88

99
## Synospis
1010

11-
This module is a PHP interface to the [Recast.AI](https://recast.ai) API. It allows you to make request to your bots
11+
This module is a wrapper around the [Recast.AI](https://recast.ai) API, and allows you to:
12+
* [build a bot](https://github.com/RecastAI/SDK-PHP/wiki/Build-your-bot)
13+
* [analyze your text](https://github.com/RecastAI/SDK-PHP/wiki/Analyse-text)
1214

1315
## Installation
1416

1517
```bash
1618
composer require recastai/sdk-php
1719
```
1820

19-
## Usage
21+
## Documentation
2022

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-
```
23+
You can find the full documentation [here](https://github.com/RecastAI/SDK-PHP/wiki).
3624

3725
## Specs
3826

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.
27+
This module contains 5 classes, as follows:
22528

226-
As it inherits from Error, it implements the default Error methods.
29+
* [Client](https://github.com/RecastAI/SDK-PHP/wiki/Class-Client) is the client allowing you to make requests.
30+
* [Conversation](https://github.com/RecastAI/SDK-PHP/wiki/Class-Conversation) wraps the response from a call to [Recast.AI](https://recast.ai) API with the textConverse Client method.
31+
* [Response](https://github.com/RecastAI/SDK-PHP/wiki/Class-Response) wraps the response from a call to [Recast.AI](https://recast.ai) API with the textRequest or fileRequest Client methods.
32+
* [Entity](https://github.com/RecastAI/SDK-PHP/wiki/Class-Entity) represents an entity extracted from an input.
22733

22834
## More
22935

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"minimum-stability": "dev",
1212
"require": {
1313
"php": "^5.3.3 || ^7.0",
14-
"rmccue/requests": "dev-master",
14+
"rmccue/requests": "^1.6.1",
1515
"guzzlehttp/guzzle": "^6.2@dev"
1616
},
1717
"require-dev": {

misc/logo-inline.png

25.6 KB
Loading

misc/recast-ai-tokens.png

94.2 KB
Loading

src/client.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use response;
77
use Requests;
88
use constants;
9+
use conversation;
910

1011
require 'constants.php';
1112
require 'response.php';
13+
require 'conversation.php';
1214

1315
class Client
1416
{
@@ -29,9 +31,9 @@ public function __construct($token=null, $language=null)
2931
*/
3032
public function textRequest($text, $options=null)
3133
{
32-
if (!$options) {
34+
if ($options === null) {
3335
$token = $this->token;
34-
} else {
36+
} else if (array_key_exists('token', $options)) {
3537
$token = $options['token'];
3638
}
3739

@@ -45,11 +47,9 @@ public function textRequest($text, $options=null)
4547
return('Token is missing');
4648
} else {
4749
$headers = array('Content-Type' => 'application/json', 'Authorization' => "Token " . $token);
48-
$response = file_get_contents("test.json");
4950

5051
$res = $this->requestPrivate(constants\Constants::API_ENDPOINT, $headers, $params);
51-
// return ($res);
52-
return(new response\Response($response));
52+
return(new response\Response($res));
5353
}
5454
}
5555

@@ -94,9 +94,9 @@ protected function requestFilePrivate($url, $params) {
9494
*/
9595
public function fileRequest($file, $options=null)
9696
{
97-
if (!$options) {
97+
if ($options === null) {
9898
$token = $this->token;
99-
} else {
99+
} else if (array_key_exists('token', $options)) {
100100
$token = $options['token'];
101101
}
102102

@@ -118,7 +118,6 @@ public function fileRequest($file, $options=null)
118118
],
119119
]
120120
];
121-
$res = $this->requestFilePrivate($url, $params);
122121
} else {
123122
$params = [
124123
'headers' => [
@@ -136,10 +135,31 @@ public function fileRequest($file, $options=null)
136135
],
137136
]
138137
];
139-
$res = $this->requestFilePrivate($url, $params);
140138
}
141-
$body = (string) $res->getBody();
142-
return(new response\Response($body));
139+
$res = $this->requestFilePrivate($url, $params);
140+
return(new response\Response($res));
141+
}
142+
}
143+
public function textConverse($text, $conversation_token=null, $options=null) {
144+
if ($options === null) {
145+
$token = $this->token;
146+
} else if ($options['token']) {
147+
$token = $options['token'];
148+
}
149+
150+
if ($this->language) {
151+
$params = array('text' => $text, 'language' => $this->language, 'conversation_token' => $conversation_token);
152+
} else {
153+
$params = array('text' => $text, 'conversation_token' => $conversation_token);
154+
}
155+
156+
if (!$token) {
157+
return('Token is missing');
158+
} else {
159+
$headers = array('Content-Type' => 'application/json', 'Authorization' => "Token " . $token);
160+
$res = $this->requestPrivate(constants\Constants::API_ENDPOINT_CONVERSATION, $headers, $params);
161+
162+
return(new conversation\Conversation(($res)));
143163
}
144164
}
145165
}

src/constants.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
class Constants
66
{
7-
const API_ENDPOINT = 'https://api.recast.ai/v1/request';
7+
const API_ENDPOINT = 'https://api.recast.ai/v2/request';
8+
const API_ENDPOINT_CONVERSATION = 'https://api.recast.ai/v2/converse';
89

910
const ACT_ASSERT = 'assert';
1011
const ACT_COMMAND = 'command';

0 commit comments

Comments
 (0)