From 25e39af2918b5cb63ebb913235ec4bce9c1834d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sat, 25 May 2024 00:18:57 +0200 Subject: [PATCH 1/3] Bot API 7.1 --- CHANGELOG.md | 1 + README.md | 10 ++++---- src/DB.php | 16 ++++++++----- src/Entities/Chat.php | 2 ++ src/Entities/ChatBoostAdded.php | 24 ++++++++++++++++++++ src/Entities/Message.php | 15 ++++++++---- src/Entities/Story.php | 13 ++++++++++- structure.sql | 3 +++ utils/db-schema-update/0.82.0-unreleased.sql | 5 +++- 9 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 src/Entities/ChatBoostAdded.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c7bc9263..ecc1ff70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c - [:exclamation:][unreleased-bc-minimum-php-81] PHP 8.1+ required! ### Added - Bot API 7.0 (@noplanman, @TiiFuchs) (#1459) +- Bot API 7.1 (@noplanman) ### Changed - [:exclamation:][unreleased-bc-user-to-users] Various fields have been pluralised from "user" to "users". ### Deprecated diff --git a/README.md b/README.md index 8be06976..0a227de7 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A Telegram Bot based on the official [Telegram Bot API] -[![API Version](https://img.shields.io/badge/Bot%20API-6.9%20%28September%202023%29-32a2da.svg)](https://core.telegram.org/bots/api-changelog#september-22-2023) +[![API Version](https://img.shields.io/badge/Bot%20API-7.1%20%28February%202024%29-32a2da.svg)](https://core.telegram.org/bots/api-changelog#february-16-2024) [![Join the bot support group on Telegram](https://img.shields.io/badge/telegram-@PHP__Telegram__Bot__Support-64659d.svg)](https://telegram.me/PHP_Telegram_Bot_Support) [![Donate](https://img.shields.io/badge/%F0%9F%92%99-Donate%20%2F%20Support%20Us-blue.svg)](#donate) @@ -78,7 +78,7 @@ This Bot aims to provide a platform where one can simply write a bot and have in The Bot can: - Retrieve updates with [webhook](#webhook-installation) and [getUpdates](#getupdates-installation) methods. -- Supports all types and methods according to Telegram Bot API 6.9 (September 2023). +- Supports all types and methods according to Telegram Bot API 7.1 (February 2024). - Supports supergroups. - Handle commands in chat with other bots. - Manage Channel from the bot admin interface. @@ -389,15 +389,15 @@ The reason for denying an update can be defined with the `$reason` parameter. Th ### Types -All types are implemented according to Telegram API 6.9 (September 2023). +All types are implemented according to Telegram API 7.1 (February 2024). ### Inline Query -Full support for inline query according to Telegram API 6.9 (September 2023). +Full support for inline query according to Telegram API 7.1 (February 2024). ### Methods -All methods are implemented according to Telegram API 6.9 (September 2023). +All methods are implemented according to Telegram API 7.1 (February 2024). #### Send Message diff --git a/src/DB.php b/src/DB.php index cae89be1..487ea7ff 100644 --- a/src/DB.php +++ b/src/DB.php @@ -1315,25 +1315,25 @@ public static function insertMessageRequest(Message $message): bool $sth = self::$pdo->prepare(' INSERT IGNORE INTO `' . TB_MESSAGE . '` ( - `id`, `user_id`, `chat_id`, `message_thread_id`, `sender_chat_id`, `date`, `forward_from`, `forward_from_chat`, `forward_from_message_id`, + `id`, `user_id`, `chat_id`, `message_thread_id`, `sender_chat_id`, `sender_boost_count`, `date`, `forward_from`, `forward_from_chat`, `forward_from_message_id`, `forward_signature`, `forward_sender_name`, `forward_date`, `is_topic_message`, - `reply_to_chat`, `reply_to_message`, `external_reply`, `via_bot`, `link_preview_options`, `edit_date`, `media_group_id`, `author_signature`, `text`, `entities`, `caption_entities`, + `reply_to_chat`, `reply_to_message`, `external_reply`, `quote`, `reply_to_story`, `via_bot`, `link_preview_options`, `edit_date`, `media_group_id`, `author_signature`, `text`, `entities`, `caption_entities`, `audio`, `document`, `animation`, `game`, `photo`, `sticker`, `story`, `video`, `voice`, `video_note`, `caption`, `has_media_spoiler`, `contact`, `location`, `venue`, `poll`, `dice`, `new_chat_members`, `left_chat_member`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`, `supergroup_chat_created`, `channel_chat_created`, `message_auto_delete_timer_changed`, `migrate_to_chat_id`, `migrate_from_chat_id`, - `pinned_message`, `invoice`, `successful_payment`, `users_shared`, `chat_shared`, `connected_website`, `write_access_allowed`, `passport_data`, `proximity_alert_triggered`, + `pinned_message`, `invoice`, `successful_payment`, `users_shared`, `chat_shared`, `connected_website`, `write_access_allowed`, `passport_data`, `proximity_alert_triggered`, `boost_added`, `forum_topic_created`, `forum_topic_edited`, `forum_topic_closed`, `forum_topic_reopened`, `general_forum_topic_hidden`, `general_forum_topic_unhidden`, `video_chat_scheduled`, `video_chat_started`, `video_chat_ended`, `video_chat_participants_invited`, `web_app_data`, `reply_markup` ) VALUES ( - :message_id, :user_id, :chat_id, :message_thread_id, :sender_chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id, + :message_id, :user_id, :chat_id, :message_thread_id, :sender_chat_id, :sender_boost_count, :date, :forward_from, :forward_from_chat, :forward_from_message_id, :forward_signature, :forward_sender_name, :forward_date, :is_topic_message, - :reply_to_chat, :reply_to_message, :external_reply, :via_bot, :link_preview_options, :edit_date, :media_group_id, :author_signature, :text, :entities, :caption_entities, + :reply_to_chat, :reply_to_message, :external_reply, :quote, :reply_to_story, :via_bot, :link_preview_options, :edit_date, :media_group_id, :author_signature, :text, :entities, :caption_entities, :audio, :document, :animation, :game, :photo, :sticker, :story, :video, :voice, :video_note, :caption, :has_media_spoiler, :contact, :location, :venue, :poll, :dice, :new_chat_members, :left_chat_member, :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created, :supergroup_chat_created, :channel_chat_created, :message_auto_delete_timer_changed, :migrate_to_chat_id, :migrate_from_chat_id, - :pinned_message, :invoice, :successful_payment, :users_shared, :chat_shared, :connected_website, :write_access_allowed, :passport_data, :proximity_alert_triggered, + :pinned_message, :invoice, :successful_payment, :users_shared, :chat_shared, :connected_website, :write_access_allowed, :passport_data, :proximity_alert_triggered, :boost_added, :forum_topic_created, :forum_topic_edited, :forum_topic_closed, :forum_topic_reopened, :general_forum_topic_hidden, :general_forum_topic_unhidden, :video_chat_scheduled, :video_chat_started, :video_chat_ended, :video_chat_participants_invited, :web_app_data, :reply_markup ) @@ -1355,6 +1355,7 @@ public static function insertMessageRequest(Message $message): bool $sth->bindValue(':sender_chat_id', $sender_chat_id); $sth->bindValue(':message_thread_id', $message->getMessageThreadId()); $sth->bindValue(':user_id', $user_id); + $sth->bindValue(':sender_boost_count', $message->getSenderBoostCount()); $sth->bindValue(':date', $date); $sth->bindValue(':forward_from', $forward_from); $sth->bindValue(':forward_from_chat', $forward_from_chat); @@ -1372,6 +1373,8 @@ public static function insertMessageRequest(Message $message): bool $sth->bindValue(':reply_to_message', $reply_to_message_id); $sth->bindValue(':external_reply', $message->getExternalReply()); + $sth->bindValue(':quote', $message->getQuote()); + $sth->bindValue(':reply_to_story', $message->getReplyToStory()); $sth->bindValue(':via_bot', $via_bot_id); $sth->bindValue(':link_preview_options', $message->getLinkPreviewOptions()); $sth->bindValue(':edit_date', self::getTimestamp($message->getEditDate())); @@ -1417,6 +1420,7 @@ public static function insertMessageRequest(Message $message): bool $sth->bindValue(':write_access_allowed', $message->getWriteAccessAllowed()); $sth->bindValue(':passport_data', $message->getPassportData()); $sth->bindValue(':proximity_alert_triggered', $message->getProximityAlertTriggered()); + $sth->bindValue(':boost_added', $message->getBoostAdded()); $sth->bindValue(':forum_topic_created', $message->getForumTopicCreated()); $sth->bindValue(':forum_topic_edited', $message->getForumTopicEdited()); $sth->bindValue(':forum_topic_closed', $message->getForumTopicClosed()); diff --git a/src/Entities/Chat.php b/src/Entities/Chat.php index 8a46c519..284e58b4 100644 --- a/src/Entities/Chat.php +++ b/src/Entities/Chat.php @@ -47,6 +47,7 @@ * @method Message getPinnedMessage() Optional. Pinned message, for groups, supergroups and channels. Returned only in getChat. * @method ChatPermissions getPermissions() Optional. Default chat member permissions, for groups and supergroups. Returned only in getChat. * @method int getSlowModeDelay() Optional. For supergroups, the minimum allowed delay between consecutive messages sent by each unpriviledged user. Returned only in getChat. + * @method int getUnrestrictBoostCount() Optional. For supergroups, the minimum number of boosts that a non-administrator user needs to add in order to ignore slow mode and chat permissions. * @method int getMessageAutoDeleteTime() Optional. The time after which all messages sent to the chat will be automatically deleted; in seconds. Returned only in getChat. * @method bool getHasAggressiveAntiSpamEnabled() Optional. True, if aggressive anti-spam checks are enabled in the supergroup. The field is only available to chat administrators. Returned only in getChat. * @method bool getHasHiddenMembers() Optional. True, if non-administrators can only get the list of bots and administrators in the chat. Returned only in getChat. @@ -54,6 +55,7 @@ * @method bool getHasVisibleHistory() Optional. True, if new chat members will have access to old messages; available only to chat administrators * @method string getStickerSetName() Optional. For supergroups, name of group sticker set. Returned only in getChat. * @method bool getCanSetStickerSet() Optional. True, if the bot can change the group sticker set. Returned only in getChat. + * @method string getCustomEmojiStickerSetName() Optional. For supergroups, the name of the group's custom emoji sticker set. Custom emoji from this set can be used by all users and bots in the group. * @method int getLinkedChatId() Optional. Unique identifier for the linked chat. Returned only in getChat. * @method ChatLocation getLocation() Optional. For supergroups, the location to which the supergroup is connected. Returned only in getChat. */ diff --git a/src/Entities/ChatBoostAdded.php b/src/Entities/ChatBoostAdded.php new file mode 100644 index 00000000..baa5d8bb --- /dev/null +++ b/src/Entities/ChatBoostAdded.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities; + +/** + * This object represents a service message about a user boosting a chat. + * + * @link https://core.telegram.org/bots/api#chatboostadded + * + * @method int getBoostCount() Number of boosts added by the user + */ +class ChatBoostAdded extends Entity +{ + +} diff --git a/src/Entities/Message.php b/src/Entities/Message.php index 8f771a30..05231220 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -38,9 +38,11 @@ * @link https://core.telegram.org/bots/api#message * * @method int getMessageId() Unique message identifier - * @method int getMessageThreadId() Optional. Unique identifier of a message thread to which the message belongs; for supergroups only + * @method int getMessageThreadId() Optional. Unique identifier of a message thread to which the message belongs; +for supergroups only * @method User getFrom() Optional. Sender, can be empty for messages sent to channels * @method Chat getSenderChat() Optional. Sender of the message, sent on behalf of a chat. The channel itself for channel messages. The supergroup itself for messages from anonymous group administrators. The linked channel for messages automatically forwarded to the discussion group + * @method int getSenderBoostCount() Optional. If the sender of the message boosted the chat, the number of boosts added by the user * @method int getDate() Date the message was sent in Unix time * @method Chat getChat() Conversation the message belongs to * @method MessageOrigin getForwardOrigin() Optional. Information about the original message for forwarded messages @@ -49,6 +51,7 @@ * @method ReplyToMessage getReplyToMessage() Optional. For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply. * @method ExternalReplyInfo getExternalReply() Optional. Information about the message that is being replied to, which may come from another chat or forum topic * @method TextQuote getQuote() Optional. For replies that quote part of the original message, the quoted part of the message + * @method Story getReplyToStory() Optional. For replies to a story, the original story * @method User getViaBot() Optional. Bot through which the message was sent * @method int getEditDate() Optional. Date the message was last edited in Unix time * @method bool getHasProtectedContent() Optional. True, if the message can't be forwarded @@ -94,6 +97,7 @@ * @method WriteAccessAllowed getWriteAccessAllowed() Optional. Service message: the user allowed the bot added to the attachment menu to write messages * @method PassportData getPassportData() Optional. Telegram Passport data * @method ProximityAlertTriggered getProximityAlertTriggered() Optional. Service message. A user in the chat triggered another user's proximity alert while sharing Live Location. + * @method ChatBoostAdded getBoostAdded() Optional. Service message: user boosted the chat * @method ForumTopicCreated getForumTopicCreated() Optional. Service message: forum topic created * @method ForumTopicEdited getForumTopicEdited() Optional. Service message: forum topic edited * @method ForumTopicClosed getForumTopicClosed() Optional. Service message: forum topic closed @@ -126,6 +130,7 @@ protected function subEntities(): array 'reply_to_message' => ReplyToMessage::class, 'external_reply' => ExternalReplyInfo::class, 'quote' => TextQuote::class, + 'reply_to_story' => Story::class, 'via_bot' => User::class, 'link_preview_options' => LinkPreviewOptions::class, 'entities' => [MessageEntity::class], @@ -157,6 +162,7 @@ protected function subEntities(): array 'write_access_allowed' => WriteAccessAllowed::class, 'passport_data' => PassportData::class, 'proximity_alert_triggered' => ProximityAlertTriggered::class, + 'boost_added' => ChatBoostAdded::class, 'forum_topic_created' => ForumTopicCreated::class, 'forum_topic_edited' => ForumTopicEdited::class, 'forum_topic_closed' => ForumTopicClosed::class, @@ -188,7 +194,7 @@ public function getFullCommand(): ?string return null; } - $no_EOL = strtok($text, PHP_EOL); + $no_EOL = strtok($text, PHP_EOL); $no_space = strtok($text, ' '); //try to understand which separator \n or space divide /command from text @@ -214,7 +220,7 @@ public function getCommand(): ?string //check if command is followed by bot username $split_cmd = explode('@', $full_command); - if (! isset($split_cmd[1])) { + if (!isset($split_cmd[1])) { //command is not followed by name return $full_command; } @@ -230,7 +236,7 @@ public function getCommand(): ?string /** * For text messages, the actual UTF-8 text of the message, 0-4096 characters. * - * @param bool $without_cmd + * @param bool $without_cmd * * @return string|null */ @@ -307,6 +313,7 @@ public function getType(): string 'write_access_allowed', 'passport_data', 'proximity_alert_triggered', + 'boost_added', 'forum_topic_created', 'forum_topic_edited', 'forum_topic_closed', diff --git a/src/Entities/Story.php b/src/Entities/Story.php index 5647db22..64fc066e 100644 --- a/src/Entities/Story.php +++ b/src/Entities/Story.php @@ -15,8 +15,19 @@ * Class Story * * @link https://core.telegram.org/bots/api#story + * + * @method Chat getChat() Chat that posted the story + * @method int getId() Unique identifier for the story in the chat */ class Story extends Entity { - + /** + * {@inheritdoc} + */ + protected function subEntities(): array + { + return [ + 'chat' => Chat::class, + ]; + } } diff --git a/structure.sql b/structure.sql index cf806153..7eccb74b 100644 --- a/structure.sql +++ b/structure.sql @@ -122,6 +122,8 @@ CREATE TABLE IF NOT EXISTS `message` ( `reply_to_chat` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier', `reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message that this message is reply to', `external_reply` TEXT NULL DEFAULT NULL COMMENT 'Optional. Information about the message that is being replied to, which may come from another chat or forum topic', + `quote` TEXT NULL DEFAULT NULL COMMENT 'Optional. For replies that quote part of the original message, the quoted part of the message', + `reply_to_story` TEXT NULL DEFAULT NULL COMMENT 'Optional. For replies to a story, the original story', `via_bot` bigint NULL DEFAULT NULL COMMENT 'Optional. Bot through which the message was sent', `link_preview_options` TEXT NULL DEFAULT NULL COMMENT 'Optional. Options used for link preview generation for the message, if it is a text message and link preview options were changed', `edit_date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was last edited in Unix time', @@ -168,6 +170,7 @@ CREATE TABLE IF NOT EXISTS `message` ( `write_access_allowed` TEXT DEFAULT NULL COMMENT 'Service message: the user allowed the bot added to the attachment menu to write messages', `passport_data` TEXT NULL COMMENT 'Telegram Passport data', `proximity_alert_triggered` TEXT NULL COMMENT 'Service message. A user in the chat triggered another user''s proximity alert while sharing Live Location.', + `boost_added` TEXT NULL COMMENT 'Service message: user boosted the chat', `forum_topic_created` TEXT DEFAULT NULL COMMENT 'Service message: forum topic created', `forum_topic_edited` TEXT DEFAULT NULL COMMENT 'Service message: forum topic edited', `forum_topic_closed` TEXT DEFAULT NULL COMMENT 'Service message: forum topic closed', diff --git a/utils/db-schema-update/0.82.0-unreleased.sql b/utils/db-schema-update/0.82.0-unreleased.sql index 3fb04f37..313ad567 100644 --- a/utils/db-schema-update/0.82.0-unreleased.sql +++ b/utils/db-schema-update/0.82.0-unreleased.sql @@ -60,7 +60,10 @@ CREATE TABLE IF NOT EXISTS `chat_boost_removed` ( ALTER TABLE `message` ADD COLUMN `external_reply` TEXT NULL DEFAULT NULL COMMENT 'Optional. Information about the message that is being replied to, which may come from another chat or forum topic' AFTER `reply_to_message`, ADD COLUMN `link_preview_options` TEXT NULL DEFAULT NULL COMMENT 'Optional. Options used for link preview generation for the message, if it is a text message and link preview options were changed' AFTER `via_bot`, - CHANGE COLUMN `user_shared` `users_shared` TEXT; + CHANGE COLUMN `user_shared` `users_shared` TEXT, + ADD COLUMN `boost_added` TEXT NULL COMMENT 'Service message: user boosted the chat' AFTER `proximity_alert_triggered`, + ADD COLUMN `quote` TEXT NULL DEFAULT NULL COMMENT 'Optional. For replies that quote part of the original message, the quoted part of the message' AFTER `external_reply`, + ADD COLUMN `reply_to_story` TEXT NULL DEFAULT NULL COMMENT 'Optional. For replies to a story, the original story' AFTER `quote`; ALTER TABLE `telegram_update` ADD COLUMN `message_reaction_id` bigint UNSIGNED DEFAULT NULL COMMENT 'A reaction to a message was changed by a user' AFTER `edited_channel_post_id`, From 60873f9802b77c0acabf8c2df3ac2ecfc25e4886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sat, 25 May 2024 00:23:16 +0200 Subject: [PATCH 2/3] PHPCS fixes, dafuq did these come from?! --- src/Commands/Command.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Commands/Command.php b/src/Commands/Command.php index 0d532508..5ddba9d6 100644 --- a/src/Commands/Command.php +++ b/src/Commands/Command.php @@ -193,7 +193,7 @@ public function preExecute(): ServerResponse 'text' => sprintf( "/%s command is only available in a private chat.\n(`%s`)", $this->getName(), - $message->getText() + $message->getText(), ), ]); } @@ -422,11 +422,11 @@ public function replyToChat(string $text, array $data = []): ServerResponse 'chat_id' => $message->getChat()->getId(), 'text' => $text, ]; - + if ($message->getIsTopicMessage()) { $reply['message_thread_id'] = $message->getMessageThreadId(); } - + return Request::sendMessage(array_merge($reply, $data)); } From 099191139daf3d82126b45da828891a859f9a195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sat, 25 May 2024 00:29:41 +0200 Subject: [PATCH 3/3] Add missing DB column --- structure.sql | 1 + utils/db-schema-update/0.82.0-unreleased.sql | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/structure.sql b/structure.sql index 7eccb74b..357501ec 100644 --- a/structure.sql +++ b/structure.sql @@ -110,6 +110,7 @@ CREATE TABLE IF NOT EXISTS `message` ( `id` bigint UNSIGNED COMMENT 'Unique message identifier', `message_thread_id` bigint(20) DEFAULT NULL COMMENT 'Unique identifier of a message thread to which the message belongs; for supergroups only', `user_id` bigint NULL COMMENT 'Unique user identifier', + `sender_boost_count` bigint NULL COMMENT 'If the sender of the message boosted the chat, the number of boosts added by the user', `date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was sent in timestamp format', `forward_from` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier, sender of the original message', `forward_from_chat` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier, chat the original message belongs to', diff --git a/utils/db-schema-update/0.82.0-unreleased.sql b/utils/db-schema-update/0.82.0-unreleased.sql index 313ad567..537307c7 100644 --- a/utils/db-schema-update/0.82.0-unreleased.sql +++ b/utils/db-schema-update/0.82.0-unreleased.sql @@ -63,7 +63,8 @@ ALTER TABLE `message` CHANGE COLUMN `user_shared` `users_shared` TEXT, ADD COLUMN `boost_added` TEXT NULL COMMENT 'Service message: user boosted the chat' AFTER `proximity_alert_triggered`, ADD COLUMN `quote` TEXT NULL DEFAULT NULL COMMENT 'Optional. For replies that quote part of the original message, the quoted part of the message' AFTER `external_reply`, - ADD COLUMN `reply_to_story` TEXT NULL DEFAULT NULL COMMENT 'Optional. For replies to a story, the original story' AFTER `quote`; + ADD COLUMN `reply_to_story` TEXT NULL DEFAULT NULL COMMENT 'Optional. For replies to a story, the original story' AFTER `quote`, + ADD COLUMN `sender_boost_count` bigint NULL COMMENT 'If the sender of the message boosted the chat, the number of boosts added by the user' AFTER `user_id`; ALTER TABLE `telegram_update` ADD COLUMN `message_reaction_id` bigint UNSIGNED DEFAULT NULL COMMENT 'A reaction to a message was changed by a user' AFTER `edited_channel_post_id`,