Skip to content

Commit 4c98cf8

Browse files
committed
Add new update fields, including their database tables.
1 parent dad24fc commit 4c98cf8

File tree

3 files changed

+268
-141
lines changed

3 files changed

+268
-141
lines changed

src/DB.php

Lines changed: 168 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Longman\TelegramBot\Entities\ChosenInlineResult;
1818
use Longman\TelegramBot\Entities\InlineQuery;
1919
use Longman\TelegramBot\Entities\Message;
20+
use Longman\TelegramBot\Entities\Payments\PreCheckoutQuery;
21+
use Longman\TelegramBot\Entities\Payments\ShippingQuery;
2022
use Longman\TelegramBot\Entities\ReplyToMessage;
2123
use Longman\TelegramBot\Entities\Update;
2224
use Longman\TelegramBot\Entities\User;
@@ -141,7 +143,9 @@ protected static function defineTables()
141143
'edited_message',
142144
'inline_query',
143145
'message',
146+
'pre_checkout_query',
144147
'request_limiter',
148+
'shipping_query',
145149
'telegram_update',
146150
'user',
147151
'user_chat',
@@ -300,30 +304,36 @@ public static function entitiesArrayToJson($entities, $default = null)
300304
/**
301305
* Insert entry to telegram_update table
302306
*
303-
* @todo Add missing values! See https://core.telegram.org/bots/api#update
304-
*
305-
* @param string $id
306-
* @param string $chat_id
307-
* @param string $message_id
308-
* @param string $inline_query_id
309-
* @param string $chosen_inline_result_id
310-
* @param string $callback_query_id
311-
* @param string $edited_message_id
307+
* @param string $update_id
308+
* @param string|null $chat_id
309+
* @param string|null $message_id
310+
* @param string|null $edited_message_id
311+
* @param string|null $channel_post_id
312+
* @param string|null $edited_channel_post_id
313+
* @param string|null $inline_query_id
314+
* @param string|null $chosen_inline_result_id
315+
* @param string|null $callback_query_id
316+
* @param string|null $shipping_query_id
317+
* @param string|null $pre_checkout_query_id
312318
*
313319
* @return bool If the insert was successful
314320
* @throws TelegramException
315321
*/
316-
public static function insertTelegramUpdate(
317-
$id,
322+
protected static function insertTelegramUpdate(
323+
$update_id,
318324
$chat_id = null,
319325
$message_id = null,
326+
$edited_message_id = null,
327+
$channel_post_id = null,
328+
$edited_channel_post_id = null,
320329
$inline_query_id = null,
321330
$chosen_inline_result_id = null,
322331
$callback_query_id = null,
323-
$edited_message_id = null
332+
$shipping_query_id = null,
333+
$pre_checkout_query_id = null
324334
) {
325-
if ($message_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $edited_message_id === null) {
326-
throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null');
335+
if ($message_id === null && $edited_message_id === null && $channel_post_id === null && $edited_channel_post_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $shipping_query_id === null && $pre_checkout_query_id === null) {
336+
throw new TelegramException('message_id, edited_message_id, channel_post_id, edited_channel_post_id, inline_query_id, chosen_inline_result_id, callback_query_id, shipping_query_id, pre_checkout_query_id are all null');
327337
}
328338

329339
if (!self::isDbConnected()) {
@@ -333,18 +343,22 @@ public static function insertTelegramUpdate(
333343
try {
334344
$sth = self::$pdo->prepare('
335345
INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
336-
(`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`)
346+
(`id`, `chat_id`, `message_id`, `edited_message_id`, `channel_post_id`, `edited_channel_post_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `shipping_query_id`, `pre_checkout_query_id`)
337347
VALUES
338-
(:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id)
348+
(:id, :chat_id, :message_id, :edited_message_id, :channel_post_id, :edited_channel_post_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :shipping_query_id, :pre_checkout_query_id)
339349
');
340350

341-
$sth->bindValue(':id', $id);
351+
$sth->bindValue(':id', $update_id);
342352
$sth->bindValue(':chat_id', $chat_id);
343353
$sth->bindValue(':message_id', $message_id);
344354
$sth->bindValue(':edited_message_id', $edited_message_id);
355+
$sth->bindValue(':channel_post_id', $channel_post_id);
356+
$sth->bindValue(':edited_channel_post_id', $edited_channel_post_id);
345357
$sth->bindValue(':inline_query_id', $inline_query_id);
346358
$sth->bindValue(':chosen_inline_result_id', $chosen_inline_result_id);
347359
$sth->bindValue(':callback_query_id', $callback_query_id);
360+
$sth->bindValue(':shipping_query_id', $shipping_query_id);
361+
$sth->bindValue(':pre_checkout_query_id', $pre_checkout_query_id);
348362

349363
return $sth->execute();
350364
} catch (PDOException $e) {
@@ -497,115 +511,56 @@ public static function insertRequest(Update $update)
497511
return false;
498512
}
499513

500-
$update_id = $update->getUpdateId();
501-
$update_type = $update->getUpdateType();
502-
503-
// @todo Make this simpler: if ($message = $update->getMessage()) ...
504-
if ($update_type === 'message') {
505-
$message = $update->getMessage();
506-
507-
if (self::insertMessageRequest($message)) {
508-
$message_id = $message->getMessageId();
509-
$chat_id = $message->getChat()->getId();
510-
511-
return self::insertTelegramUpdate(
512-
$update_id,
513-
$chat_id,
514-
$message_id
515-
);
516-
}
517-
} elseif ($update_type === 'edited_message') {
518-
$edited_message = $update->getEditedMessage();
519-
520-
if (self::insertEditedMessageRequest($edited_message)) {
521-
$edited_message_local_id = self::$pdo->lastInsertId();
522-
$chat_id = $edited_message->getChat()->getId();
523-
524-
return self::insertTelegramUpdate(
525-
$update_id,
526-
$chat_id,
527-
null,
528-
null,
529-
null,
530-
null,
531-
$edited_message_local_id
532-
);
533-
}
534-
} elseif ($update_type === 'channel_post') {
535-
$channel_post = $update->getChannelPost();
536-
537-
if (self::insertMessageRequest($channel_post)) {
538-
$message_id = $channel_post->getMessageId();
539-
$chat_id = $channel_post->getChat()->getId();
540-
541-
return self::insertTelegramUpdate(
542-
$update_id,
543-
$chat_id,
544-
$message_id
545-
);
546-
}
547-
} elseif ($update_type === 'edited_channel_post') {
548-
$edited_channel_post = $update->getEditedChannelPost();
549-
550-
if (self::insertEditedMessageRequest($edited_channel_post)) {
551-
$edited_channel_post_local_id = self::$pdo->lastInsertId();
552-
$chat_id = $edited_channel_post->getChat()->getId();
553-
554-
return self::insertTelegramUpdate(
555-
$update_id,
556-
$chat_id,
557-
null,
558-
null,
559-
null,
560-
null,
561-
$edited_channel_post_local_id
562-
);
563-
}
564-
} elseif ($update_type === 'inline_query') {
565-
$inline_query = $update->getInlineQuery();
566-
567-
if (self::insertInlineQueryRequest($inline_query)) {
568-
$inline_query_id = $inline_query->getId();
569-
570-
return self::insertTelegramUpdate(
571-
$update_id,
572-
null,
573-
null,
574-
$inline_query_id
575-
);
576-
}
577-
} elseif ($update_type === 'chosen_inline_result') {
578-
$chosen_inline_result = $update->getChosenInlineResult();
579-
580-
if (self::insertChosenInlineResultRequest($chosen_inline_result)) {
581-
$chosen_inline_result_local_id = self::$pdo->lastInsertId();
582-
583-
return self::insertTelegramUpdate(
584-
$update_id,
585-
null,
586-
null,
587-
null,
588-
$chosen_inline_result_local_id
589-
);
590-
}
591-
} elseif ($update_type === 'callback_query') {
592-
$callback_query = $update->getCallbackQuery();
593-
594-
if (self::insertCallbackQueryRequest($callback_query)) {
595-
$callback_query_id = $callback_query->getId();
596-
597-
return self::insertTelegramUpdate(
598-
$update_id,
599-
null,
600-
null,
601-
null,
602-
null,
603-
$callback_query_id
604-
);
605-
}
514+
$chat_id = null;
515+
$message_id = null;
516+
$edited_message_id = null;
517+
$channel_post_id = null;
518+
$edited_channel_post_id = null;
519+
$inline_query_id = null;
520+
$chosen_inline_result_id = null;
521+
$callback_query_id = null;
522+
$shipping_query_id = null;
523+
$pre_checkout_query_id = null;
524+
525+
if (($message = $update->getMessage()) && self::insertMessageRequest($message)) {
526+
$chat_id = $message->getChat()->getId();
527+
$message_id = $message->getMessageId();
528+
} elseif (($edited_message = $update->getEditedMessage()) && self::insertEditedMessageRequest($edited_message)) {
529+
$chat_id = $edited_message->getChat()->getId();
530+
$edited_message_id = self::$pdo->lastInsertId();
531+
} elseif (($channel_post = $update->getChannelPost()) && self::insertMessageRequest($channel_post)) {
532+
$chat_id = $channel_post->getChat()->getId();
533+
$channel_post_id = $channel_post->getMessageId();
534+
} elseif (($edited_channel_post = $update->getEditedChannelPost()) && self::insertEditedMessageRequest($edited_channel_post)) {
535+
$chat_id = $edited_channel_post->getChat()->getId();
536+
$edited_channel_post_id = self::$pdo->lastInsertId();
537+
} elseif (($inline_query = $update->getInlineQuery()) && self::insertInlineQueryRequest($inline_query)) {
538+
$inline_query_id = $inline_query->getId();
539+
} elseif (($chosen_inline_result = $update->getChosenInlineResult()) && self::insertChosenInlineResultRequest($chosen_inline_result)) {
540+
$chosen_inline_result_id = self::$pdo->lastInsertId();
541+
} elseif (($callback_query = $update->getCallbackQuery()) && self::insertCallbackQueryRequest($callback_query)) {
542+
$callback_query_id = $callback_query->getId();
543+
} elseif (($shipping_query = $update->getShippingQuery()) && self::insertShippingQueryRequest($shipping_query)) {
544+
$shipping_query_id = $shipping_query->getId();
545+
} elseif (($pre_checkout_query = $update->getPreCheckoutQuery()) && self::insertPreCheckoutQueryRequest($pre_checkout_query)) {
546+
$pre_checkout_query_id = $pre_checkout_query->getId();
547+
} else {
548+
return false;
606549
}
607550

608-
return false;
551+
return self::insertTelegramUpdate(
552+
$update->getUpdateId(),
553+
$chat_id,
554+
$message_id,
555+
$edited_message_id,
556+
$channel_post_id,
557+
$edited_channel_post_id,
558+
$inline_query_id,
559+
$chosen_inline_result_id,
560+
$callback_query_id,
561+
$shipping_query_id,
562+
$pre_checkout_query_id
563+
);
609564
}
610565

611566
/**
@@ -766,9 +721,96 @@ public static function insertCallbackQueryRequest(CallbackQuery $callback_query)
766721
}
767722

768723
/**
769-
* Insert Message request in db
724+
* Insert shipping query request into database
725+
*
726+
* @param ShippingQuery $shipping_query
770727
*
771-
* @todo Complete with new fields: https://core.telegram.org/bots/api#message
728+
* @return bool If the insert was successful
729+
* @throws TelegramException
730+
*/
731+
public static function insertShippingQueryRequest(ShippingQuery $shipping_query)
732+
{
733+
if (!self::isDbConnected()) {
734+
return false;
735+
}
736+
737+
try {
738+
$sth = self::$pdo->prepare('
739+
INSERT IGNORE INTO `' . TB_SHIPPING_QUERY . '`
740+
(`id`, `user_id`, `chat_id`, `invoice_payload`, `shipping_address`, `created_at`)
741+
VALUES
742+
(:id, :user_id, :chat_id, :invoice_payload, :shipping_address, :created_at)
743+
');
744+
745+
$date = self::getTimestamp();
746+
$user_id = null;
747+
748+
$user = $shipping_query->getFrom();
749+
if ($user instanceof User) {
750+
$user_id = $user->getId();
751+
self::insertUser($user, $date);
752+
}
753+
754+
$sth->bindValue(':id', $shipping_query->getId());
755+
$sth->bindValue(':user_id', $user_id);
756+
$sth->bindValue(':invoice_payload', $shipping_query->getInvoicePayload());
757+
$sth->bindValue(':shipping_address', $shipping_query->getShippingAddress());
758+
$sth->bindValue(':created_at', $date);
759+
760+
return $sth->execute();
761+
} catch (PDOException $e) {
762+
throw new TelegramException($e->getMessage());
763+
}
764+
}
765+
766+
/**
767+
* Insert pre checkout query request into database
768+
*
769+
* @param PreCheckoutQuery $pre_checkout_query
770+
*
771+
* @return bool If the insert was successful
772+
* @throws TelegramException
773+
*/
774+
public static function insertPreCheckoutQueryRequest(PreCheckoutQuery $pre_checkout_query)
775+
{
776+
if (!self::isDbConnected()) {
777+
return false;
778+
}
779+
780+
try {
781+
$sth = self::$pdo->prepare('
782+
INSERT IGNORE INTO `' . TB_PRE_CHECKOUT_QUERY . '`
783+
(`id`, `user_id`, `currency`, `total_amount`, `invoice_payload`, `shipping_option_id`, `order_info`, `created_at`)
784+
VALUES
785+
(:id, :user_id, :currency, :total_amount, :invoice_payload, :shipping_option_id, :order_info, :created_at)
786+
');
787+
788+
$date = self::getTimestamp();
789+
$user_id = null;
790+
791+
$user = $pre_checkout_query->getFrom();
792+
if ($user instanceof User) {
793+
$user_id = $user->getId();
794+
self::insertUser($user, $date);
795+
}
796+
797+
$sth->bindValue(':id', $pre_checkout_query->getId());
798+
$sth->bindValue(':user_id', $user_id);
799+
$sth->bindValue(':currency', $pre_checkout_query->getCurrency());
800+
$sth->bindValue(':total_amount', $pre_checkout_query->getTotalAmount());
801+
$sth->bindValue(':invoice_payload', $pre_checkout_query->getInvoicePayload());
802+
$sth->bindValue(':shipping_option_id', $pre_checkout_query->getShippingOptionId());
803+
$sth->bindValue(':order_info', $pre_checkout_query->getOrderInfo());
804+
$sth->bindValue(':created_at', $date);
805+
806+
return $sth->execute();
807+
} catch (PDOException $e) {
808+
throw new TelegramException($e->getMessage());
809+
}
810+
}
811+
812+
/**
813+
* Insert Message request in db
772814
*
773815
* @param Message $message
774816
*

0 commit comments

Comments
 (0)