Skip to content

Commit 10ca443

Browse files
committed
Bot API 7.0 - Chat Boost
1 parent 4ba3ee9 commit 10ca443

14 files changed

+437
-6
lines changed

src/DB.php

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use Longman\TelegramBot\Entities\CallbackQuery;
1616
use Longman\TelegramBot\Entities\Chat;
17+
use Longman\TelegramBot\Entities\ChatBoostRemoved;
18+
use Longman\TelegramBot\Entities\ChatBoostUpdated;
1719
use Longman\TelegramBot\Entities\ChatJoinRequest;
1820
use Longman\TelegramBot\Entities\ChatMemberUpdated;
1921
use Longman\TelegramBot\Entities\ChosenInlineResult;
@@ -149,6 +151,8 @@ protected static function defineTables(): void
149151
$tables = [
150152
'callback_query',
151153
'chat',
154+
'chat_boost_updated',
155+
'chat_boost_removed',
152156
'chat_join_request',
153157
'chat_member_updated',
154158
'chosen_inline_result',
@@ -340,10 +344,12 @@ protected static function insertTelegramUpdate(
340344
?string $poll_answer_poll_id = null,
341345
?string $my_chat_member_updated_id = null,
342346
?string $chat_member_updated_id = null,
343-
?string $chat_join_request_id = null
347+
?string $chat_join_request_id = null,
348+
?string $chat_boost_updated_id = null,
349+
?string $chat_boost_removed_id = null
344350
): ?bool {
345-
if ($message_id === null && $edited_message_id === null && $channel_post_id === null && $edited_channel_post_id === null && $message_reaction_id === null && $message_reaction_count_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 && $chat_join_request_id === null) {
346-
throw new TelegramException('message_id, edited_message_id, channel_post_id, edited_channel_post_id, message_reaction_id, message_reaction_count_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');
351+
if ($message_id === null && $edited_message_id === null && $channel_post_id === null && $edited_channel_post_id === null && $message_reaction_id === null && $message_reaction_count_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 && $chat_join_request_id === null && $chat_boost_updated_id === null && $chat_boost_removed_id === null) {
352+
throw new TelegramException('message_id, edited_message_id, channel_post_id, edited_channel_post_id, message_reaction_id, message_reaction_count_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, chat_join_request_id, chat_boost_updated_id, chat_boost_removed_id are all null');
347353
}
348354

349355
if (!self::isDbConnected()) {
@@ -359,14 +365,14 @@ protected static function insertTelegramUpdate(
359365
`inline_query_id`, `chosen_inline_result_id`,
360366
`callback_query_id`, `shipping_query_id`, `pre_checkout_query_id`,
361367
`poll_id`, `poll_answer_poll_id`, `my_chat_member_updated_id`, `chat_member_updated_id`,
362-
`chat_join_request_id`
368+
`chat_join_request_id`, `chat_boost_updated_id`, `chat_boost_removed_id`
363369
) VALUES (
364370
:id, :chat_id, :message_id, :edited_message_id,
365371
:channel_post_id, :edited_channel_post_id, :message_reaction_id, :message_reaction_count_id,
366372
:inline_query_id, :chosen_inline_result_id,
367373
:callback_query_id, :shipping_query_id, :pre_checkout_query_id,
368374
:poll_id, :poll_answer_poll_id, :my_chat_member_updated_id, :chat_member_updated_id,
369-
:chat_join_request_id
375+
:chat_join_request_id, :chat_boost_updated_id, :chat_boost_removed_id
370376
)
371377
');
372378

@@ -388,6 +394,8 @@ protected static function insertTelegramUpdate(
388394
$sth->bindValue(':my_chat_member_updated_id', $my_chat_member_updated_id);
389395
$sth->bindValue(':chat_member_updated_id', $chat_member_updated_id);
390396
$sth->bindValue(':chat_join_request_id', $chat_join_request_id);
397+
$sth->bindValue(':chat_boost_updated_id', $chat_boost_updated_id);
398+
$sth->bindValue(':chat_boost_removed_id', $chat_boost_removed_id);
391399

392400
return $sth->execute();
393401
} catch (PDOException $e) {
@@ -561,6 +569,8 @@ public static function insertRequest(Update $update): bool
561569
$my_chat_member_updated_id = null;
562570
$chat_member_updated_id = null;
563571
$chat_join_request_id = null;
572+
$chat_boost_updated_id = null;
573+
$chat_boost_removed_id = null;
564574

565575
if (($message = $update->getMessage()) && self::insertMessageRequest($message)) {
566576
$chat_id = $message->getChat()->getId();
@@ -602,6 +612,10 @@ public static function insertRequest(Update $update): bool
602612
$chat_member_updated_id = self::$pdo->lastInsertId();
603613
} elseif (($chat_join_request = $update->getChatJoinRequest()) && self::insertChatJoinRequestRequest($chat_join_request)) {
604614
$chat_join_request_id = self::$pdo->lastInsertId();
615+
} elseif (($chat_boost_updated = $update->getChatBoost()) && self::insertChatBoostUpdatedRequest($chat_boost_updated)) {
616+
$chat_boost_updated_id = self::$pdo->lastInsertId();
617+
} elseif (($chat_boost_removed = $update->getRemovedChatBoost()) && self::insertChatBoostRemovedRequest($chat_boost_removed)) {
618+
$chat_boost_removed_id = self::$pdo->lastInsertId();
605619
} else {
606620
return false;
607621
}
@@ -624,7 +638,9 @@ public static function insertRequest(Update $update): bool
624638
$poll_answer_poll_id,
625639
$my_chat_member_updated_id,
626640
$chat_member_updated_id,
627-
$chat_join_request_id
641+
$chat_join_request_id,
642+
$chat_boost_updated_id,
643+
$chat_boost_removed_id
628644
);
629645
}
630646

@@ -1143,6 +1159,88 @@ public static function insertChatJoinRequestRequest(ChatJoinRequest $chat_join_r
11431159
}
11441160
}
11451161

1162+
/**
1163+
* Insert chat boost updated into database
1164+
*
1165+
* @param ChatBoostUpdated $chat_boost_updated
1166+
*
1167+
* @return bool If the insert was successful
1168+
* @throws TelegramException
1169+
*/
1170+
public static function insertChatBoostUpdatedRequest(ChatBoostUpdated $chat_boost_updated): bool
1171+
{
1172+
if (!self::isDbConnected()) {
1173+
return false;
1174+
}
1175+
1176+
try {
1177+
$sth = self::$pdo->prepare('
1178+
INSERT INTO `' . TB_CHAT_BOOST_UPDATED . '`
1179+
(`chat_id`, `boost`, `created_at`)
1180+
VALUES
1181+
(:chat_id, :boost, :created_at)
1182+
');
1183+
1184+
$date = self::getTimestamp();
1185+
$chat_id = null;
1186+
1187+
if ($chat = $chat_boost_updated->getChat()) {
1188+
$chat_id = $chat->getId();
1189+
self::insertChat($chat, $date);
1190+
}
1191+
1192+
$sth->bindValue(':chat_id', $chat_id);
1193+
$sth->bindValue(':boost', $chat_boost_updated->getBoost());
1194+
$sth->bindValue(':created_at', $date);
1195+
1196+
return $sth->execute();
1197+
} catch (PDOException $e) {
1198+
throw new TelegramException($e->getMessage());
1199+
}
1200+
}
1201+
1202+
/**
1203+
* Insert chat boost removed into database
1204+
*
1205+
* @param ChatBoostRemoved $chat_boost_removed
1206+
*
1207+
* @return bool If the insert was successful
1208+
* @throws TelegramException
1209+
*/
1210+
public static function insertChatBoostRemovedRequest(ChatBoostRemoved $chat_boost_removed): bool
1211+
{
1212+
if (!self::isDbConnected()) {
1213+
return false;
1214+
}
1215+
1216+
try {
1217+
$sth = self::$pdo->prepare('
1218+
INSERT INTO `' . TB_CHAT_BOOST_REMOVED . '`
1219+
(`chat_id`, `boost_id`, `remove_date`, `source`, `created_at`)
1220+
VALUES
1221+
(:chat_id, :boost_id, :remove_date, :source, :created_at)
1222+
');
1223+
1224+
$date = self::getTimestamp();
1225+
$chat_id = null;
1226+
1227+
if ($chat = $chat_boost_removed->getChat()) {
1228+
$chat_id = $chat->getId();
1229+
self::insertChat($chat, $date);
1230+
}
1231+
1232+
$sth->bindValue(':chat_id', $chat_id);
1233+
$sth->bindValue(':boost_id', $chat_boost_removed->getBoostId());
1234+
$sth->bindValue(':remove_date', self::getTimestamp($chat_boost_removed->getRemoveDate()));
1235+
$sth->bindValue(':source', $chat_boost_removed->getSource());
1236+
$sth->bindValue(':created_at', $date);
1237+
1238+
return $sth->execute();
1239+
} catch (PDOException $e) {
1240+
throw new TelegramException($e->getMessage());
1241+
}
1242+
}
1243+
11461244
/**
11471245
* Insert Message request in db
11481246
*

src/Entities/ChatBoost.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
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+
use Longman\TelegramBot\Entities\ChatBoostSource\Factory as ChatBoostSourceFactory;
15+
16+
/**
17+
* This object contains information about a chat boost.
18+
*
19+
* @link https://core.telegram.org/bots/api#chatboost
20+
*
21+
* @method string getBoostId() Unique identifier of the boost
22+
* @method int getAddDate() Point in time (Unix timestamp) when the chat was boosted
23+
* @method int getExpirationDate() Point in time (Unix timestamp) when the boost will automatically expire, unless the booster's Telegram Premium subscription is prolonged
24+
* @method ChatBoostSource getSource() Source of the added boost
25+
*/
26+
class ChatBoost extends Entity
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function subEntities(): array
32+
{
33+
return [
34+
'source' => ChatBoostSourceFactory::class,
35+
];
36+
}
37+
}

src/Entities/ChatBoostRemoved.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
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+
use Longman\TelegramBot\Entities\ChatBoostSource\Factory as ChatBoostSourceFactory;
15+
16+
/**
17+
* This object represents a boost removed from a chat.
18+
*
19+
* @link https://core.telegram.org/bots/api#chatboostremoved
20+
*
21+
* @method Chat getChat() Chat which was boosted
22+
* @method string getBoostId() Unique identifier of the boost
23+
* @method int getRemoveDate() Point in time (Unix timestamp) when the boost was removed
24+
* @method ChatBoostSource getSource() Source of the removed boost
25+
*/
26+
class ChatBoostRemoved extends Entity
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function subEntities(): array
32+
{
33+
return [
34+
'chat' => Chat::class,
35+
'source' => ChatBoostSourceFactory::class,
36+
];
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Longman\TelegramBot\Entities\ChatBoostSource;
4+
5+
use Longman\TelegramBot\Entities\User;
6+
7+
/**
8+
* Interface ChatBoostSource
9+
*
10+
* @method string getSource() Source of the boost
11+
* @method User getUser() Information about the user
12+
*/
13+
interface ChatBoostSource
14+
{
15+
//
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Longman\TelegramBot\Entities\ChatBoostSource;
4+
5+
use Longman\TelegramBot\Entities\Entity;
6+
use Longman\TelegramBot\Entities\User;
7+
8+
/**
9+
* The boost was obtained by the creation of Telegram Premium gift codes to boost a chat.
10+
*
11+
* @link https://core.telegram.org/bots/api#chatboostsourcegiftcode
12+
*
13+
* @method string getSource() Source of the boost, always “gift_code”
14+
* @method User getUser() User for which the gift code was created
15+
*/
16+
class ChatBoostSourceGiftCode extends Entity implements ChatBoostSource
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function subEntities(): array
22+
{
23+
return [
24+
'user' => User::class,
25+
];
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Longman\TelegramBot\Entities\ChatBoostSource;
4+
5+
use Longman\TelegramBot\Entities\Entity;
6+
use Longman\TelegramBot\Entities\User;
7+
8+
/**
9+
* The boost was obtained by the creation of a Telegram Premium giveaway. This boosts the chat 4 times for the duration of the corresponding Telegram Premium subscription.
10+
*
11+
* @link https://core.telegram.org/bots/api#chatboostsourcegiveaway
12+
*
13+
* @method string getSource() Source of the boost, always “giveaway”
14+
* @method int getGiveawayMessageId() Identifier of a message in the chat with the giveaway; the message could have been deleted already. May be 0 if the message isn't sent yet.
15+
* @method User getUser() Optional. User that won the prize in the giveaway if any
16+
* @method bool getIsUnclaimed() Optional. True, if the giveaway was completed, but there was no user to win the prize
17+
*/
18+
class ChatBoostSourceGiveaway extends Entity implements ChatBoostSource
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
protected function subEntities(): array
24+
{
25+
return [
26+
'user' => User::class,
27+
];
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Longman\TelegramBot\Entities\ChatBoostSource;
4+
5+
use Longman\TelegramBot\Entities\Entity;
6+
use Longman\TelegramBot\Entities\User;
7+
8+
/**
9+
* @method string getSource() Source of the boost
10+
* @method User getUser() Information about the user
11+
*/
12+
class ChatBoostSourceNotImplemented extends Entity implements ChatBoostSource
13+
{
14+
//
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Longman\TelegramBot\Entities\ChatBoostSource;
4+
5+
use Longman\TelegramBot\Entities\Entity;
6+
use Longman\TelegramBot\Entities\User;
7+
8+
/**
9+
* The boost was obtained by subscribing to Telegram Premium or by gifting a Telegram Premium subscription to another user.
10+
*
11+
* @link https://core.telegram.org/bots/api#chatboostsourcepremium
12+
*
13+
* @method string getSource() Source of the boost, always “premium”
14+
* @method User getUser() User that boosted the chat
15+
*/
16+
class ChatBoostSourcePremium extends Entity implements ChatBoostSource
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function subEntities(): array
22+
{
23+
return [
24+
'user' => User::class,
25+
];
26+
}
27+
}

0 commit comments

Comments
 (0)