|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tgallice\Wit; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Tgallice\Wit\Model\Context; |
|
7
|
|
|
|
|
8
|
|
|
class Api |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Client client |
|
12
|
|
|
*/ |
|
13
|
|
|
private $client; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param Client $client |
|
17
|
|
|
*/ |
|
18
|
8 |
|
public function __construct(Client $client) |
|
19
|
|
|
{ |
|
20
|
8 |
|
$this->client = $client; |
|
21
|
8 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $sessionId |
|
25
|
|
|
* @param string $text |
|
26
|
|
|
* @param Context|null $context |
|
27
|
|
|
* |
|
28
|
|
|
* @return array|null |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function getConverseNextStep($sessionId, $text, Context $context = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$query = [ |
|
33
|
1 |
|
'session_id' => $sessionId, |
|
34
|
1 |
|
'q' => $text, |
|
35
|
1 |
|
]; |
|
36
|
|
|
|
|
37
|
1 |
|
$response = $this->client->send('POST', '/converse', $context, $query); |
|
38
|
|
|
|
|
39
|
1 |
|
return $this->decodeResponse($response); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $text |
|
44
|
|
|
* @param Context|null $context |
|
45
|
|
|
* @param array $extraParams |
|
46
|
|
|
* |
|
47
|
|
|
* @return array|null |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function getIntentByText($text, Context $context = null, array $extraParams = []) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$query = array_merge($extraParams, [ |
|
52
|
2 |
|
'q' => $text, |
|
53
|
2 |
|
]); |
|
54
|
|
|
|
|
55
|
2 |
|
if (null !== $context) { |
|
56
|
2 |
|
$query['context'] = json_encode($context); |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
$response = $this->client->get('/message', $query); |
|
60
|
|
|
|
|
61
|
2 |
|
return $this->decodeResponse($response); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string|resource $file |
|
66
|
|
|
* @param array|null $context |
|
67
|
|
|
* @param array $queryParams |
|
68
|
|
|
* |
|
69
|
|
|
* @return array|null |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function getIntentBySpeech($file, Context $context = null, array $queryParams = []) |
|
72
|
|
|
{ |
|
73
|
3 |
|
if (!$file || (!is_resource($file) && !is_readable($file))) { |
|
74
|
1 |
|
throw new \InvalidArgumentException('$file argument must be a readable file path or a valid resource'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
if (null !== $context) { |
|
78
|
2 |
|
$queryParams['context'] = json_encode($context); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
$file = is_resource($file) ? $file : fopen($file, 'r'); |
|
82
|
2 |
|
$response = $this->client->send('POST', '/speech', $file, $queryParams); |
|
83
|
|
|
|
|
84
|
2 |
|
return $this->decodeResponse($response); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $messageId |
|
89
|
|
|
* |
|
90
|
|
|
* @return array|null |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getMessage($messageId) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$response = $this->client->get(sprintf('/messages/%s', $messageId)); |
|
95
|
|
|
|
|
96
|
1 |
|
return $this->decodeResponse($response); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return Client |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getClient() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->client; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @deprecated This method is deprecated as of 0.1 and will be removed in 1.0. |
|
109
|
|
|
* You should use the ResponseHandler::decodeResponse() instead |
|
110
|
|
|
* |
|
111
|
|
|
* @param ResponseInterface $response |
|
112
|
|
|
* |
|
113
|
|
|
* @return array|null |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function decodeResponse(ResponseInterface $response) |
|
116
|
|
|
{ |
|
117
|
|
|
return json_decode((string) $response->getBody(), true); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.