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
|
|
|
$postParams = [ |
33
|
1 |
|
'session_id' => $sessionId, |
34
|
1 |
|
'q' => $text, |
35
|
1 |
|
'context' => $context, |
36
|
1 |
|
]; |
37
|
|
|
|
38
|
1 |
|
$response = $this->client->post('/converse', $postParams); |
39
|
|
|
|
40
|
1 |
|
return $this->decodeResponse($response); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $text |
45
|
|
|
* @param Context|null $context |
46
|
|
|
* @param array $extraParams |
47
|
|
|
* |
48
|
|
|
* @return array|null |
49
|
|
|
*/ |
50
|
2 |
|
public function getIntentByText($text, Context $context = null, array $extraParams = []) |
51
|
|
|
{ |
52
|
2 |
|
$query = array_merge($extraParams, [ |
53
|
2 |
|
'q' => $text, |
54
|
2 |
|
]); |
55
|
|
|
|
56
|
2 |
|
if (null !== $context) { |
57
|
2 |
|
$query['context'] = json_encode($context); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
2 |
|
$response = $this->client->get('/message', $query); |
61
|
|
|
|
62
|
2 |
|
return $this->decodeResponse($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string|resource $file |
67
|
|
|
* @param array|null $context |
68
|
|
|
* @param array $queryParams |
69
|
|
|
* |
70
|
|
|
* @return array|null |
71
|
|
|
*/ |
72
|
3 |
|
public function getIntentBySpeech($file, Context $context = null, array $queryParams = []) |
73
|
|
|
{ |
74
|
3 |
|
if (!$file || (!is_resource($file) && !is_readable($file))) { |
75
|
1 |
|
throw new \InvalidArgumentException('$file argument must be a readable file path or a valid resource'); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
if (null !== $context) { |
79
|
2 |
|
$queryParams['context'] = json_encode($context); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
2 |
|
$file = is_resource($file) ? $file : fopen($file, 'r+'); |
83
|
|
|
|
84
|
2 |
|
$response = $this->client->send('POST', '/speech', $queryParams, $file); |
85
|
|
|
|
86
|
2 |
|
return $this->decodeResponse($response); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $messageId |
91
|
|
|
* |
92
|
|
|
* @return array|null |
93
|
|
|
*/ |
94
|
1 |
|
public function getMessage($messageId) |
95
|
|
|
{ |
96
|
1 |
|
$response = $this->client->get(sprintf('/messages/%s', $messageId)); |
97
|
|
|
|
98
|
1 |
|
return $this->decodeResponse($response); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ResponseInterface $response |
103
|
|
|
* |
104
|
|
|
* @return array|null |
105
|
|
|
*/ |
106
|
6 |
|
protected function decodeResponse(ResponseInterface $response) |
107
|
|
|
{ |
108
|
6 |
|
return json_decode((string) $response->getBody(), true); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|