@@ -134,6 +134,9 @@ protected static function defineTable()
134
134
if (!defined ('TB_MESSAGE ' )) {
135
135
define ('TB_MESSAGE ' , self ::$ table_prefix .'message ' );
136
136
}
137
+ if (!defined ('TB_EDITED_MESSAGE ' )) {
138
+ define ('TB_EDITED_MESSAGE ' , self ::$ table_prefix .'edited_message ' );
139
+ }
137
140
if (!defined ('TB_INLINE_QUERY ' )) {
138
141
define ('TB_INLINE_QUERY ' , self ::$ table_prefix .'inline_query ' );
139
142
}
@@ -260,13 +263,14 @@ protected static function getTimestamp($time = null)
260
263
* @param int $inline_query_id
261
264
* @param int $chosen_inline_result_id
262
265
* @param int $callback_query_id
266
+ * @param int $edited_message_id
263
267
*
264
268
* @return bool|null
265
269
*/
266
- public static function insertTelegramUpdate ($ id , $ chat_id , $ message_id , $ inline_query_id , $ chosen_inline_result_id , $ callback_query_id )
270
+ public static function insertTelegramUpdate ($ id , $ chat_id , $ message_id , $ inline_query_id , $ chosen_inline_result_id , $ callback_query_id, $ edited_message_id )
267
271
{
268
- if (is_null ($ message_id ) && is_null ($ inline_query_id ) && is_null ($ chosen_inline_result_id ) && is_null ($ callback_query_id )) {
269
- throw new TelegramException ('Error both query_id and message_id are null ' );
272
+ 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 ) ) {
273
+ throw new TelegramException ('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null ' );
270
274
}
271
275
272
276
if (!self ::isDbConnected ()) {
@@ -277,10 +281,10 @@ public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_
277
281
//telegram_update table
278
282
$ sth_insert_telegram_update = self ::$ pdo ->prepare ('INSERT IGNORE INTO ` ' . TB_TELEGRAM_UPDATE . '`
279
283
(
280
- `id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`
284
+ `id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`
281
285
)
282
286
VALUES (
283
- :id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id
287
+ :id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id
284
288
)
285
289
' );
286
290
@@ -290,6 +294,7 @@ public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_
290
294
$ sth_insert_telegram_update ->bindParam (':inline_query_id ' , $ inline_query_id , \PDO ::PARAM_INT );
291
295
$ sth_insert_telegram_update ->bindParam (':chosen_inline_result_id ' , $ chosen_inline_result_id , \PDO ::PARAM_INT );
292
296
$ sth_insert_telegram_update ->bindParam (':callback_query_id ' , $ callback_query_id , \PDO ::PARAM_INT );
297
+ $ sth_insert_telegram_update ->bindParam (':edited_message_id ' , $ edited_message_id , \PDO ::PARAM_INT );
293
298
294
299
$ status = $ sth_insert_telegram_update ->execute ();
295
300
} catch (PDOException $ e ) {
@@ -419,6 +424,8 @@ public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null)
419
424
/**
420
425
* Insert request into database
421
426
*
427
+ * @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails?
428
+ *
422
429
* @param Entities\Update &$update
423
430
*
424
431
* @return bool
@@ -431,22 +438,28 @@ public static function insertRequest(Update &$update)
431
438
$ message_id = $ message ->getMessageId ();
432
439
$ chat_id = $ message ->getChat ()->getId ();
433
440
self ::insertMessageRequest ($ message );
434
- return self ::insertTelegramUpdate ($ update_id , $ chat_id , $ message_id , null , null , null );
441
+ return self ::insertTelegramUpdate ($ update_id , $ chat_id , $ message_id , null , null , null , null );
442
+ } elseif ($ update ->getUpdateType () == 'edited_message ' ) {
443
+ $ edited_message = $ update ->getEditedMessage ();
444
+ $ chat_id = $ edited_message ->getChat ()->getId ();
445
+ self ::insertEditedMessageRequest ($ edited_message );
446
+ $ edited_message_local_id = self ::$ pdo ->lastInsertId ();
447
+ return self ::insertTelegramUpdate ($ update_id , $ chat_id , null , null , null , null , $ edited_message_local_id );
435
448
} elseif ($ update ->getUpdateType () == 'inline_query ' ) {
436
449
$ inline_query = $ update ->getInlineQuery ();
437
450
$ inline_query_id = $ inline_query ->getId ();
438
451
self ::insertInlineQueryRequest ($ inline_query );
439
- return self ::insertTelegramUpdate ($ update_id , null , null , $ inline_query_id , null , null );
452
+ return self ::insertTelegramUpdate ($ update_id , null , null , $ inline_query_id , null , null , null );
440
453
} elseif ($ update ->getUpdateType () == 'chosen_inline_result ' ) {
441
454
$ chosen_inline_result = $ update ->getChosenInlineResult ();
442
455
self ::insertChosenInlineResultRequest ($ chosen_inline_result );
443
456
$ chosen_inline_result_local_id = self ::$ pdo ->lastInsertId ();
444
- return self ::insertTelegramUpdate ($ update_id , null , null , null , $ chosen_inline_result_local_id , null );
457
+ return self ::insertTelegramUpdate ($ update_id , null , null , null , $ chosen_inline_result_local_id , null , null );
445
458
} elseif ($ update ->getUpdateType () == 'callback_query ' ) {
446
459
$ callback_query = $ update ->getCallbackQuery ();
447
460
$ callback_query_id = $ callback_query ->getId ();
448
461
self ::insertCallbackQueryRequest ($ callback_query );
449
- return self ::insertTelegramUpdate ($ update_id , null , null , null , null , $ callback_query_id );
462
+ return self ::insertTelegramUpdate ($ update_id , null , null , null , null , $ callback_query_id, null );
450
463
}
451
464
}
452
465
@@ -552,7 +565,6 @@ public static function insertChosenInlineResultRequest(ChosenInlineResult &$chos
552
565
}
553
566
}
554
567
555
-
556
568
/**
557
569
* Insert callback query request into database
558
570
*
@@ -800,6 +812,72 @@ public static function insertMessageRequest(Message &$message)
800
812
return true ;
801
813
}
802
814
815
+ /**
816
+ * Insert Edited Message request in db
817
+ *
818
+ * @param Entities\Message &$edited_message
819
+ *
820
+ * @return bool If the insert was successful
821
+ */
822
+ public static function insertEditedMessageRequest (Message &$ edited_message )
823
+ {
824
+ if (!self ::isDbConnected ()) {
825
+ return false ;
826
+ }
827
+
828
+ $ from = $ edited_message ->getFrom ();
829
+ $ chat = $ edited_message ->getChat ();
830
+
831
+ $ chat_id = $ chat ->getId ();
832
+
833
+ $ edit_date = self ::getTimestamp ($ edited_message ->getEditDate ());
834
+
835
+ $ entities = $ edited_message ->getEntities ();
836
+
837
+ try {
838
+ //edited_message Table
839
+ $ sth = self ::$ pdo ->prepare ('INSERT IGNORE INTO ` ' . TB_EDITED_MESSAGE . '`
840
+ (
841
+ `chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`
842
+ )
843
+ VALUES (
844
+ :chat_id, :message_id, :user_id, :date, :text, :entities, :caption
845
+ ) ' );
846
+
847
+ $ message_id = $ edited_message ->getMessageId ();
848
+ $ from_id = $ from ->getId ();
849
+
850
+ $ text = $ edited_message ->getText ();
851
+ $ caption = $ edited_message ->getCaption ();
852
+
853
+ $ sth ->bindParam (':chat_id ' , $ chat_id , \PDO ::PARAM_INT );
854
+ $ sth ->bindParam (':message_id ' , $ message_id , \PDO ::PARAM_INT );
855
+ $ sth ->bindParam (':user_id ' , $ from_id , \PDO ::PARAM_INT );
856
+ $ sth ->bindParam (':date ' , $ edit_date , \PDO ::PARAM_STR );
857
+
858
+ $ var = [];
859
+ if (is_array ($ entities )) {
860
+ foreach ($ entities as $ elm ) {
861
+ $ var [] = json_decode ($ elm , true );
862
+ }
863
+
864
+ $ entities = json_encode ($ var );
865
+ } else {
866
+ $ entities = null ;
867
+ }
868
+
869
+ $ sth ->bindParam (':text ' , $ text , \PDO ::PARAM_STR );
870
+ $ sth ->bindParam (':entities ' , $ entities , \PDO ::PARAM_STR );
871
+ $ sth ->bindParam (':caption ' , $ caption , \PDO ::PARAM_STR );
872
+
873
+ $ status = $ sth ->execute ();
874
+ } catch (PDOException $ e ) {
875
+ throw new TelegramException ($ e ->getMessage ());
876
+ }
877
+
878
+ return true ;
879
+ }
880
+
803
881
/**
804
882
* Select Group and single Chats
805
883
*
0 commit comments