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
|
|
|
* Written by Marco Boretto <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Longman\TelegramBot; |
13
|
|
|
|
14
|
|
|
use Longman\TelegramBot\Entities\CallbackQuery; |
15
|
|
|
use Longman\TelegramBot\Entities\Chat; |
16
|
|
|
use Longman\TelegramBot\Entities\ChosenInlineResult; |
17
|
|
|
use Longman\TelegramBot\Entities\InlineQuery; |
18
|
|
|
use Longman\TelegramBot\Entities\Message; |
19
|
|
|
use Longman\TelegramBot\Entities\Update; |
20
|
|
|
use Longman\TelegramBot\Entities\User; |
21
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
22
|
|
|
use PDO; |
23
|
|
|
use PDOException; |
24
|
|
|
|
25
|
|
|
class DB |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* MySQL credentials |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
static protected $mysql_credentials = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* PDO object |
36
|
|
|
* |
37
|
|
|
* @var PDO |
38
|
|
|
*/ |
39
|
|
|
static protected $pdo; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Table prefix |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
static protected $table_prefix; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Telegram class object |
50
|
|
|
* |
51
|
|
|
* @var \Longman\TelegramBot\Telegram |
52
|
|
|
*/ |
53
|
|
|
static protected $telegram; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize |
57
|
|
|
* |
58
|
|
|
* @param array $credentials Database connection details |
59
|
|
|
* @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
60
|
|
|
* @param string $table_prefix Table prefix |
61
|
|
|
* @param string $encoding Database character encoding |
62
|
|
|
* |
63
|
|
|
* @return PDO PDO database object |
64
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
65
|
|
|
*/ |
66
|
9 |
|
public static function initialize( |
67
|
|
|
array $credentials, |
68
|
|
|
Telegram $telegram, |
69
|
|
|
$table_prefix = null, |
70
|
|
|
$encoding = 'utf8mb4' |
71
|
|
|
) { |
72
|
9 |
|
if (empty($credentials)) { |
73
|
|
|
throw new TelegramException('MySQL credentials not provided!'); |
74
|
|
|
} |
75
|
|
|
|
76
|
9 |
|
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database']; |
77
|
9 |
|
$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $encoding]; |
78
|
|
|
try { |
79
|
9 |
|
$pdo = new PDO($dsn, $credentials['user'], $credentials['password'], $options); |
80
|
9 |
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); |
81
|
|
|
} catch (PDOException $e) { |
82
|
|
|
throw new TelegramException($e->getMessage()); |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
self::$pdo = $pdo; |
86
|
9 |
|
self::$telegram = $telegram; |
87
|
9 |
|
self::$mysql_credentials = $credentials; |
88
|
9 |
|
self::$table_prefix = $table_prefix; |
89
|
|
|
|
90
|
9 |
|
self::defineTable(); |
91
|
|
|
|
92
|
9 |
|
return self::$pdo; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* External Initialize |
97
|
|
|
* |
98
|
|
|
* Let you use the class with an external already existing Pdo Mysql connection. |
99
|
|
|
* |
100
|
|
|
* @param PDO $external_pdo_connection PDO database object |
101
|
|
|
* @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
102
|
|
|
* @param string $table_prefix Table prefix |
103
|
|
|
* |
104
|
|
|
* @return PDO PDO database object |
105
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
106
|
|
|
*/ |
107
|
|
|
public static function externalInitialize($external_pdo_connection, Telegram $telegram, $table_prefix = null) |
108
|
|
|
{ |
109
|
|
|
if (empty($external_pdo_connection)) { |
110
|
|
|
throw new TelegramException('MySQL external connection not provided!'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
self::$pdo = $external_pdo_connection; |
114
|
|
|
self::$telegram = $telegram; |
115
|
|
|
self::$mysql_credentials = null; |
|
|
|
|
116
|
|
|
self::$table_prefix = $table_prefix; |
117
|
|
|
|
118
|
|
|
self::defineTable(); |
119
|
|
|
|
120
|
|
|
return self::$pdo; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Define all the table with the proper prefix |
125
|
|
|
*/ |
126
|
9 |
|
protected static function defineTable() |
127
|
|
|
{ |
128
|
9 |
|
if (!defined('TB_TELEGRAM_UPDATE')) { |
129
|
1 |
|
define('TB_TELEGRAM_UPDATE', self::$table_prefix . 'telegram_update'); |
130
|
|
|
} |
131
|
9 |
|
if (!defined('TB_MESSAGE')) { |
132
|
1 |
|
define('TB_MESSAGE', self::$table_prefix . 'message'); |
133
|
|
|
} |
134
|
9 |
|
if (!defined('TB_EDITED_MESSAGE')) { |
135
|
1 |
|
define('TB_EDITED_MESSAGE', self::$table_prefix . 'edited_message'); |
136
|
|
|
} |
137
|
9 |
|
if (!defined('TB_INLINE_QUERY')) { |
138
|
1 |
|
define('TB_INLINE_QUERY', self::$table_prefix . 'inline_query'); |
139
|
|
|
} |
140
|
9 |
|
if (!defined('TB_CHOSEN_INLINE_RESULT')) { |
141
|
1 |
|
define('TB_CHOSEN_INLINE_RESULT', self::$table_prefix . 'chosen_inline_result'); |
142
|
|
|
} |
143
|
9 |
|
if (!defined('TB_CALLBACK_QUERY')) { |
144
|
1 |
|
define('TB_CALLBACK_QUERY', self::$table_prefix . 'callback_query'); |
145
|
|
|
} |
146
|
9 |
|
if (!defined('TB_USER')) { |
147
|
1 |
|
define('TB_USER', self::$table_prefix . 'user'); |
148
|
|
|
} |
149
|
9 |
|
if (!defined('TB_CHAT')) { |
150
|
1 |
|
define('TB_CHAT', self::$table_prefix . 'chat'); |
151
|
|
|
} |
152
|
9 |
|
if (!defined('TB_USER_CHAT')) { |
153
|
1 |
|
define('TB_USER_CHAT', self::$table_prefix . 'user_chat'); |
154
|
|
|
} |
155
|
9 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Check if database connection has been created |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
9 |
|
public static function isDbConnected() |
163
|
|
|
{ |
164
|
9 |
|
if (empty(self::$pdo)) { |
165
|
|
|
return false; |
166
|
|
|
} else { |
167
|
9 |
|
return true; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Fetch update(s) from DB |
173
|
|
|
* |
174
|
|
|
* @param int $limit Limit the number of updates to fetch |
175
|
|
|
* |
176
|
|
|
* @return array|bool Fetched data or false if not connected |
177
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
178
|
|
|
*/ |
179
|
|
|
public static function selectTelegramUpdate($limit = null) |
180
|
|
|
{ |
181
|
|
|
if (!self::isDbConnected()) { |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
try { |
186
|
|
|
$query = 'SELECT `id` FROM `' . TB_TELEGRAM_UPDATE . '` '; |
187
|
|
|
$query .= 'ORDER BY `id` DESC'; |
188
|
|
|
|
189
|
|
|
if (!is_null($limit)) { |
190
|
|
|
$query .= ' LIMIT :limit '; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$sth_select_telegram_update = self::$pdo->prepare($query); |
194
|
|
|
$sth_select_telegram_update->bindParam(':limit', $limit, PDO::PARAM_INT); |
195
|
|
|
$sth_select_telegram_update->execute(); |
196
|
|
|
$results = $sth_select_telegram_update->fetchAll(PDO::FETCH_ASSOC); |
197
|
|
|
} catch (PDOException $e) { |
198
|
|
|
throw new TelegramException($e->getMessage()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $results; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Fetch message(s) from DB |
206
|
|
|
* |
207
|
|
|
* @param int $limit Limit the number of messages to fetch |
208
|
|
|
* |
209
|
|
|
* @return array|bool Fetched data or false if not connected |
210
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
211
|
|
|
*/ |
212
|
|
|
public static function selectMessages($limit = null) |
213
|
|
|
{ |
214
|
|
|
if (!self::isDbConnected()) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
try { |
219
|
|
|
//message table |
220
|
|
|
$query = 'SELECT * FROM `' . TB_MESSAGE . '` '; |
221
|
|
|
$query .= 'WHERE ' . TB_MESSAGE . '.`update_id` != 0 '; |
222
|
|
|
$query .= 'ORDER BY ' . TB_MESSAGE . '.`message_id` DESC'; |
223
|
|
|
|
224
|
|
|
if (!is_null($limit)) { |
225
|
|
|
$query .= ' LIMIT :limit '; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$sth = self::$pdo->prepare($query); |
229
|
|
|
$sth->bindParam(':limit', $limit, PDO::PARAM_INT); |
230
|
|
|
$sth->execute(); |
231
|
|
|
$results = $sth->fetchAll(PDO::FETCH_ASSOC); |
232
|
|
|
} catch (PDOException $e) { |
233
|
|
|
throw new TelegramException($e->getMessage()); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $results; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Convert from unix timestamp to timestamp |
241
|
|
|
* |
242
|
|
|
* @param int $time Unix timestamp |
243
|
|
|
* |
244
|
|
|
* @return null|string Timestamp if a time has been passed, else null |
245
|
|
|
*/ |
246
|
7 |
|
protected static function getTimestamp($time = null) |
247
|
|
|
{ |
248
|
7 |
|
if (is_null($time)) { |
249
|
6 |
|
return date('Y-m-d H:i:s', time()); |
250
|
|
|
} |
251
|
6 |
|
return date('Y-m-d H:i:s', $time); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Insert entry to telegram_update table |
256
|
|
|
* |
257
|
|
|
* @param int $id |
258
|
|
|
* @param int $chat_id |
259
|
|
|
* @param int $message_id |
260
|
|
|
* @param int $inline_query_id |
261
|
|
|
* @param int $chosen_inline_result_id |
262
|
|
|
* @param int $callback_query_id |
263
|
|
|
* @param int $edited_message_id |
264
|
|
|
* |
265
|
|
|
* @return bool If the insert was successful |
266
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
267
|
|
|
*/ |
268
|
|
|
public static function insertTelegramUpdate( |
269
|
|
|
$id, |
270
|
|
|
$chat_id, |
271
|
|
|
$message_id, |
272
|
|
|
$inline_query_id, |
273
|
|
|
$chosen_inline_result_id, |
274
|
|
|
$callback_query_id, |
275
|
|
|
$edited_message_id |
276
|
|
|
) { |
277
|
|
|
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_result_id) && is_null($callback_query_id) && is_null($edited_message_id)) { |
278
|
|
|
throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if (!self::isDbConnected()) { |
282
|
|
|
return false; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
try { |
286
|
|
|
$sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '` |
287
|
|
|
( |
288
|
|
|
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id` |
289
|
|
|
) |
290
|
|
|
VALUES ( |
291
|
|
|
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id |
292
|
|
|
) |
293
|
|
|
'); |
294
|
|
|
|
295
|
|
|
$sth_insert_telegram_update->bindParam(':id', $id, PDO::PARAM_INT); |
296
|
|
|
$sth_insert_telegram_update->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
297
|
|
|
$sth_insert_telegram_update->bindParam(':message_id', $message_id, PDO::PARAM_INT); |
298
|
|
|
$sth_insert_telegram_update->bindParam(':inline_query_id', $inline_query_id, PDO::PARAM_INT); |
299
|
|
|
$sth_insert_telegram_update->bindParam( |
300
|
|
|
':chosen_inline_result_id', |
301
|
|
|
$chosen_inline_result_id, |
302
|
|
|
PDO::PARAM_INT |
303
|
|
|
); |
304
|
|
|
$sth_insert_telegram_update->bindParam(':callback_query_id', $callback_query_id, PDO::PARAM_INT); |
305
|
|
|
$sth_insert_telegram_update->bindParam(':edited_message_id', $edited_message_id, PDO::PARAM_INT); |
306
|
|
|
|
307
|
|
|
return $sth_insert_telegram_update->execute(); |
308
|
|
|
} catch (PDOException $e) { |
309
|
|
|
throw new TelegramException($e->getMessage()); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Insert users and save their connection to chats |
315
|
|
|
* |
316
|
|
|
* @param \Longman\TelegramBot\Entities\User $user |
317
|
|
|
* @param string $date |
318
|
|
|
* @param \Longman\TelegramBot\Entities\Chat $chat |
319
|
|
|
* |
320
|
|
|
* @return bool If the insert was successful |
321
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
322
|
|
|
*/ |
323
|
6 |
|
public static function insertUser(User $user, $date, Chat $chat = null) |
324
|
|
|
{ |
325
|
6 |
|
if (!self::isDbConnected()) { |
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
6 |
|
$user_id = $user->getId(); |
330
|
6 |
|
$username = $user->getUsername(); |
331
|
6 |
|
$first_name = $user->getFirstName(); |
332
|
6 |
|
$last_name = $user->getLastName(); |
333
|
|
|
|
334
|
|
|
try { |
335
|
6 |
|
$sth1 = self::$pdo->prepare('INSERT INTO `' . TB_USER . '` |
336
|
|
|
( |
337
|
|
|
`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at` |
338
|
|
|
) |
339
|
|
|
VALUES ( |
340
|
|
|
:id, :username, :first_name, :last_name, :date, :date |
341
|
|
|
) |
342
|
|
|
ON DUPLICATE KEY UPDATE `username`=:username, `first_name`=:first_name, |
343
|
|
|
`last_name`=:last_name, `updated_at`=:date |
344
|
6 |
|
'); |
345
|
|
|
|
346
|
6 |
|
$sth1->bindParam(':id', $user_id, PDO::PARAM_INT); |
347
|
6 |
|
$sth1->bindParam(':username', $username, PDO::PARAM_STR, 255); |
348
|
6 |
|
$sth1->bindParam(':first_name', $first_name, PDO::PARAM_STR, 255); |
349
|
6 |
|
$sth1->bindParam(':last_name', $last_name, PDO::PARAM_STR, 255); |
350
|
6 |
|
$sth1->bindParam(':date', $date, PDO::PARAM_STR); |
351
|
|
|
|
352
|
6 |
|
$status = $sth1->execute(); |
353
|
|
|
} catch (PDOException $e) { |
354
|
|
|
throw new TelegramException($e->getMessage()); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
//insert also the relationship to the chat into user_chat table |
358
|
6 |
|
if (!is_null($chat)) { |
359
|
6 |
|
$chat_id = $chat->getId(); |
360
|
|
|
try { |
361
|
6 |
|
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_USER_CHAT . '` |
362
|
|
|
( |
363
|
|
|
`user_id`, `chat_id` |
364
|
|
|
) |
365
|
|
|
VALUES ( |
366
|
|
|
:user_id, :chat_id |
367
|
6 |
|
)'); |
368
|
|
|
|
369
|
6 |
|
$sth3->bindParam(':user_id', $user_id, PDO::PARAM_INT); |
370
|
6 |
|
$sth3->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
371
|
|
|
|
372
|
6 |
|
$status = $sth3->execute(); |
373
|
|
|
} catch (PDOException $e) { |
374
|
|
|
throw new TelegramException($e->getMessage()); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
6 |
|
return $status; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Insert chat |
383
|
|
|
* |
384
|
|
|
* @param \Longman\TelegramBot\Entities\Chat $chat |
385
|
|
|
* @param string $date |
386
|
|
|
* @param int $migrate_to_chat_id |
387
|
|
|
* |
388
|
|
|
* @return bool If the insert was successful |
389
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
390
|
|
|
*/ |
391
|
6 |
|
public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null) |
392
|
|
|
{ |
393
|
6 |
|
if (!self::isDbConnected()) { |
394
|
|
|
return false; |
395
|
|
|
} |
396
|
|
|
|
397
|
6 |
|
$chat_id = $chat->getId(); |
398
|
6 |
|
$chat_title = $chat->getTitle(); |
399
|
6 |
|
$type = $chat->getType(); |
|
|
|
|
400
|
|
|
|
401
|
|
|
try { |
402
|
6 |
|
$sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '` |
403
|
|
|
( |
404
|
|
|
`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id` |
405
|
|
|
) |
406
|
|
|
VALUES ( |
407
|
|
|
:id, :type, :title, :date, :date, :oldid |
408
|
|
|
) |
409
|
|
|
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date |
410
|
6 |
|
'); |
411
|
|
|
|
412
|
6 |
|
if ($migrate_to_chat_id) { |
|
|
|
|
413
|
|
|
$type = 'supergroup'; |
414
|
|
|
|
415
|
|
|
$sth2->bindParam(':id', $migrate_to_chat_id, PDO::PARAM_INT); |
416
|
|
|
$sth2->bindParam(':oldid', $chat_id, PDO::PARAM_INT); |
417
|
|
|
} else { |
418
|
6 |
|
$sth2->bindParam(':id', $chat_id, PDO::PARAM_INT); |
419
|
6 |
|
$sth2->bindParam(':oldid', $migrate_to_chat_id, PDO::PARAM_INT); |
420
|
|
|
} |
421
|
|
|
|
422
|
6 |
|
$sth2->bindParam(':type', $type, PDO::PARAM_INT); |
423
|
6 |
|
$sth2->bindParam(':title', $chat_title, PDO::PARAM_STR, 255); |
424
|
6 |
|
$sth2->bindParam(':date', $date, PDO::PARAM_STR); |
425
|
|
|
|
426
|
6 |
|
return $sth2->execute(); |
427
|
|
|
} catch (PDOException $e) { |
428
|
|
|
throw new TelegramException($e->getMessage()); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Insert request into database |
434
|
|
|
* |
435
|
|
|
* @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails? |
436
|
|
|
* |
437
|
|
|
* @param \Longman\TelegramBot\Entities\Update $update |
438
|
|
|
* |
439
|
|
|
* @return bool |
440
|
|
|
*/ |
441
|
|
|
public static function insertRequest(Update $update) |
442
|
|
|
{ |
443
|
|
|
$update_id = $update->getUpdateId(); |
444
|
|
|
if ($update->getUpdateType() == 'message') { |
445
|
|
|
$message = $update->getMessage(); |
446
|
|
|
|
447
|
|
|
if (self::insertMessageRequest($message)) { |
448
|
|
|
$message_id = $message->getMessageId(); |
449
|
|
|
$chat_id = $message->getChat()->getId(); |
450
|
|
|
return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null, null); |
451
|
|
|
} |
452
|
|
View Code Duplication |
} elseif ($update->getUpdateType() == 'inline_query') { |
|
|
|
|
453
|
|
|
$inline_query = $update->getInlineQuery(); |
454
|
|
|
|
455
|
|
|
if (self::insertInlineQueryRequest($inline_query)) { |
456
|
|
|
$inline_query_id = $inline_query->getId(); |
457
|
|
|
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null, null); |
458
|
|
|
} |
459
|
|
|
} elseif ($update->getUpdateType() == 'chosen_inline_result') { |
460
|
|
|
$chosen_inline_result = $update->getChosenInlineResult(); |
461
|
|
|
|
462
|
|
|
if (self::insertChosenInlineResultRequest($chosen_inline_result)) { |
463
|
|
|
$chosen_inline_result_local_id = self::$pdo->lastInsertId(); |
464
|
|
|
return self::insertTelegramUpdate( |
465
|
|
|
$update_id, |
466
|
|
|
null, |
467
|
|
|
null, |
468
|
|
|
null, |
469
|
|
|
$chosen_inline_result_local_id, |
470
|
|
|
null, |
471
|
|
|
null |
472
|
|
|
); |
473
|
|
|
} |
474
|
|
View Code Duplication |
} elseif ($update->getUpdateType() == 'callback_query') { |
|
|
|
|
475
|
|
|
$callback_query = $update->getCallbackQuery(); |
476
|
|
|
|
477
|
|
|
if (self::insertCallbackQueryRequest($callback_query)) { |
478
|
|
|
$callback_query_id = $callback_query->getId(); |
479
|
|
|
return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id, null); |
480
|
|
|
} |
481
|
|
|
} elseif ($update->getUpdateType() == 'edited_message') { |
482
|
|
|
$edited_message = $update->getEditedMessage(); |
483
|
|
|
|
484
|
|
|
if (self::insertEditedMessageRequest($edited_message)) { |
485
|
|
|
$chat_id = $edited_message->getChat()->getId(); |
486
|
|
|
$edited_message_local_id = self::$pdo->lastInsertId(); |
487
|
|
|
return self::insertTelegramUpdate( |
488
|
|
|
$update_id, |
489
|
|
|
$chat_id, |
490
|
|
|
null, |
491
|
|
|
null, |
492
|
|
|
null, |
493
|
|
|
null, |
494
|
|
|
$edited_message_local_id |
495
|
|
|
); |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
return false; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Insert inline query request into database |
504
|
|
|
* |
505
|
|
|
* @param \Longman\TelegramBot\Entities\InlineQuery $inline_query |
506
|
|
|
* |
507
|
|
|
* @return bool If the insert was successful |
508
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
509
|
|
|
*/ |
510
|
|
View Code Duplication |
public static function insertInlineQueryRequest(InlineQuery $inline_query) |
|
|
|
|
511
|
|
|
{ |
512
|
|
|
if (!self::isDbConnected()) { |
513
|
|
|
return false; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
try { |
517
|
|
|
$mysql_query = 'INSERT IGNORE INTO `' . TB_INLINE_QUERY . '` |
518
|
|
|
( |
519
|
|
|
`id`, `user_id`, `location`, `query`, `offset`, `created_at` |
520
|
|
|
) |
521
|
|
|
VALUES ( |
522
|
|
|
:inline_query_id, :user_id, :location, :query, :param_offset, :created_at |
523
|
|
|
)'; |
524
|
|
|
|
525
|
|
|
$sth_insert_inline_query = self::$pdo->prepare($mysql_query); |
526
|
|
|
|
527
|
|
|
$date = self::getTimestamp(time()); |
528
|
|
|
$inline_query_id = $inline_query->getId(); |
529
|
|
|
$from = $inline_query->getFrom(); |
530
|
|
|
$user_id = null; |
531
|
|
|
if (is_object($from)) { |
532
|
|
|
$user_id = $from->getId(); |
533
|
|
|
self::insertUser($from, $date); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
$location = $inline_query->getLocation(); |
537
|
|
|
$query = $inline_query->getQuery(); |
538
|
|
|
$offset = $inline_query->getOffset(); |
539
|
|
|
|
540
|
|
|
$sth_insert_inline_query->bindParam(':inline_query_id', $inline_query_id, PDO::PARAM_INT); |
541
|
|
|
$sth_insert_inline_query->bindParam(':user_id', $user_id, PDO::PARAM_INT); |
542
|
|
|
$sth_insert_inline_query->bindParam(':location', $location, PDO::PARAM_STR); |
543
|
|
|
$sth_insert_inline_query->bindParam(':query', $query, PDO::PARAM_STR); |
544
|
|
|
$sth_insert_inline_query->bindParam(':param_offset', $offset, PDO::PARAM_STR); |
545
|
|
|
$sth_insert_inline_query->bindParam(':created_at', $date, PDO::PARAM_STR); |
546
|
|
|
|
547
|
|
|
return $sth_insert_inline_query->execute(); |
548
|
|
|
} catch (PDOException $e) { |
549
|
|
|
throw new TelegramException($e->getMessage()); |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Insert chosen inline result request into database |
555
|
|
|
* |
556
|
|
|
* @param \Longman\TelegramBot\Entities\ChosenInlineResult $chosen_inline_result |
557
|
|
|
* |
558
|
|
|
* @return bool If the insert was successful |
559
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
560
|
|
|
*/ |
561
|
|
View Code Duplication |
public static function insertChosenInlineResultRequest(ChosenInlineResult $chosen_inline_result) |
|
|
|
|
562
|
|
|
{ |
563
|
|
|
if (!self::isDbConnected()) { |
564
|
|
|
return false; |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
try { |
568
|
|
|
$mysql_query = 'INSERT INTO `' . TB_CHOSEN_INLINE_RESULT . '` |
569
|
|
|
( |
570
|
|
|
`result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at` |
571
|
|
|
) |
572
|
|
|
VALUES ( |
573
|
|
|
:result_id, :user_id, :location, :inline_message_id, :query, :created_at |
574
|
|
|
)'; |
575
|
|
|
|
576
|
|
|
$sth_insert_chosen_inline_result = self::$pdo->prepare($mysql_query); |
577
|
|
|
|
578
|
|
|
$date = self::getTimestamp(time()); |
579
|
|
|
$result_id = $chosen_inline_result->getResultId(); |
580
|
|
|
$from = $chosen_inline_result->getFrom(); |
581
|
|
|
$user_id = null; |
582
|
|
|
if (is_object($from)) { |
583
|
|
|
$user_id = $from->getId(); |
584
|
|
|
self::insertUser($from, $date); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
$location = $chosen_inline_result->getLocation(); |
588
|
|
|
$inline_message_id = $chosen_inline_result->getInlineMessageId(); |
589
|
|
|
$query = $chosen_inline_result->getQuery(); |
590
|
|
|
|
591
|
|
|
$sth_insert_chosen_inline_result->bindParam(':result_id', $result_id, PDO::PARAM_STR); |
592
|
|
|
$sth_insert_chosen_inline_result->bindParam(':user_id', $user_id, PDO::PARAM_INT); |
593
|
|
|
$sth_insert_chosen_inline_result->bindParam(':location', $location, PDO::PARAM_INT); |
594
|
|
|
$sth_insert_chosen_inline_result->bindParam(':inline_message_id', $inline_message_id, PDO::PARAM_STR); |
595
|
|
|
$sth_insert_chosen_inline_result->bindParam(':query', $query, PDO::PARAM_STR); |
596
|
|
|
$sth_insert_chosen_inline_result->bindParam(':created_at', $date, PDO::PARAM_STR); |
597
|
|
|
|
598
|
|
|
return $sth_insert_chosen_inline_result->execute(); |
599
|
|
|
} catch (PDOException $e) { |
600
|
|
|
throw new TelegramException($e->getMessage()); |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Insert callback query request into database |
606
|
|
|
* |
607
|
|
|
* @param \Longman\TelegramBot\Entities\CallbackQuery $callback_query |
608
|
|
|
* |
609
|
|
|
* @return bool If the insert was successful |
610
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
611
|
|
|
*/ |
612
|
|
|
public static function insertCallbackQueryRequest(CallbackQuery $callback_query) |
613
|
|
|
{ |
614
|
|
|
if (!self::isDbConnected()) { |
615
|
|
|
return false; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
try { |
619
|
|
|
$mysql_query = 'INSERT IGNORE INTO `' . TB_CALLBACK_QUERY . '` |
620
|
|
|
( |
621
|
|
|
`id`, `user_id`, `chat_id`, `message_id`, `inline_message_id`, `data`, `created_at` |
622
|
|
|
) |
623
|
|
|
VALUES ( |
624
|
|
|
:callback_query_id, :user_id, :chat_id, :message_id, :inline_message_id, :data, :created_at |
625
|
|
|
)'; |
626
|
|
|
|
627
|
|
|
$sth_insert_callback_query = self::$pdo->prepare($mysql_query); |
628
|
|
|
|
629
|
|
|
$date = self::getTimestamp(time()); |
630
|
|
|
|
631
|
|
|
$callback_query_id = $callback_query->getId(); |
632
|
|
|
$from = $callback_query->getFrom(); |
633
|
|
|
$user_id = null; |
634
|
|
|
if (is_object($from)) { |
635
|
|
|
$user_id = $from->getId(); |
636
|
|
|
self::insertUser($from, $date); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
$message = $callback_query->getMessage(); |
640
|
|
|
$chat_id = null; |
641
|
|
|
$message_id = null; |
642
|
|
|
if ($message) { |
643
|
|
|
$chat_id = $message->getChat()->getId(); |
644
|
|
|
$message_id = $message->getMessageId(); |
645
|
|
|
|
646
|
|
|
$sth = self::$pdo->prepare('SELECT * FROM `' . TB_MESSAGE . '` |
647
|
|
|
WHERE `id` = ' . $message_id . ' AND `chat_id` = ' . $chat_id . ' LIMIT 1'); |
648
|
|
|
$sth->execute(); |
649
|
|
|
|
650
|
|
|
if ($sth->rowCount() > 0) { |
651
|
|
|
self::insertEditedMessageRequest($message); |
652
|
|
|
} else { |
653
|
|
|
self::insertMessageRequest($message); |
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
$inline_message_id = $callback_query->getInlineMessageId(); |
658
|
|
|
$data = $callback_query->getData(); |
659
|
|
|
|
660
|
|
|
$sth_insert_callback_query->bindParam(':callback_query_id', $callback_query_id, PDO::PARAM_INT); |
661
|
|
|
$sth_insert_callback_query->bindParam(':user_id', $user_id, PDO::PARAM_INT); |
662
|
|
|
$sth_insert_callback_query->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
663
|
|
|
$sth_insert_callback_query->bindParam(':message_id', $message_id, PDO::PARAM_INT); |
664
|
|
|
$sth_insert_callback_query->bindParam(':inline_message_id', $inline_message_id, PDO::PARAM_STR); |
665
|
|
|
$sth_insert_callback_query->bindParam(':data', $data, PDO::PARAM_STR); |
666
|
|
|
$sth_insert_callback_query->bindParam(':created_at', $date, PDO::PARAM_STR); |
667
|
|
|
|
668
|
|
|
return $sth_insert_callback_query->execute(); |
669
|
|
|
} catch (PDOException $e) { |
670
|
|
|
throw new TelegramException($e->getMessage()); |
671
|
|
|
} |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* Insert Message request in db |
676
|
|
|
* |
677
|
|
|
* @param \Longman\TelegramBot\Entities\Message $message |
678
|
|
|
* |
679
|
|
|
* @return bool If the insert was successful |
680
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
681
|
|
|
*/ |
682
|
6 |
|
public static function insertMessageRequest(Message $message) |
683
|
|
|
{ |
684
|
6 |
|
if (!self::isDbConnected()) { |
685
|
|
|
return false; |
686
|
|
|
} |
687
|
|
|
|
688
|
6 |
|
$from = $message->getFrom(); |
689
|
6 |
|
$chat = $message->getChat(); |
690
|
|
|
|
691
|
6 |
|
$chat_id = $chat->getId(); |
692
|
|
|
|
693
|
6 |
|
$date = self::getTimestamp($message->getDate()); |
694
|
|
|
|
695
|
6 |
|
$forward_from = $message->getForwardFrom(); |
696
|
6 |
|
$forward_from_chat = $message->getForwardFromChat(); |
697
|
6 |
|
$photo = $message->getPhoto(); |
698
|
6 |
|
$entities = $message->getEntities(); |
699
|
6 |
|
$new_chat_member = $message->getNewChatMember(); |
700
|
6 |
|
$new_chat_photo = $message->getNewChatPhoto(); |
701
|
6 |
|
$left_chat_member = $message->getLeftChatMember(); |
702
|
6 |
|
$migrate_to_chat_id = $message->getMigrateToChatId(); |
703
|
|
|
|
704
|
|
|
//Insert chat, update chat id in case it migrated |
705
|
6 |
|
self::insertChat($chat, $date, $migrate_to_chat_id); |
|
|
|
|
706
|
|
|
|
707
|
|
|
//Insert user and the relation with the chat |
708
|
6 |
|
self::insertUser($from, $date, $chat); |
|
|
|
|
709
|
|
|
|
710
|
|
|
//Insert the forwarded message user in users table |
711
|
6 |
|
if (is_object($forward_from)) { |
712
|
|
|
$forward_date = self::getTimestamp($message->getForwardDate()); |
713
|
|
|
self::insertUser($forward_from, $forward_date); |
714
|
|
|
$forward_from = $forward_from->getId(); |
715
|
|
|
} |
716
|
|
|
|
717
|
6 |
|
if (is_object($forward_from_chat)) { |
718
|
|
|
$forward_date = self::getTimestamp($message->getForwardDate()); |
719
|
|
|
self::insertChat($forward_from_chat, $forward_date); |
720
|
|
|
$forward_from_chat = $forward_from_chat->getId(); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
//New and left chat member |
724
|
6 |
|
if ($new_chat_member) { |
725
|
|
|
//Insert the new chat user |
726
|
|
|
self::insertUser($new_chat_member, $date, $chat); |
727
|
|
|
$new_chat_member = $new_chat_member->getId(); |
728
|
6 |
|
} elseif ($left_chat_member) { |
729
|
|
|
//Insert the left chat user |
730
|
|
|
self::insertUser($left_chat_member, $date, $chat); |
731
|
|
|
$left_chat_member = $left_chat_member->getId(); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
try { |
735
|
6 |
|
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '` |
736
|
|
|
( |
737
|
|
|
`id`, `user_id`, `chat_id`, `date`, `forward_from`, `forward_from_chat`, |
738
|
|
|
`forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`, |
739
|
|
|
`photo`, `sticker`, `video`, `voice`, `caption`, `contact`, |
740
|
|
|
`location`, `venue`, `new_chat_member`, `left_chat_member`, |
741
|
|
|
`new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`, |
742
|
|
|
`supergroup_chat_created`, `channel_chat_created`, |
743
|
|
|
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message` |
744
|
|
|
) |
745
|
|
|
VALUES ( |
746
|
|
|
:message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat, |
747
|
|
|
:forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document, |
748
|
|
|
:photo, :sticker, :video, :voice, :caption, :contact, |
749
|
|
|
:location, :venue, :new_chat_member, :left_chat_member, |
750
|
|
|
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created, |
751
|
|
|
:supergroup_chat_created, :channel_chat_created, |
752
|
|
|
:migrate_from_chat_id, :migrate_to_chat_id, :pinned_message |
753
|
|
|
) |
754
|
6 |
|
'); |
755
|
|
|
|
756
|
6 |
|
$message_id = $message->getMessageId(); |
757
|
6 |
|
$from_id = $from->getId(); |
758
|
|
|
|
759
|
6 |
|
$reply_to_message = $message->getReplyToMessage(); |
760
|
6 |
|
$reply_to_message_id = null; |
761
|
6 |
|
if (is_object($reply_to_message)) { |
762
|
|
|
$reply_to_message_id = $reply_to_message->getMessageId(); |
763
|
|
|
// please notice that, as explained in the documentation, reply_to_message don't contain other |
764
|
|
|
// reply_to_message field so recursion deep is 1 |
765
|
|
|
self::insertMessageRequest($reply_to_message); |
766
|
|
|
} |
767
|
|
|
|
768
|
6 |
|
$text = $message->getText(); |
769
|
6 |
|
$audio = $message->getAudio(); |
770
|
6 |
|
$document = $message->getDocument(); |
771
|
6 |
|
$sticker = $message->getSticker(); |
772
|
6 |
|
$video = $message->getVideo(); |
773
|
6 |
|
$voice = $message->getVoice(); |
774
|
6 |
|
$caption = $message->getCaption(); |
775
|
6 |
|
$contact = $message->getContact(); |
776
|
6 |
|
$location = $message->getLocation(); |
777
|
6 |
|
$venue = $message->getVenue(); |
778
|
6 |
|
$new_chat_title = $message->getNewChatTitle(); |
779
|
6 |
|
$delete_chat_photo = $message->getDeleteChatPhoto(); |
780
|
6 |
|
$group_chat_created = $message->getGroupChatCreated(); |
781
|
6 |
|
$supergroup_chat_created = $message->getSupergroupChatCreated(); |
782
|
6 |
|
$channel_chat_created = $message->getChannelChatCreated(); |
783
|
6 |
|
$migrate_from_chat_id = $message->getMigrateFromChatId(); |
784
|
6 |
|
$migrate_to_chat_id = $message->getMigrateToChatId(); |
785
|
6 |
|
$pinned_message = $message->getPinnedMessage(); |
786
|
|
|
|
787
|
6 |
|
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
788
|
6 |
|
$sth->bindParam(':message_id', $message_id, PDO::PARAM_INT); |
789
|
6 |
|
$sth->bindParam(':user_id', $from_id, PDO::PARAM_INT); |
790
|
6 |
|
$sth->bindParam(':date', $date, PDO::PARAM_STR); |
791
|
6 |
|
$sth->bindParam(':forward_from', $forward_from, PDO::PARAM_INT); |
792
|
6 |
|
$sth->bindParam(':forward_from_chat', $forward_from_chat, PDO::PARAM_INT); |
793
|
6 |
|
$sth->bindParam(':forward_date', $forward_date, PDO::PARAM_STR); |
794
|
|
|
|
795
|
6 |
|
$reply_chat_id = null; |
796
|
6 |
|
if ($reply_to_message_id) { |
797
|
|
|
$reply_chat_id = $chat_id; |
798
|
|
|
} |
799
|
|
|
|
800
|
6 |
|
$var = []; |
801
|
6 |
View Code Duplication |
if (is_array($entities)) { |
|
|
|
|
802
|
|
|
foreach ($entities as $elm) { |
803
|
|
|
$var[] = json_decode($elm, true); |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
$entities = json_encode($var); |
807
|
|
|
} else { |
808
|
6 |
|
$entities = null; |
809
|
|
|
} |
810
|
|
|
|
811
|
6 |
|
$sth->bindParam(':reply_to_chat', $reply_chat_id, PDO::PARAM_INT); |
812
|
6 |
|
$sth->bindParam(':reply_to_message', $reply_to_message_id, PDO::PARAM_INT); |
813
|
6 |
|
$sth->bindParam(':text', $text, PDO::PARAM_STR); |
814
|
6 |
|
$sth->bindParam(':entities', $entities, PDO::PARAM_STR); |
815
|
6 |
|
$sth->bindParam(':audio', $audio, PDO::PARAM_STR); |
816
|
6 |
|
$sth->bindParam(':document', $document, PDO::PARAM_STR); |
817
|
|
|
|
818
|
6 |
|
$var = []; |
819
|
6 |
View Code Duplication |
if (is_array($photo)) { |
|
|
|
|
820
|
|
|
foreach ($photo as $elm) { |
821
|
|
|
$var[] = json_decode($elm, true); |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
$photo = json_encode($var); |
825
|
|
|
} else { |
826
|
6 |
|
$photo = ''; |
827
|
|
|
} |
828
|
|
|
|
829
|
6 |
|
$sth->bindParam(':photo', $photo, PDO::PARAM_STR); |
830
|
6 |
|
$sth->bindParam(':sticker', $sticker, PDO::PARAM_STR); |
831
|
6 |
|
$sth->bindParam(':video', $video, PDO::PARAM_STR); |
832
|
6 |
|
$sth->bindParam(':voice', $voice, PDO::PARAM_STR); |
833
|
6 |
|
$sth->bindParam(':caption', $caption, PDO::PARAM_STR); |
834
|
6 |
|
$sth->bindParam(':contact', $contact, PDO::PARAM_STR); |
835
|
6 |
|
$sth->bindParam(':location', $location, PDO::PARAM_STR); |
836
|
6 |
|
$sth->bindParam(':venue', $venue, PDO::PARAM_STR); |
837
|
6 |
|
$sth->bindParam(':new_chat_member', $new_chat_member, PDO::PARAM_INT); |
838
|
6 |
|
$sth->bindParam(':left_chat_member', $left_chat_member, PDO::PARAM_INT); |
839
|
6 |
|
$sth->bindParam(':new_chat_title', $new_chat_title, PDO::PARAM_STR); |
840
|
|
|
|
841
|
|
|
//Array of PhotoSize |
842
|
6 |
|
$var = []; |
843
|
6 |
View Code Duplication |
if (is_array($new_chat_photo)) { |
|
|
|
|
844
|
|
|
foreach ($new_chat_photo as $elm) { |
845
|
|
|
$var[] = json_decode($elm, true); |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
$new_chat_photo = json_encode($var); |
849
|
|
|
} else { |
850
|
6 |
|
$new_chat_photo = ''; |
851
|
|
|
} |
852
|
|
|
|
853
|
6 |
|
$sth->bindParam(':new_chat_photo', $new_chat_photo, PDO::PARAM_STR); |
854
|
6 |
|
$sth->bindParam(':delete_chat_photo', $delete_chat_photo, PDO::PARAM_STR); |
855
|
6 |
|
$sth->bindParam(':group_chat_created', $group_chat_created, PDO::PARAM_STR); |
856
|
6 |
|
$sth->bindParam(':supergroup_chat_created', $supergroup_chat_created, PDO::PARAM_INT); |
857
|
6 |
|
$sth->bindParam(':channel_chat_created', $channel_chat_created, PDO::PARAM_INT); |
858
|
6 |
|
$sth->bindParam(':migrate_from_chat_id', $migrate_from_chat_id, PDO::PARAM_INT); |
859
|
6 |
|
$sth->bindParam(':migrate_to_chat_id', $migrate_to_chat_id, PDO::PARAM_INT); |
860
|
6 |
|
$sth->bindParam(':pinned_message', $pinned_message, PDO::PARAM_INT); |
861
|
|
|
|
862
|
6 |
|
return $sth->execute(); |
863
|
|
|
} catch (PDOException $e) { |
864
|
|
|
throw new TelegramException($e->getMessage()); |
865
|
|
|
} |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
/** |
869
|
|
|
* Insert Edited Message request in db |
870
|
|
|
* |
871
|
|
|
* @param \Longman\TelegramBot\Entities\Message $edited_message |
872
|
|
|
* |
873
|
|
|
* @return bool If the insert was successful |
874
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
875
|
|
|
*/ |
876
|
|
|
public static function insertEditedMessageRequest(Message $edited_message) |
877
|
|
|
{ |
878
|
|
|
if (!self::isDbConnected()) { |
879
|
|
|
return false; |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
$from = $edited_message->getFrom(); |
883
|
|
|
$chat = $edited_message->getChat(); |
884
|
|
|
|
885
|
|
|
$chat_id = $chat->getId(); |
886
|
|
|
|
887
|
|
|
$edit_date = self::getTimestamp($edited_message->getEditDate()); |
888
|
|
|
|
889
|
|
|
$entities = $edited_message->getEntities(); |
890
|
|
|
|
891
|
|
|
//Insert chat |
892
|
|
|
self::insertChat($chat, $edit_date); |
|
|
|
|
893
|
|
|
|
894
|
|
|
//Insert user and the relation with the chat |
895
|
|
|
self::insertUser($from, $edit_date, $chat); |
|
|
|
|
896
|
|
|
|
897
|
|
|
try { |
898
|
|
|
$sth = self::$pdo->prepare('INSERT INTO `' . TB_EDITED_MESSAGE . '` |
899
|
|
|
( |
900
|
|
|
`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption` |
901
|
|
|
) |
902
|
|
|
VALUES ( |
903
|
|
|
:chat_id, :message_id, :user_id, :date, :text, :entities, :caption |
904
|
|
|
)'); |
905
|
|
|
|
906
|
|
|
$message_id = $edited_message->getMessageId(); |
907
|
|
|
$from_id = $from->getId(); |
908
|
|
|
|
909
|
|
|
$text = $edited_message->getText(); |
910
|
|
|
$caption = $edited_message->getCaption(); |
911
|
|
|
|
912
|
|
|
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
913
|
|
|
$sth->bindParam(':message_id', $message_id, PDO::PARAM_INT); |
914
|
|
|
$sth->bindParam(':user_id', $from_id, PDO::PARAM_INT); |
915
|
|
|
$sth->bindParam(':date', $edit_date, PDO::PARAM_STR); |
916
|
|
|
|
917
|
|
|
$var = []; |
918
|
|
View Code Duplication |
if (is_array($entities)) { |
|
|
|
|
919
|
|
|
foreach ($entities as $elm) { |
920
|
|
|
$var[] = json_decode($elm, true); |
921
|
|
|
} |
922
|
|
|
|
923
|
|
|
$entities = json_encode($var); |
924
|
|
|
} else { |
925
|
|
|
$entities = null; |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
$sth->bindParam(':text', $text, PDO::PARAM_STR); |
929
|
|
|
$sth->bindParam(':entities', $entities, PDO::PARAM_STR); |
930
|
|
|
$sth->bindParam(':caption', $caption, PDO::PARAM_STR); |
931
|
|
|
|
932
|
|
|
return $sth->execute(); |
933
|
|
|
} catch (PDOException $e) { |
934
|
|
|
throw new TelegramException($e->getMessage()); |
935
|
|
|
} |
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
/** |
939
|
|
|
* Select Group and/or single Chats |
940
|
|
|
* |
941
|
|
|
* @param bool $select_groups |
942
|
|
|
* @param bool $select_super_groups |
943
|
|
|
* @param bool $select_users |
944
|
|
|
* @param string $date_from |
945
|
|
|
* @param string $date_to |
946
|
|
|
* @param int $chat_id |
947
|
|
|
* @param string $text |
948
|
|
|
* |
949
|
|
|
* @return array|bool (Selected chats or false if invalid arguments) |
950
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
951
|
|
|
*/ |
952
|
|
|
public static function selectChats( |
953
|
|
|
$select_groups = true, |
954
|
|
|
$select_super_groups = true, |
955
|
|
|
$select_users = true, |
956
|
|
|
$date_from = null, |
957
|
|
|
$date_to = null, |
958
|
|
|
$chat_id = null, |
959
|
|
|
$text = null |
960
|
|
|
) { |
961
|
|
|
if (!self::isDbConnected()) { |
962
|
|
|
return false; |
963
|
|
|
} |
964
|
|
|
|
965
|
|
|
if (!$select_groups && !$select_users && !$select_super_groups) { |
966
|
|
|
return false; |
967
|
|
|
} |
968
|
|
|
|
969
|
|
|
try { |
970
|
|
|
$query = 'SELECT * , |
971
|
|
|
' . TB_CHAT . '.`id` AS `chat_id`, |
972
|
|
|
' . TB_CHAT . '.`created_at` AS `chat_created_at`, |
973
|
|
|
' . TB_CHAT . '.`updated_at` AS `chat_updated_at` |
974
|
|
|
' . |
975
|
|
|
(($select_users) ? ', ' . TB_USER . '.`id` AS `user_id` FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '` |
976
|
|
|
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`' : 'FROM `' . TB_CHAT . '`'); |
977
|
|
|
|
978
|
|
|
//Building parts of query |
979
|
|
|
$where = []; |
980
|
|
|
$tokens = []; |
981
|
|
|
|
982
|
|
|
if (!$select_groups || !$select_users || !$select_super_groups) { |
983
|
|
|
$chat_or_user = ''; |
984
|
|
|
|
985
|
|
|
if ($select_groups) { |
986
|
|
|
$chat_or_user .= TB_CHAT . '.`type` = "group"'; |
987
|
|
|
} |
988
|
|
|
|
989
|
|
|
if ($select_super_groups) { |
990
|
|
|
if (!empty($chat_or_user)) { |
991
|
|
|
$chat_or_user .= ' OR '; |
992
|
|
|
} |
993
|
|
|
|
994
|
|
|
$chat_or_user .= TB_CHAT . '.`type` = "supergroup"'; |
995
|
|
|
} |
996
|
|
|
|
997
|
|
|
if ($select_users) { |
998
|
|
|
if (!empty($chat_or_user)) { |
999
|
|
|
$chat_or_user .= ' OR '; |
1000
|
|
|
} |
1001
|
|
|
|
1002
|
|
|
$chat_or_user .= TB_CHAT . '.`type` = "private"'; |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
$where[] = '(' . $chat_or_user . ')'; |
1006
|
|
|
} |
1007
|
|
|
|
1008
|
|
|
if (!is_null($date_from)) { |
1009
|
|
|
$where[] = TB_CHAT . '.`updated_at` >= :date_from'; |
1010
|
|
|
$tokens[':date_from'] = $date_from; |
1011
|
|
|
} |
1012
|
|
|
|
1013
|
|
|
if (!is_null($date_to)) { |
1014
|
|
|
$where[] = TB_CHAT . '.`updated_at` <= :date_to'; |
1015
|
|
|
$tokens[':date_to'] = $date_to; |
1016
|
|
|
} |
1017
|
|
|
|
1018
|
|
|
if (!is_null($chat_id)) { |
1019
|
|
|
$where[] = TB_CHAT . '.`id` = :chat_id'; |
1020
|
|
|
$tokens[':chat_id'] = $chat_id; |
1021
|
|
|
} |
1022
|
|
|
|
1023
|
|
|
if (!is_null($text)) { |
1024
|
|
|
if ($select_users) { |
1025
|
|
|
$where[] = '(LOWER(' . TB_CHAT . '.`title`) LIKE :text OR LOWER(' . TB_USER . '.`first_name`) LIKE :text OR LOWER(' . TB_USER . '.`last_name`) LIKE :text OR LOWER(' . TB_USER . '.`username`) LIKE :text)'; |
1026
|
|
|
} else { |
1027
|
|
|
$where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text'; |
1028
|
|
|
} |
1029
|
|
|
|
1030
|
|
|
$tokens[':text'] = '%' . strtolower($text) . '%'; |
1031
|
|
|
} |
1032
|
|
|
|
1033
|
|
|
$a = 0; |
1034
|
|
|
foreach ($where as $part) { |
1035
|
|
|
if ($a) { |
1036
|
|
|
$query .= ' AND ' . $part; |
1037
|
|
|
} else { |
1038
|
|
|
$query .= ' WHERE ' . $part; |
1039
|
|
|
++$a; |
1040
|
|
|
} |
1041
|
|
|
} |
1042
|
|
|
|
1043
|
|
|
$query .= ' ORDER BY ' . TB_CHAT . '.`updated_at` ASC'; |
1044
|
|
|
|
1045
|
|
|
$sth = self::$pdo->prepare($query); |
1046
|
|
|
$sth->execute($tokens); |
1047
|
|
|
|
1048
|
|
|
$result = $sth->fetchAll(PDO::FETCH_ASSOC); |
1049
|
|
|
} catch (PDOException $e) { |
1050
|
|
|
throw new TelegramException($e->getMessage()); |
1051
|
|
|
} |
1052
|
|
|
|
1053
|
|
|
return $result; |
1054
|
|
|
} |
1055
|
|
|
} |
1056
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..