Skip to content

Commit ce8452e

Browse files
authored
Merge pull request #1199 from php-telegram-bot/bot_api_5.1
Bot API 5.1
2 parents 91a5aa0 + ae84c26 commit ce8452e

16 files changed

+461
-118
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Notes
88
- [:ledger: View file changes][Unreleased][:page_with_curl: DB migration script][unreleased-sql-migration]
99
### Added
10+
- Bot API 5.1 (ChatMember Update types, Improved Invite Links, Voice Chat). (@massadm, @noplanman)
1011
### Changed
1112
### Deprecated
1213
### Removed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A Telegram Bot based on the official [Telegram Bot API]
99

10-
[![API Version](https://img.shields.io/badge/Bot%20API-5.0%20%28November%202020%29-32a2da.svg)](https://core.telegram.org/bots/api#november-4-2020)
10+
[![API Version](https://img.shields.io/badge/Bot%20API-5.1%20%28March%202021%29-32a2da.svg)](https://core.telegram.org/bots/api#november-4-2020)
1111
[![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)
1212
[![Donate](https://img.shields.io/badge/%F0%9F%92%99-Donate%20%2F%20Support%20Us-blue.svg)](#donate)
1313

@@ -78,7 +78,7 @@ This Bot aims to provide a platform where one can simply write a bot and have in
7878

7979
The Bot can:
8080
- Retrieve updates with [webhook](#webhook-installation) and [getUpdates](#getupdates-installation) methods.
81-
- Supports all types and methods according to Telegram Bot API 5.0 (November 2020).
81+
- Supports all types and methods according to Telegram Bot API 5.1 (March 2021).
8282
- Supports supergroups.
8383
- Handle commands in chat with other bots.
8484
- Manage Channel from the bot admin interface.
@@ -343,15 +343,15 @@ $telegram->useGetUpdatesWithoutDatabase();
343343

344344
### Types
345345

346-
All types are implemented according to Telegram API 5.0 (November 2020).
346+
All types are implemented according to Telegram API 5.1 (March 2021).
347347

348348
### Inline Query
349349

350-
Full support for inline query according to Telegram API 5.0 (November 2020).
350+
Full support for inline query according to Telegram API 5.1 (March 2021).
351351

352352
### Methods
353353

354-
All methods are implemented according to Telegram API 5.0 (November 2020).
354+
All methods are implemented according to Telegram API 5.1 (March 2021).
355355

356356
#### Send Message
357357

src/DB.php

Lines changed: 95 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Longman\TelegramBot\Entities\CallbackQuery;
1616
use Longman\TelegramBot\Entities\Chat;
17+
use Longman\TelegramBot\Entities\ChatMemberUpdated;
1718
use Longman\TelegramBot\Entities\ChosenInlineResult;
1819
use Longman\TelegramBot\Entities\InlineQuery;
1920
use Longman\TelegramBot\Entities\Message;
@@ -140,13 +141,14 @@ protected static function defineTables(): void
140141
$tables = [
141142
'callback_query',
142143
'chat',
144+
'chat_member_updated',
143145
'chosen_inline_result',
144146
'edited_message',
145147
'inline_query',
146148
'message',
147-
'pre_checkout_query',
148149
'poll',
149150
'poll_answer',
151+
'pre_checkout_query',
150152
'request_limiter',
151153
'shipping_query',
152154
'telegram_update',
@@ -320,6 +322,8 @@ public static function entitiesArrayToJson(array $entities, $default = null)
320322
* @param string|null $pre_checkout_query_id
321323
* @param string|null $poll_id
322324
* @param string|null $poll_answer_poll_id
325+
* @param string|null $my_chat_member_updated_id
326+
* @param string|null $chat_member_updated_id
323327
*
324328
* @return bool If the insert was successful
325329
* @throws TelegramException
@@ -337,10 +341,12 @@ protected static function insertTelegramUpdate(
337341
?string $shipping_query_id = null,
338342
?string $pre_checkout_query_id = null,
339343
?string $poll_id = null,
340-
?string $poll_answer_poll_id = null
344+
?string $poll_answer_poll_id = null,
345+
?string $my_chat_member_updated_id = null,
346+
?string $chat_member_updated_id = null
341347
): ?bool {
342-
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 && $poll_id === null && $poll_answer_poll_id === null) {
343-
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, poll_id, poll_answer_poll_id are all null');
348+
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 && $poll_id === null && $poll_answer_poll_id === null && $my_chat_member_updated_id === null && $chat_member_updated_id === null) {
349+
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, poll_id, poll_answer_poll_id, my_chat_member_updated_id, chat_member_updated_id are all null');
344350
}
345351

346352
if (!self::isDbConnected()) {
@@ -350,9 +356,9 @@ protected static function insertTelegramUpdate(
350356
try {
351357
$sth = self::$pdo->prepare('
352358
INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
353-
(`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`, `poll_id`, `poll_answer_poll_id`)
359+
(`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`, `poll_id`, `poll_answer_poll_id`, `my_chat_member_updated_id`, `chat_member_updated_id`)
354360
VALUES
355-
(: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, :poll_id, :poll_answer_poll_id)
361+
(: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, :poll_id, :poll_answer_poll_id, :my_chat_member_updated_id, :chat_member_updated_id)
356362
');
357363

358364
$sth->bindValue(':id', $update_id);
@@ -368,6 +374,8 @@ protected static function insertTelegramUpdate(
368374
$sth->bindValue(':pre_checkout_query_id', $pre_checkout_query_id);
369375
$sth->bindValue(':poll_id', $poll_id);
370376
$sth->bindValue(':poll_answer_poll_id', $poll_answer_poll_id);
377+
$sth->bindValue(':my_chat_member_updated_id', $my_chat_member_updated_id);
378+
$sth->bindValue(':chat_member_updated_id', $chat_member_updated_id);
371379

372380
return $sth->execute();
373381
} catch (PDOException $e) {
@@ -520,18 +528,20 @@ public static function insertRequest(Update $update): bool
520528
return false;
521529
}
522530

523-
$chat_id = null;
524-
$message_id = null;
525-
$edited_message_id = null;
526-
$channel_post_id = null;
527-
$edited_channel_post_id = null;
528-
$inline_query_id = null;
529-
$chosen_inline_result_id = null;
530-
$callback_query_id = null;
531-
$shipping_query_id = null;
532-
$pre_checkout_query_id = null;
533-
$poll_id = null;
534-
$poll_answer_poll_id = null;
531+
$chat_id = null;
532+
$message_id = null;
533+
$edited_message_id = null;
534+
$channel_post_id = null;
535+
$edited_channel_post_id = null;
536+
$inline_query_id = null;
537+
$chosen_inline_result_id = null;
538+
$callback_query_id = null;
539+
$shipping_query_id = null;
540+
$pre_checkout_query_id = null;
541+
$poll_id = null;
542+
$poll_answer_poll_id = null;
543+
$my_chat_member_updated_id = null;
544+
$chat_member_updated_id = null;
535545

536546
if (($message = $update->getMessage()) && self::insertMessageRequest($message)) {
537547
$chat_id = $message->getChat()->getId();
@@ -559,6 +569,10 @@ public static function insertRequest(Update $update): bool
559569
$poll_id = $poll->getId();
560570
} elseif (($poll_answer = $update->getPollAnswer()) && self::insertPollAnswerRequest($poll_answer)) {
561571
$poll_answer_poll_id = $poll_answer->getPollId();
572+
} elseif (($my_chat_member = $update->getMyChatMember()) && self::insertChatMemberUpdatedRequest($my_chat_member)) {
573+
$my_chat_member_updated_id = self::$pdo->lastInsertId();
574+
} elseif (($chat_member = $update->getChatMember()) && self::insertChatMemberUpdatedRequest($chat_member)) {
575+
$chat_member_updated_id = self::$pdo->lastInsertId();
562576
} else {
563577
return false;
564578
}
@@ -576,7 +590,9 @@ public static function insertRequest(Update $update): bool
576590
$shipping_query_id,
577591
$pre_checkout_query_id,
578592
$poll_id,
579-
$poll_answer_poll_id
593+
$poll_answer_poll_id,
594+
$my_chat_member_updated_id,
595+
$chat_member_updated_id
580596
);
581597
}
582598

@@ -918,6 +934,55 @@ public static function insertPollAnswerRequest(PollAnswer $poll_answer): bool
918934
}
919935
}
920936

937+
/**
938+
* Insert chat member updated request into database
939+
*
940+
* @param ChatMemberUpdated $chat_member_updated
941+
*
942+
* @return bool If the insert was successful
943+
* @throws TelegramException
944+
*/
945+
public static function insertChatMemberUpdatedRequest(ChatMemberUpdated $chat_member_updated): bool
946+
{
947+
if (!self::isDbConnected()) {
948+
return false;
949+
}
950+
951+
try {
952+
$sth = self::$pdo->prepare('
953+
INSERT INTO `' . TB_CHAT_MEMBER_UPDATED . '`
954+
(`chat_id`, `user_id`, `date`, `old_chat_member`, `new_chat_member`, `invite_link`, `created_at`)
955+
VALUES
956+
(:chat_id, :user_id, :date, :old_chat_member, :new_chat_member, :invite_link, :created_at)
957+
');
958+
959+
$date = self::getTimestamp();
960+
$chat_id = null;
961+
$user_id = null;
962+
963+
if ($chat = $chat_member_updated->getChat()) {
964+
$chat_id = $chat->getId();
965+
self::insertChat($chat, $date);
966+
}
967+
if ($user = $chat_member_updated->getFrom()) {
968+
$user_id = $user->getId();
969+
self::insertUser($user, $date);
970+
}
971+
972+
$sth->bindValue(':chat_id', $chat_id);
973+
$sth->bindValue(':user_id', $user_id);
974+
$sth->bindValue(':date', self::getTimestamp($chat_member_updated->getDate()));
975+
$sth->bindValue(':old_chat_member', $chat_member_updated->getOldChatMember());
976+
$sth->bindValue(':new_chat_member', $chat_member_updated->getNewChatMember());
977+
$sth->bindValue(':invite_link', $chat_member_updated->getInviteLink());
978+
$sth->bindValue(':created_at', $date);
979+
980+
return $sth->execute();
981+
} catch (PDOException $e) {
982+
throw new TelegramException($e->getMessage());
983+
}
984+
}
985+
921986
/**
922987
* Insert Message request in db
923988
*
@@ -998,17 +1063,19 @@ public static function insertMessageRequest(Message $message): bool
9981063
`audio`, `document`, `animation`, `game`, `photo`, `sticker`, `video`, `voice`, `video_note`, `caption`, `contact`,
9991064
`location`, `venue`, `poll`, `dice`, `new_chat_members`, `left_chat_member`,
10001065
`new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
1001-
`supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`,
1002-
`pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data`, `proximity_alert_triggered`, `reply_markup`
1066+
`supergroup_chat_created`, `channel_chat_created`, `message_auto_delete_timer_changed`, `migrate_to_chat_id`, `migrate_from_chat_id`,
1067+
`pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data`, `proximity_alert_triggered`,
1068+
`voice_chat_started`, `voice_chat_ended`, `voice_chat_participants_invited`, `reply_markup`
10031069
) VALUES (
10041070
:message_id, :user_id, :chat_id, :sender_chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id,
10051071
:forward_signature, :forward_sender_name, :forward_date,
10061072
:reply_to_chat, :reply_to_message, :via_bot, :edit_date, :media_group_id, :author_signature, :text, :entities, :caption_entities,
10071073
:audio, :document, :animation, :game, :photo, :sticker, :video, :voice, :video_note, :caption, :contact,
10081074
:location, :venue, :poll, :dice, :new_chat_members, :left_chat_member,
10091075
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created,
1010-
:supergroup_chat_created, :channel_chat_created, :migrate_to_chat_id, :migrate_from_chat_id,
1011-
:pinned_message, :invoice, :successful_payment, :connected_website, :passport_data, :proximity_alert_triggered, :reply_markup
1076+
:supergroup_chat_created, :channel_chat_created, :message_auto_delete_timer_changed, :migrate_to_chat_id, :migrate_from_chat_id,
1077+
:pinned_message, :invoice, :successful_payment, :connected_website, :passport_data, :proximity_alert_triggered,
1078+
:voice_chat_started, :voice_chat_ended, :voice_chat_participants_invited, :reply_markup
10121079
)
10131080
');
10141081

@@ -1043,7 +1110,7 @@ public static function insertMessageRequest(Message $message): bool
10431110
$sth->bindValue(':reply_to_message', $reply_to_message_id);
10441111

10451112
$sth->bindValue(':via_bot', $via_bot_id);
1046-
$sth->bindValue(':edit_date', $message->getEditDate());
1113+
$sth->bindValue(':edit_date', self::getTimestamp($message->getEditDate()));
10471114
$sth->bindValue(':media_group_id', $message->getMediaGroupId());
10481115
$sth->bindValue(':author_signature', $message->getAuthorSignature());
10491116
$sth->bindValue(':text', $message->getText());
@@ -1072,6 +1139,7 @@ public static function insertMessageRequest(Message $message): bool
10721139
$sth->bindValue(':group_chat_created', $message->getGroupChatCreated());
10731140
$sth->bindValue(':supergroup_chat_created', $message->getSupergroupChatCreated());
10741141
$sth->bindValue(':channel_chat_created', $message->getChannelChatCreated());
1142+
$sth->bindValue(':message_auto_delete_timer_changed', $message->getMessageAutoDeleteTimerChanged());
10751143
$sth->bindValue(':migrate_to_chat_id', $message->getMigrateToChatId());
10761144
$sth->bindValue(':migrate_from_chat_id', $message->getMigrateFromChatId());
10771145
$sth->bindValue(':pinned_message', $message->getPinnedMessage());
@@ -1080,6 +1148,9 @@ public static function insertMessageRequest(Message $message): bool
10801148
$sth->bindValue(':connected_website', $message->getConnectedWebsite());
10811149
$sth->bindValue(':passport_data', $message->getPassportData());
10821150
$sth->bindValue(':proximity_alert_triggered', $message->getProximityAlertTriggered());
1151+
$sth->bindValue(':voice_chat_started', $message->getVoiceChatStarted());
1152+
$sth->bindValue(':voice_chat_ended', $message->getVoiceChatEnded());
1153+
$sth->bindValue(':voice_chat_participants_invited', $message->getVoiceChatParticipantsInvited());
10831154
$sth->bindValue(':reply_markup', $message->getReplyMarkup());
10841155

10851156
return $sth->execute();

src/Entities/ChatInviteLink.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Longman\TelegramBot\Entities;
13+
14+
/**
15+
* Class ChatInviteLink
16+
*
17+
* Represents an invite link for a chat
18+
*
19+
* @link https://core.telegram.org/bots/api#chatinvitelink
20+
*
21+
* @method string GetInviteLink() The invite link. If the link was created by another chat administrator, then the second part of the link will be replaced with “…”
22+
* @method User getCreator() Creator of the link
23+
* @method bool getIsPrimary() True, if the link is primary
24+
* @method bool getIsRevoked() True, if the link is revoked
25+
* @method int getExpireDate() Optional. Point in time (Unix timestamp) when the link will expire or has been expired
26+
* @method int getMemberLimit() Optional. Maximum number of users that can be members of the chat simultaneously after joining the chat via this invite link; 1-99999
27+
*/
28+
class ChatInviteLink extends Entity
29+
{
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function subEntities(): array
34+
{
35+
return [
36+
'creator' => User::class,
37+
];
38+
}
39+
}

0 commit comments

Comments
 (0)