|
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', $query, $context); |
|
38
|
|
|
|
|
39
|
1 |
|
return $this->transform($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->send('GET', 'message', $query); |
|
60
|
|
|
|
|
61
|
2 |
|
return $this->transform($response); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string|resource $file |
|
66
|
|
|
* @param array|null $context |
|
67
|
|
|
* @param array $extraParams |
|
68
|
|
|
* |
|
69
|
|
|
* @return array|null |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function getIntentBySpeech($file, Context $context = null, array $extraParams = []) |
|
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 |
|
$extraParams['context'] = json_encode($context); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
$file = is_resource($file) ? $file : fopen($file, 'r+'); |
|
82
|
|
|
|
|
83
|
2 |
|
$response = $this->client->send('POST', 'speech', $extraParams, $file); |
|
84
|
|
|
|
|
85
|
2 |
|
return $this->transform($response); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $messageId |
|
90
|
|
|
* |
|
91
|
|
|
* @return array|null |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function getMessage($messageId) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$response = $this->client->send('GET', sprintf('messages/%s', $messageId)); |
|
96
|
|
|
|
|
97
|
1 |
|
return $this->transform($response); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param ResponseInterface $response |
|
102
|
|
|
* |
|
103
|
|
|
* @return array|null |
|
104
|
|
|
*/ |
|
105
|
6 |
|
protected function transform(ResponseInterface $response) |
|
106
|
|
|
{ |
|
107
|
6 |
|
return json_decode((string) $response->getBody(), true); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|