|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Conversation; |
|
6
|
|
|
|
|
7
|
|
|
use FondBot\Chat; |
|
8
|
|
|
use FondBot\User; |
|
9
|
|
|
use FondBot\Channel; |
|
10
|
|
|
use FondBot\FondBot; |
|
11
|
|
|
use FondBot\Conversation; |
|
12
|
|
|
use Illuminate\Support\Facades\Cache; |
|
13
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
14
|
|
|
|
|
15
|
|
|
class Context implements Arrayable |
|
16
|
|
|
{ |
|
17
|
|
|
private $channel; |
|
18
|
|
|
private $chat; |
|
19
|
|
|
private $user; |
|
20
|
|
|
private $intent; |
|
21
|
|
|
private $interaction; |
|
22
|
9 |
|
private $items; |
|
23
|
|
|
private $attempts = 0; |
|
24
|
9 |
|
|
|
25
|
9 |
|
public function __construct(Channel $channel, Chat $chat, User $user, array $items = []) |
|
26
|
9 |
|
{ |
|
27
|
9 |
|
$this->channel = $channel; |
|
28
|
9 |
|
$this->chat = $chat; |
|
29
|
|
|
$this->user = $user; |
|
30
|
4 |
|
$this->items = collect($items); |
|
31
|
|
|
} |
|
32
|
4 |
|
|
|
33
|
|
|
public static function load(Channel $channel, Chat $chat, User $user) |
|
34
|
|
|
{ |
|
35
|
4 |
|
$value = Cache::get(static::getCacheKey($channel, $chat, $user), [ |
|
36
|
|
|
'chat' => $chat, |
|
37
|
4 |
|
'user' => $user, |
|
38
|
|
|
'intent' => null, |
|
39
|
|
|
'interaction' => null, |
|
40
|
4 |
|
'items' => [], |
|
41
|
|
|
]); |
|
42
|
4 |
|
|
|
43
|
|
|
$context = new static($channel, $chat, $user, $value['items'] ?? []); |
|
44
|
|
|
|
|
45
|
2 |
|
if (isset($value['intent'])) { |
|
46
|
|
|
$context->setIntent(resolve($value['intent'])); |
|
|
|
|
|
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (isset($value['interaction'])) { |
|
50
|
2 |
|
$context->setInteraction(resolve($value['interaction'])); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
2 |
|
|
|
53
|
|
|
return $context; |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function save(): void |
|
57
|
2 |
|
{ |
|
58
|
|
|
Cache::put(static::getCacheKey($this->channel, $this->chat, $this->user), $this->toArray(), FondBot::$contextTtl); |
|
|
|
|
|
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function delete(): void |
|
62
|
2 |
|
{ |
|
63
|
|
|
Cache::forget(static::getCacheKey($this->channel, $this->chat, $this->user)); |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
public function getChannel(): Channel |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->channel; |
|
69
|
5 |
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
public function getChat(): Chat |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->chat; |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
public function getUser(): User |
|
77
|
|
|
{ |
|
78
|
3 |
|
return $this->user; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getIntent(): ?Intent |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->intent; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function setIntent(Intent $intent): Context |
|
87
|
|
|
{ |
|
88
|
|
|
$this->intent = $intent; |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
public function getInteraction(): ?Interaction |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->interaction; |
|
96
|
2 |
|
} |
|
97
|
2 |
|
|
|
98
|
2 |
|
public function setInteraction(?Interaction $interaction): Context |
|
99
|
2 |
|
{ |
|
100
|
|
|
$this->interaction = $interaction; |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getItem(string $key, $default = null) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->items->get($key, $default); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function setItem(string $key, $value): Context |
|
111
|
|
|
{ |
|
112
|
|
|
$this->items->put($key, $value); |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function incrementAttempts(): Context |
|
118
|
|
|
{ |
|
119
|
|
|
$this->attempts++; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function attempts(): int |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->attempts; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function toArray(): array |
|
130
|
|
|
{ |
|
131
|
|
|
return [ |
|
132
|
|
|
'intent' => $this->intent ? get_class($this->intent) : null, |
|
133
|
|
|
'interaction' => $this->interaction ? get_class($this->interaction) : null, |
|
134
|
|
|
'items' => $this->items->toArray(), |
|
135
|
|
|
'attempts' => $this->attempts, |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function __destruct() |
|
140
|
|
|
{ |
|
141
|
|
|
if (Conversation::instance()->transitioned()) { |
|
142
|
|
|
$this->save(); |
|
143
|
|
|
} else { |
|
144
|
|
|
$this->delete(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
private static function getCacheKey(Channel $channel, Chat $chat, User $user): string |
|
149
|
|
|
{ |
|
150
|
|
|
return implode('.', ['context', $channel->getName(), $chat->getId(), $user->getId()]); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|