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
|
|
|
public function __construct(Api $api, ActionMapping $actionMapping) |
26
|
|
|
{ |
27
|
|
|
$this->api = $api; |
28
|
|
|
$this->actionMapping = $actionMapping; |
29
|
|
|
} |
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
|
|
|
public function converse($sessionId, $message = null, Context $context = null, $maxStepsIteration = self::MAX_STEPS_ITERATION) |
42
|
|
|
{ |
43
|
|
|
$context = (null !== $context) ? $context : new Context(); |
44
|
|
|
|
45
|
|
|
if ($maxStepsIteration === 0) { |
46
|
|
|
throw new ConversationException($sessionId, "Max iteration exceeded", $context); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$step = $this->getNextStep($sessionId, $message, $context); |
50
|
|
|
|
51
|
|
|
switch ($step->getType()) { |
52
|
|
|
case Step::TYPE_MERGE: |
53
|
|
|
$newContext = $this->actionMapping->merge($sessionId, $context, $step->getEntities()); |
|
|
|
|
54
|
|
|
$context = $this->converse($sessionId, null, $newContext, --$maxStepsIteration); |
55
|
|
|
break; |
56
|
|
|
case Step::TYPE_MESSAGE: |
57
|
|
|
$this->actionMapping->say($sessionId, $step->getMessage(), $context); |
|
|
|
|
58
|
|
|
break; |
59
|
|
|
case Step::TYPE_ACTION: |
60
|
|
|
$newContext = $this->actionMapping->action($sessionId, $step->getAction(), $context); |
|
|
|
|
61
|
|
|
$context = $this->converse($sessionId, null, $newContext, --$maxStepsIteration); |
62
|
|
|
break; |
63
|
|
|
case Step::TYPE_STOP: |
64
|
|
|
$this->actionMapping->stop($sessionId, $context); |
65
|
|
|
break; |
66
|
|
|
default: |
67
|
|
|
$this->actionMapping->error($sessionId, $context, 'Unknown error', $step); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
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
|
|
|
private function getNextStep($sessionId, $message, Context $context) |
83
|
|
|
{ |
84
|
|
|
$response = $this->api->getConverseNextStep($sessionId, $message, $context); |
85
|
|
|
|
86
|
|
|
if (200 !== $response->getStatusCode()) { |
87
|
|
|
throw new ConversationException($sessionId, $response->getReasonPhrase(), $context); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$step = json_decode((string) $response->getBody(), true); |
91
|
|
|
|
92
|
|
|
if (null === $step) { |
93
|
|
|
$step = []; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (isset($step['error'])) { |
97
|
|
|
throw new ConversationException($sessionId, $step['error'], $context, $step); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
$step = StepFactory::create($step); |
102
|
|
|
} catch (InvalidStepException $e) { |
103
|
|
|
throw new ConversationException($sessionId, $e->getMessage(), $context, $e->getStepData()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
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: