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 ResponseInterface |
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 |
|
return $this->client->send('POST', 'converse', $query, $context); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $text |
42
|
|
|
* @param Context|null $context |
43
|
|
|
* @param array $extraParams |
44
|
|
|
* |
45
|
|
|
* @return ResponseInterface |
46
|
|
|
*/ |
47
|
2 |
|
public function getIntentByText($text, Context $context = null, array $extraParams = []) |
48
|
|
|
{ |
49
|
2 |
|
$query = array_merge($extraParams, [ |
50
|
2 |
|
'q' => $text, |
51
|
2 |
|
]); |
52
|
|
|
|
53
|
2 |
|
if (null !== $context) { |
54
|
2 |
|
$query['context'] = json_encode($context); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
return $this->client->send('GET', 'message', $query); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string|resource $file |
62
|
|
|
* @param array|null $context |
63
|
|
|
* @param array $extraParams |
64
|
|
|
* |
65
|
|
|
* @return ResponseInterface |
66
|
|
|
*/ |
67
|
3 |
|
public function getIntentBySpeech($file, Context $context = null, array $extraParams = []) |
68
|
|
|
{ |
69
|
3 |
|
if (!$file || (!is_resource($file) && !is_readable($file))) { |
70
|
1 |
|
throw new \InvalidArgumentException('$file argument must be a readable file path or a valid resource'); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
if (null !== $context) { |
74
|
2 |
|
$extraParams['context'] = json_encode($context); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
2 |
|
$file = is_resource($file) ? $file : fopen($file, 'r+'); |
78
|
|
|
|
79
|
2 |
|
return $this->client->send('POST', 'speech', $extraParams, $file); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $messageId |
84
|
|
|
* |
85
|
|
|
* @return ResponseInterface |
86
|
|
|
*/ |
87
|
1 |
|
public function getMessage($messageId) |
88
|
|
|
{ |
89
|
1 |
|
return $this->client->send('GET', sprintf('messages/%s', $messageId)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|