|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tgallice\Wit; |
|
4
|
|
|
|
|
5
|
|
|
use Tgallice\Wit\Exception\BadResponseException; |
|
6
|
|
|
use Tgallice\Wit\Exception\ConversationException; |
|
7
|
|
|
use Tgallice\Wit\Exception\InvalidStepException; |
|
8
|
|
|
use Tgallice\Wit\Model\Context; |
|
9
|
|
|
use Tgallice\Wit\Model\Step; |
|
10
|
|
|
|
|
11
|
|
|
class Conversation |
|
12
|
|
|
{ |
|
13
|
|
|
const MAX_STEPS_ITERATION = 5; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var API |
|
17
|
|
|
*/ |
|
18
|
|
|
private $api; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ActionMapping |
|
22
|
|
|
*/ |
|
23
|
|
|
private $actionMapping; |
|
24
|
|
|
|
|
25
|
7 |
|
public function __construct(Api $api, ActionMapping $actionMapping) |
|
26
|
|
|
{ |
|
27
|
7 |
|
$this->api = $api; |
|
28
|
7 |
|
$this->actionMapping = $actionMapping; |
|
29
|
7 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $sessionId |
|
33
|
|
|
* @param string|null $message |
|
34
|
|
|
* @param Context|null $context |
|
35
|
|
|
* @param int $maxStepsIteration |
|
36
|
|
|
* |
|
37
|
|
|
* @return Context $context |
|
38
|
|
|
* |
|
39
|
|
|
* @throws ConversationException |
|
40
|
|
|
*/ |
|
41
|
6 |
|
public function converse($sessionId, $message = null, Context $context = null, $maxStepsIteration = self::MAX_STEPS_ITERATION) |
|
42
|
|
|
{ |
|
43
|
6 |
|
$context = (null !== $context) ? $context : new Context(); |
|
44
|
|
|
|
|
45
|
6 |
|
if ($maxStepsIteration === 0) { |
|
46
|
1 |
|
throw new ConversationException($sessionId, "Max iteration exceeded", $context); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
6 |
|
$step = $this->getNextStep($sessionId, $message, $context); |
|
50
|
|
|
|
|
51
|
3 |
|
switch ($step->getType()) { |
|
52
|
3 |
|
case Step::TYPE_MERGE: |
|
53
|
2 |
|
$newContext = $this->actionMapping->merge($sessionId, $context, $step->getEntities()); |
|
|
|
|
|
|
54
|
2 |
|
$context = $this->converse($sessionId, null, $newContext, --$maxStepsIteration); |
|
55
|
1 |
|
break; |
|
56
|
2 |
|
case Step::TYPE_MESSAGE: |
|
57
|
1 |
|
$this->actionMapping->say($sessionId, $step->getMessage(), $context); |
|
|
|
|
|
|
58
|
1 |
|
break; |
|
59
|
1 |
|
case Step::TYPE_ACTION: |
|
60
|
|
|
$newContext = $this->actionMapping->action($sessionId, $step->getAction(), $context); |
|
|
|
|
|
|
61
|
|
|
$context = $this->converse($sessionId, null, $newContext, --$maxStepsIteration); |
|
62
|
|
|
break; |
|
63
|
1 |
|
case Step::TYPE_STOP: |
|
64
|
1 |
|
$this->actionMapping->stop($sessionId, $context); |
|
65
|
1 |
|
break; |
|
66
|
|
|
default: |
|
67
|
|
|
$this->actionMapping->error($sessionId, $context, 'Unknown error', $step); |
|
68
|
2 |
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
return $context; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $sessionId |
|
75
|
|
|
* @param string|null $message |
|
76
|
|
|
* @param Context $context |
|
77
|
|
|
* |
|
78
|
|
|
* @return Step Return the step sequence |
|
79
|
|
|
* |
|
80
|
|
|
* @throws BadResponseException |
|
81
|
|
|
*/ |
|
82
|
6 |
|
private function getNextStep($sessionId, $message, Context $context) |
|
83
|
|
|
{ |
|
84
|
6 |
|
$response = $this->api->getConverseNextStep($sessionId, $message, $context); |
|
85
|
|
|
|
|
86
|
6 |
|
if (200 !== $response->getStatusCode()) { |
|
87
|
1 |
|
throw new ConversationException($sessionId, $response->getReasonPhrase(), $context); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
5 |
|
$step = json_decode((string) $response->getBody(), true); |
|
91
|
|
|
|
|
92
|
5 |
|
if (null === $step) { |
|
93
|
|
|
$step = []; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
if (isset($step['error'])) { |
|
97
|
1 |
|
throw new ConversationException($sessionId, $step['error'], $context, $step); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
try { |
|
101
|
4 |
|
$step = StepFactory::create($step); |
|
102
|
4 |
|
} catch (InvalidStepException $e) { |
|
103
|
1 |
|
throw new ConversationException($sessionId, $e->getMessage(), $context, $e->getStepData()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
return $step; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: