-
-
Notifications
You must be signed in to change notification settings - Fork 962
/
Copy pathConversation.php
264 lines (234 loc) · 5.96 KB
/
Conversation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot;
use Longman\TelegramBot\Exception\TelegramException;
/**
* Class Conversation
*
* Only one conversation can be active at any one time.
* A conversation is directly linked to a user, chat and the command that is managing the conversation.
*/
class Conversation
{
/**
* All information fetched from the database
*
* @var array|null
*/
protected $conversation;
/**
* Notes stored inside the conversation
*
* @var mixed
*/
protected $protected_notes;
/**
* Notes to be stored
*
* @var mixed
*/
public $notes;
/**
* Telegram user id
*
* @var int
*/
protected $user_id;
/**
* Telegram chat id
*
* @var int
*/
protected $chat_id;
/**
* Command to be executed if the conversation is active
*
* @var string
*/
protected $command;
/**
* Conversation constructor to initialize a new conversation
*
* @param int $user_id
* @param int $chat_id
* @param string $command
*
* @throws TelegramException
*/
public function __construct(int $user_id, int $chat_id, string $command = '')
{
$this->user_id = $user_id;
$this->chat_id = $chat_id;
$this->command = $command;
//Try to load an existing conversation if possible
if (!$this->load() && $command !== '') {
//A new conversation start
$this->start();
}
}
/**
* Clear all conversation variables.
*
* @return bool Always return true, to allow this method in an if statement.
*/
protected function clear(): bool
{
$this->conversation = null;
$this->protected_notes = null;
$this->notes = null;
return true;
}
/**
* Load the conversation from the database
*
* @return bool
* @throws TelegramException
*/
protected function load(): bool
{
//Select an active conversation
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
if (isset($conversation[0])) {
//Pick only the first element
$this->conversation = $conversation[0];
//Load the command from the conversation if it hasn't been passed
$this->command = $this->command ?: $this->conversation['command'];
if ($this->command !== $this->conversation['command']) {
$this->cancel();
return false;
}
//Load the conversation notes
$this->protected_notes = json_decode($this->conversation['notes'], true);
$this->notes = $this->protected_notes;
}
return $this->exists();
}
/**
* Check if the conversation already exists
*
* @return bool
*/
public function exists(): bool
{
return $this->conversation !== null;
}
/**
* Start a new conversation if the current command doesn't have one yet
*
* @return bool
* @throws TelegramException
*/
protected function start(): bool
{
if (
$this->command
&& !$this->exists()
&& ConversationDB::insertConversation(
$this->user_id,
$this->chat_id,
$this->command
)
) {
return $this->load();
}
return false;
}
/**
* Delete the current conversation
*
* Currently the Conversation is not deleted but just set to 'stopped'
*
* @return bool
* @throws TelegramException
*/
public function stop(): bool
{
return $this->updateStatus('stopped') && $this->clear();
}
/**
* Cancel the current conversation
*
* @return bool
* @throws TelegramException
*/
public function cancel(): bool
{
return $this->updateStatus('cancelled') && $this->clear();
}
/**
* Update the status of the current conversation
*
* @param string $status
*
* @return bool
* @throws TelegramException
*/
protected function updateStatus(string $status): bool
{
if ($this->exists()) {
$fields = ['status' => $status];
$where = [
'id' => $this->conversation['id'],
'status' => 'active',
'user_id' => $this->user_id,
'chat_id' => $this->chat_id,
];
if (ConversationDB::updateConversation($fields, $where)) {
return true;
}
}
return false;
}
/**
* Store the array/variable in the database with json_encode() function
*
* @return bool
* @throws TelegramException
*/
public function update(): bool
{
if ($this->exists()) {
$fields = ['notes' => json_encode($this->notes, JSON_UNESCAPED_UNICODE)];
//I can update a conversation whatever the state is
$where = ['id' => $this->conversation['id']];
if (ConversationDB::updateConversation($fields, $where)) {
return true;
}
}
return false;
}
/**
* Retrieve the command to execute from the conversation
*
* @return string
*/
public function getCommand(): string
{
return $this->command;
}
/**
* Retrieve the user id
*
* @return int
*/
public function getUserId(): int
{
return $this->user_id;
}
/**
* Retrieve the chat id
*
* @return int
*/
public function getChatId(): int
{
return $this->chat_id;
}
}