1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Conversation; |
6
|
|
|
|
7
|
|
|
use FondBot\Channels\Chat; |
8
|
|
|
use FondBot\Channels\User; |
9
|
|
|
use FondBot\Channels\Channel; |
10
|
|
|
use Illuminate\Cache\Repository; |
11
|
|
|
use FondBot\Events\MessageReceived; |
12
|
|
|
use FondBot\Contracts\Conversation\Manager; |
13
|
|
|
|
14
|
|
|
class ConversationManager implements Manager |
15
|
|
|
{ |
16
|
|
|
private $intents = []; |
17
|
|
|
private $fallbackIntent; |
18
|
|
|
|
19
|
|
|
private $cache; |
20
|
|
|
|
21
|
68 |
|
public function __construct(Repository $cache) |
22
|
|
|
{ |
23
|
68 |
|
$this->cache = $cache; |
24
|
68 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Register intent. |
28
|
|
|
* |
29
|
|
|
* @param string $class |
30
|
|
|
*/ |
31
|
|
|
public function registerIntent(string $class): void |
32
|
|
|
{ |
33
|
|
|
$this->intents[] = $class; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Register fallback intent. |
38
|
|
|
* |
39
|
|
|
* @param string $class |
40
|
|
|
*/ |
41
|
68 |
|
public function registerFallbackIntent(string $class): void |
42
|
|
|
{ |
43
|
68 |
|
$this->fallbackIntent = $class; |
44
|
68 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get all registered intents. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function getIntents(): array |
52
|
|
|
{ |
53
|
|
|
return $this->intents; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Match intent by received message. |
58
|
|
|
* |
59
|
|
|
* @param MessageReceived $messageReceived |
60
|
|
|
* |
61
|
|
|
* @return Intent|null |
62
|
|
|
*/ |
63
|
|
|
public function matchIntent(MessageReceived $messageReceived): ?Intent |
64
|
|
|
{ |
65
|
|
|
foreach ($this->intents as $intent) { |
66
|
|
|
/** @var Intent $intent */ |
67
|
|
|
$intent = resolve($intent); |
|
|
|
|
68
|
|
|
|
69
|
|
|
foreach ($intent->activators() as $activator) { |
70
|
|
|
if ($activator->matches($messageReceived) && $intent->passesAuthorization($messageReceived)) { |
71
|
|
|
return $intent; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Otherwise, return fallback intent |
77
|
|
|
return resolve($this->fallbackIntent); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Resolve conversation context. |
82
|
|
|
* |
83
|
|
|
* @param Channel $channel |
84
|
|
|
* @param Chat $chat |
85
|
|
|
* @param User $user |
86
|
|
|
* |
87
|
|
|
* @return Context|null |
88
|
|
|
*/ |
89
|
|
|
public function resolveContext(Channel $channel, Chat $chat, User $user): Context |
90
|
|
|
{ |
91
|
|
|
$value = $this->cache->get($this->getCacheKeyForContext($channel, $chat, $user), [ |
92
|
|
|
'chat' => $chat, |
93
|
|
|
'user' => $user, |
94
|
|
|
'intent' => null, |
95
|
|
|
'interaction' => null, |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$context = new Context($channel, $chat, $user); |
99
|
|
|
|
100
|
|
|
if ($value['intent'] !== null) { |
101
|
|
|
$context->setIntent($value['intent']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($value['interaction'] !== null) { |
105
|
|
|
$context->setInteraction($value['interaction']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $context; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Save context. |
113
|
|
|
* |
114
|
|
|
* @param Context $context |
115
|
|
|
*/ |
116
|
|
|
public function saveContext(Context $context): void |
117
|
|
|
{ |
118
|
|
|
$this->cache->forever( |
119
|
|
|
$this->getCacheKeyForContext($context->getChannel(), $context->getChat(), $context->getUser()), |
120
|
|
|
$context->toArray() |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function getCacheKeyForContext(Channel $channel, Chat $chat, User $user): string |
125
|
|
|
{ |
126
|
|
|
return implode('.', ['context', $channel->getName(), $chat->getId(), $user->getId()]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: