1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DanySpin97\PhpBotFramework; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* \class Bot Bot |
7
|
|
|
* \brief Bot class to handle updates and commandes. |
8
|
|
|
* \details Class Bot to handle task like api request, or more specific api function(sendMessage, editMessageText, etc). |
9
|
|
|
* Usage example in webhook.php |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class Bot extends CoreBot { |
13
|
|
|
|
14
|
|
|
use LongPolling, |
15
|
|
|
MessageCommand, |
16
|
|
|
CallbackCommand, |
17
|
|
|
DatabaseHandler, |
18
|
|
|
BotState, |
19
|
|
|
Localization; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* \addtogroup Bot Bot |
23
|
|
|
* \brief Properties and methods to handle the TelegramBot. |
24
|
|
|
* \details Here are listed all the properties and methods that will help the developer create the basic bot functions. |
25
|
|
|
* @{ |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** \brief Text received in messages */ |
29
|
|
|
protected $_text; |
30
|
|
|
|
31
|
|
|
/** \brief Data received in callback query */ |
32
|
|
|
protected $_data; |
33
|
|
|
|
34
|
|
|
/** \brief Query sent by the user in the inline query */ |
35
|
|
|
protected $_query; |
36
|
|
|
|
37
|
|
|
/** \brief Store the inline keyboard */ |
38
|
|
|
public $keyboard; |
39
|
|
|
|
40
|
|
|
/** \brief Pdo reference */ |
41
|
|
|
public $pdo; |
42
|
|
|
|
43
|
|
|
/** \brief Redis connection */ |
44
|
|
|
public $redis; |
45
|
|
|
|
46
|
|
|
/** @} */ |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* \addtogroup Bot |
51
|
|
|
* @{ |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* \brief Construct an empy bot. |
56
|
|
|
* \details Construct a bot with commands, multilanguage and status. |
57
|
|
|
*/ |
58
|
|
|
public function __construct(string $token) { |
59
|
|
|
|
60
|
|
|
// Parent constructor |
61
|
|
|
parent::__construct($token); |
62
|
|
|
|
63
|
|
|
// Initialize to an empty array |
64
|
|
|
$this->_message_commands = []; |
65
|
|
|
$this->_callback_commands = []; |
66
|
|
|
|
67
|
|
|
$this->keyboard = new InlineKeyboard($this); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** \brief Descruct the class. */ |
72
|
|
|
public function __destruct() { |
73
|
|
|
// Close redis connection if it is open |
74
|
|
|
if (isset($this->redis)) { |
75
|
|
|
|
76
|
|
|
$this->redis->close(); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* \brief Get the text of the message, if set (for updates of type "message"). |
84
|
|
|
* @return Text of the message, empty string if not set. |
85
|
|
|
*/ |
86
|
|
|
public function getMessageText() : string { |
87
|
|
|
|
88
|
|
|
if (isset($this->_text)) { |
89
|
|
|
|
90
|
|
|
return $this->_text; |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return ''; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* \brief Get the data of callback query, if set (for updates of type "callback_query"). |
100
|
|
|
* @return Data of the callback query, empty string if not set. |
101
|
|
|
*/ |
102
|
|
|
public function getCallbackData() : string { |
103
|
|
|
|
104
|
|
|
if (isset($this->_data)) { |
105
|
|
|
|
106
|
|
|
return $this->_data; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return ''; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* \brief Get the query received from the inline query (for updates of type "inline_query"). |
116
|
|
|
* @return The query sent by the user, throw exception if the current update is not an inline query. |
117
|
|
|
*/ |
118
|
|
|
public function getInlineQuery() : string { |
119
|
|
|
|
120
|
|
|
if (isset($this->_query)) { |
121
|
|
|
|
122
|
|
|
return $this->_query; |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
throw new BotException("Query from inline query is not set: wrong update type"); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* \brief Get update and process it. |
131
|
|
|
* \details Call this method if you are using webhook. |
132
|
|
|
* It will get update from php::\input, check it and then process it using processUpdate. |
133
|
|
|
*/ |
134
|
|
|
public function processWebhookUpdate() { |
135
|
|
|
|
136
|
|
|
$this->initBot(); |
137
|
|
|
|
138
|
|
|
$this->processUpdate(json_decode(file_get_contents('php://input'), true)); |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** @} */ |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* \addtogroup Core Core(Internal) |
146
|
|
|
* @{ |
147
|
|
|
*/ |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* \brief Init variables to skip parsing commands if there aren't any. |
151
|
|
|
* \details Called internnaly by |
152
|
|
|
* - <code>getUpdatesLocal</code> |
153
|
|
|
* - <code>getUpdatesRedis</code> |
154
|
|
|
* - <code>getUpdatesDatabase</code> |
155
|
|
|
* - <code>processWebhookUpdate</code> |
156
|
|
|
*/ |
157
|
|
|
private function initBot() { |
158
|
|
|
|
159
|
|
|
// Are there message commands? |
160
|
|
|
$this->_message_commands_set = !empty($this->_message_commands); |
161
|
|
|
|
162
|
|
|
// Are there callback commands? |
163
|
|
|
$this->_callback_commands_set = !empty($this->_callback_commands); |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* \brief Dispatch each update to the right method (processMessage, processCallbackQuery, etc). |
169
|
|
|
* \details Set $chat_id for each update, $text, $data and $query are set for each update that contains them. |
170
|
|
|
* It also calls commands for each updates, before process methods. |
171
|
|
|
* @param $update Reference to the update received. |
172
|
|
|
* @return The id of the update processed. |
173
|
|
|
*/ |
174
|
|
|
public function processUpdate(array $update) : int { |
175
|
|
|
|
176
|
|
|
if (isset($update['message'])) { |
177
|
|
|
|
178
|
|
|
// Set data from the message |
179
|
|
|
$this->_chat_id = $update['message']['chat']['id']; |
180
|
|
|
|
181
|
|
|
// If the message contains text |
182
|
|
|
if (isset($update['message']['text'])) { |
183
|
|
|
|
184
|
|
|
$this->_text = $update['message']['text']; |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// If there are commands set by the user |
189
|
|
|
// and there are bot commands in the message, checking message entities |
190
|
|
|
if ($this->_message_commands_set && isset($update['message']['entities']) && $update['message']['entities'][0]['type'] === 'bot_command') { |
191
|
|
|
|
192
|
|
|
// The lenght of the command |
193
|
|
|
$length = $update['message']['entities'][0]['length']; |
194
|
|
|
|
195
|
|
|
// Offset of the command |
196
|
|
|
$offset = $update['message']['entities'][0]['offset']; |
197
|
|
|
|
198
|
|
|
// For each command added by the user |
199
|
|
|
foreach ($this->_message_commands as $trigger) { |
200
|
|
|
|
201
|
|
|
// If the current command is a regex |
202
|
|
|
if ($trigger['regex_active']) { |
203
|
|
|
|
204
|
|
|
// Use preg_match to check if it is true |
205
|
|
|
$matched = preg_match('/' . $trigger['regex_rule'] . '/', substr($update['message']['text'], $offset + 1, $length)); |
206
|
|
|
|
207
|
|
|
// else check if the command sent by the user is the same as the one we are expecting |
208
|
|
|
} else if ($trigger['length'] == $length && mb_strpos($trigger['command'], $update['message']['text'], $offset) !== false) { |
209
|
|
|
|
210
|
|
|
// We found a valid command |
211
|
|
|
$matched = true; |
212
|
|
|
|
213
|
|
|
} else { |
214
|
|
|
|
215
|
|
|
// We did not |
216
|
|
|
$matched = false; |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// Check the results for the current command |
221
|
|
|
if ($matched) { |
222
|
|
|
|
223
|
|
|
// Execute script, |
224
|
|
|
$trigger['script']($this, $update['message']); |
225
|
|
|
|
226
|
|
|
// clear text variable |
227
|
|
|
unset($this->_text); |
228
|
|
|
|
229
|
|
|
// and return the id of the current update to stop processing this update |
230
|
|
|
return $update['update_id']; |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// And process it |
239
|
|
|
$this->processMessage($update['message']); |
240
|
|
|
|
241
|
|
|
// clear text variable |
242
|
|
|
unset($this->_text); |
243
|
|
|
|
244
|
|
|
// If the update is a callback query |
245
|
|
|
} elseif (isset($update['callback_query'])) { |
246
|
|
|
|
247
|
|
|
// Set variables |
248
|
|
|
$this->_chat_id = $update['callback_query']['message']['chat']['id']; |
249
|
|
|
$this->_callback_query_id = $update['callback_query']['id']; |
250
|
|
|
|
251
|
|
|
// If data is set for the current callback query |
252
|
|
|
if (isset($update['callback_query']['data'])) { |
253
|
|
|
|
254
|
|
|
$this->_data = $update['callback_query']['data']; |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// Check for callback commands |
259
|
|
|
if (isset($this->_data) && $this->_callback_commands_set) { |
260
|
|
|
|
261
|
|
|
// Parse all commands |
262
|
|
|
foreach ($this->_callback_commands as $trigger) { |
263
|
|
|
|
264
|
|
|
// If command is found in callback data |
265
|
|
|
if (strpos($trigger['data'], $this->_data) !== false) { |
266
|
|
|
|
267
|
|
|
// Trigger the script |
268
|
|
|
$trigger['script']($this, $update['callback_query']); |
269
|
|
|
|
270
|
|
|
// Clear data |
271
|
|
|
unset($this->_data); |
272
|
|
|
unset($this->_callback_query_id); |
273
|
|
|
|
274
|
|
|
// and return the id of the current update |
275
|
|
|
return $update['update_id']; |
276
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// Process the callback query through processCallbackQuery |
284
|
|
|
$this->processCallbackQuery($update['callback_query']); |
285
|
|
|
|
286
|
|
|
// Unset callback query variables |
287
|
|
|
unset($this->_callback_query_id); |
288
|
|
|
unset($this->_data); |
289
|
|
|
|
290
|
|
|
} elseif (isset($update['inline_query'])) { |
291
|
|
|
|
292
|
|
|
$this->_chat_id = $update['inline_query']['from']['id']; |
293
|
|
|
$this->_query = $update['inline_query']['query']; |
294
|
|
|
$this->_inline_query_id = $update['inline_query']['id']; |
295
|
|
|
|
296
|
|
|
$this->processInlineQuery($update['inline_query']); |
297
|
|
|
|
298
|
|
|
unset($this->_query); |
299
|
|
|
unset($this->_inline_query_id); |
300
|
|
|
|
301
|
|
|
} elseif (isset($update['channel_post'])) { |
302
|
|
|
|
303
|
|
|
// Set data from the post |
304
|
|
|
$this->_chat_id = $update['channel_post']['chat']['id']; |
305
|
|
|
|
306
|
|
|
$this->processChannelPost($update['channel_post']); |
307
|
|
|
|
308
|
|
|
} elseif (isset($update['edited_message'])) { |
309
|
|
|
|
310
|
|
|
$this->_chat_id = $update['edited_message']['chat']['id']; |
311
|
|
|
|
312
|
|
|
$this->processEditedMessage($update['edited_message']); |
313
|
|
|
|
314
|
|
|
} elseif (isset($update['edited_channel_post'])) { |
315
|
|
|
|
316
|
|
|
$this->_chat_id = $update['edited_channel_post']['chat']['id']; |
317
|
|
|
|
318
|
|
|
$this->processEditedChannelPost($update['edited_channel_post']); |
319
|
|
|
|
320
|
|
|
} elseif (isset($update['chosen_inline_result'])) { |
321
|
|
|
|
322
|
|
|
$this->_chat_id = $update['chosen_inline_result']['chat']['id']; |
323
|
|
|
|
324
|
|
|
$this->processChosenInlineResult($update['chosen_inline_result']); |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $update['update_id']; |
329
|
|
|
|
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** @} */ |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* \addtogroup Bot Bot |
336
|
|
|
* @{ |
337
|
|
|
*/ |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* \brief Called every message received by the bot. |
341
|
|
|
* \details Override it to script the bot answer for each message. |
342
|
|
|
* <code>$chat_id</code> and <code>$text</code>, if the message contains text(use getMessageText() to access it), set inside of this function. |
343
|
|
|
* @param $message Reference to the message received. |
344
|
|
|
*/ |
345
|
|
|
protected function processMessage($message) { |
|
|
|
|
346
|
|
|
|
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* \brief Called every callback query received by the bot. |
351
|
|
|
* \details Override it to script the bot answer for each callback. |
352
|
|
|
* <code>$chat_id</code> and <code>$data</code>, if set in the callback query(use getCallbackData() to access it) set inside of this function. |
353
|
|
|
* @param $callback_query Reference to the callback query received. |
354
|
|
|
*/ |
355
|
|
|
protected function processCallbackQuery($callback_query) { |
|
|
|
|
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* \brief Called every inline query received by the bot. |
361
|
|
|
* \details Override it to script the bot answer for each inline query. |
362
|
|
|
* $chat_id and $query(use getInlineQuery() to access it) set inside of this function. |
363
|
|
|
* @param $inline_query Reference to the inline query received. |
364
|
|
|
*/ |
365
|
|
|
protected function processInlineQuery($inline_query) { |
|
|
|
|
366
|
|
|
|
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* \brief Called every chosen inline result received by the bot. |
371
|
|
|
* \details Override it to script the bot answer for each chosen inline result. |
372
|
|
|
* <code>$chat_id</code> set inside of this function. |
373
|
|
|
* @param $chosen_inline_result Reference to the chosen inline result received. |
374
|
|
|
*/ |
375
|
|
|
protected function processChosenInlineResult($chosen_inline_result) { |
|
|
|
|
376
|
|
|
|
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* \brief Called every chosen edited message received by the bot. |
381
|
|
|
* \details Override it to script the bot answer for each edited message. |
382
|
|
|
* <code>$chat_id</code> set inside of this function. |
383
|
|
|
* @param $edited_message The message edited by the user. |
384
|
|
|
*/ |
385
|
|
|
protected function processEditedMessage($edited_message) { |
|
|
|
|
386
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* \brief Called every new post in the channel where the bot is in. |
391
|
|
|
* \details Override it to script the bot answer for each post sent in a channel. |
392
|
|
|
* <code>$chat_id</code> set inside of this function. |
393
|
|
|
* @param $post The message sent in the channel. |
394
|
|
|
*/ |
395
|
|
|
protected function processChannelPost($post) { |
|
|
|
|
396
|
|
|
|
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* \brief Called every time a post get edited in the channel where the bot is in. |
401
|
|
|
* \details Override it to script the bot answer for each post edited in a channel. |
402
|
|
|
* <code>$chat_id</code> set inside of this function. |
403
|
|
|
* @param $post The message edited in the channel. |
404
|
|
|
*/ |
405
|
|
|
protected function processEditedChannelPost($edited_post) { |
|
|
|
|
406
|
|
|
|
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** @} */ |
410
|
|
|
|
411
|
|
|
} |
412
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.