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