Skip to content

Commit af3a691

Browse files
committed
Implement Telegram Games platform.
1 parent d61262b commit af3a691

File tree

13 files changed

+246
-12
lines changed

13 files changed

+246
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
55

66
## [Unreleased]
77
### Added
8+
- Telegram Games platform!
89
### Changed
910
### Deprecated
1011
### Removed

src/DB.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,15 +831,15 @@ public static function insertMessageRequest(Message $message)
831831
(
832832
`id`, `user_id`, `chat_id`, `date`, `forward_from`, `forward_from_chat`, `forward_from_message_id`,
833833
`forward_date`, `reply_to_chat`, `reply_to_message`, `media_group_id`, `text`, `entities`, `audio`, `document`,
834-
`photo`, `sticker`, `video`, `voice`, `video_note`, `caption`, `contact`,
834+
`game`, `photo`, `sticker`, `video`, `voice`, `video_note`, `caption`, `contact`,
835835
`location`, `venue`, `new_chat_members`, `left_chat_member`,
836836
`new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
837837
`supergroup_chat_created`, `channel_chat_created`,
838838
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
839839
) VALUES (
840840
:message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id,
841841
:forward_date, :reply_to_chat, :reply_to_message, :media_group_id, :text, :entities, :audio, :document,
842-
:photo, :sticker, :video, :voice, :video_note, :caption, :contact,
842+
:game, :photo, :sticker, :video, :voice, :video_note, :caption, :contact,
843843
:location, :venue, :new_chat_members, :left_chat_member,
844844
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created,
845845
:supergroup_chat_created, :channel_chat_created,
@@ -883,6 +883,7 @@ public static function insertMessageRequest(Message $message)
883883
$sth->bindValue(':entities', $t = self::entitiesArrayToJson($message->getEntities(), null));
884884
$sth->bindValue(':audio', $message->getAudio());
885885
$sth->bindValue(':document', $message->getDocument());
886+
$sth->bindValue(':game', $message->getGame());
886887
$sth->bindValue(':photo', $t = self::entitiesArrayToJson($message->getPhoto(), null));
887888
$sth->bindValue(':sticker', $message->getSticker());
888889
$sth->bindValue(':video', $message->getVideo());

src/Entities/CallbackQuery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @method Message getMessage() Optional. Message with the callback button that originated the query. Note that message content and message date will not be available if the message is too old
2323
* @method string getInlineMessageId() Optional. Identifier of the message sent via the bot in inline mode, that originated the query
2424
* @method string getData() Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field
25+
* @method string getGameShortName() Optional. Short name of a Game to be returned, serves as the unique identifier for the game
2526
*/
2627
class CallbackQuery extends Entity
2728
{

src/Entities/Games/Animation.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities\Games;
12+
13+
use Longman\TelegramBot\Entities\Entity;
14+
15+
/**
16+
* Class Animation
17+
*
18+
* You can provide an animation for your game so that it looks stylish in chats (check out Lumberjack for an example). This object represents an animation file to be displayed in the message containing a game.
19+
*
20+
* @link https://core.telegram.org/bots/api#animation
21+
*
22+
* @method string getFileId() Unique file identifier
23+
* @method PhotoSize getThumb() Optional. Animation thumbnail as defined by sender
24+
* @method string getFileName() Optional. Original animation filename as defined by sender
25+
* @method string getMimeType() Optional. MIME type of the file as defined by sender
26+
* @method int getFileSize() Optional. File size
27+
**/
28+
class Animation extends Entity
29+
{
30+
31+
}

src/Entities/Games/CallbackGame.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities\Games;
12+
13+
use Longman\TelegramBot\Entities\Entity;
14+
15+
/**
16+
* Class CallbackGame
17+
*
18+
* A placeholder, currently holds no information. Use BotFather to set up your game.
19+
*
20+
* @link https://core.telegram.org/bots/api#callbackgame
21+
**/
22+
class CallbackGame extends Entity
23+
{
24+
25+
}

src/Entities/Games/Game.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities\Games;
12+
13+
use Longman\TelegramBot\Entities\Entity;
14+
use Longman\TelegramBot\Entities\MessageEntity;
15+
16+
/**
17+
* Class Game
18+
*
19+
* This object represents a game. Use BotFather to create and edit games, their short names will act as unique identifiers.
20+
*
21+
* @link https://core.telegram.org/bots/api#game
22+
*
23+
* @method string getTitle() Title of the game
24+
* @method string getDescription() Description of the game
25+
* @method string getText() Optional. Brief description of the game or high scores included in the game message. Can be automatically edited to include current high scores for the game when the bot calls setGameScore, or manually edited using editMessageText. 0-4096 characters.
26+
* @method Animation getAnimation() Optional. Animation that will be displayed in the game message in chats. Upload via BotFather
27+
**/
28+
class Game extends Entity
29+
{
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function subEntities()
34+
{
35+
return [
36+
'photo' => PhotoSize::class,
37+
'text_entities' => MessageEntity::class,
38+
'animation' => Animation::class,
39+
];
40+
}
41+
42+
/**
43+
* Photo that will be displayed in the game message in chats.
44+
*
45+
* This method overrides the default getPhoto method
46+
* and returns a nice array of PhotoSize objects.
47+
*
48+
* @return null|PhotoSize[]
49+
*/
50+
public function getPhoto()
51+
{
52+
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo');
53+
54+
return empty($pretty_array) ? null : $pretty_array;
55+
}
56+
57+
/**
58+
* Optional. Special entities that appear in text, such as usernames, URLs, bot commands, etc.
59+
*
60+
* This method overrides the default getTextEntities method
61+
* and returns a nice array of MessageEntity objects.
62+
*
63+
* @return null|MessageEntity[]
64+
*/
65+
public function getTextEntities()
66+
{
67+
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'text_entities');
68+
69+
return empty($pretty_array) ? null : $pretty_array;
70+
}
71+
}

src/Entities/Games/GameHighScore.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities\Games;
12+
13+
use Longman\TelegramBot\Entities\Entity;
14+
15+
/**
16+
* Class GameHighScore
17+
*
18+
* This object represents one row of the high scores table for a game.
19+
*
20+
* @link https://core.telegram.org/bots/api#gamehighscore
21+
*
22+
* @method int getPosition() Position in high score table for the game
23+
* @method User getUser() User
24+
* @method int getScore() Score
25+
**/
26+
class GameHighScore extends Entity
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function subEntities()
32+
{
33+
return [
34+
'user' => User::class,
35+
];
36+
}
37+
}

src/Entities/InlineKeyboardButton.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@
1717
*
1818
* @link https://core.telegram.org/bots/api#inlinekeyboardbutton
1919
*
20-
* @method string getText() Label text on the button
21-
* @method string getUrl() Optional. HTTP url to be opened when button is pressed
22-
* @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
23-
* @method string getSwitchInlineQuery() Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted.
24-
* @method string getSwitchInlineQueryCurrentChat() Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted.
25-
* @method bool getPay() Optional. Specify True, to send a Pay button.
20+
* @method string getText() Label text on the button
21+
* @method string getUrl() Optional. HTTP url to be opened when button is pressed
22+
* @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
23+
* @method string getSwitchInlineQuery() Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted.
24+
* @method string getSwitchInlineQueryCurrentChat() Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted.
25+
* @method CallbackGame getCallbackGame() Optional. Description of the game that will be launched when the user presses the button.
26+
* @method bool getPay() Optional. Specify True, to send a Pay button.
2627
*
2728
* @method $this setText(string $text) Label text on the button
2829
* @method $this setUrl(string $url) Optional. HTTP url to be opened when button is pressed
2930
* @method $this setCallbackData(string $callback_data) Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
3031
* @method $this setSwitchInlineQuery(string $switch_inline_query) Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted.
3132
* @method $this setSwitchInlineQueryCurrentChat(string $switch_inline_query_current_chat) Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted.
33+
* @method $this setCallbackGame(CallbackGame $callback_game) Optional. Description of the game that will be launched when the user presses the button.
3234
* @method $this setPay(bool $pay) Optional. Specify True, to send a Pay button.
3335
*/
3436
class InlineKeyboardButton extends KeyboardButton
@@ -48,6 +50,7 @@ public static function couldBe($data)
4850
array_key_exists('callback_data', $data) ||
4951
array_key_exists('switch_inline_query', $data) ||
5052
array_key_exists('switch_inline_query_current_chat', $data) ||
53+
array_key_exists('callback_game', $data) ||
5154
array_key_exists('pay', $data)
5255
);
5356
}
@@ -63,14 +66,14 @@ protected function validate()
6366

6467
$num_params = 0;
6568

66-
foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat', 'pay'] as $param) {
69+
foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat', 'callback_game', 'pay'] as $param) {
6770
if ($this->getProperty($param, '') !== '') {
6871
$num_params++;
6972
}
7073
}
7174

7275
if ($num_params !== 1) {
73-
throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, pay!');
76+
throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay!');
7477
}
7578
}
7679

@@ -80,8 +83,8 @@ protected function validate()
8083
public function __call($method, $args)
8184
{
8285
// Only 1 of these can be set, so clear the others when setting a new one.
83-
if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setPay'], true)) {
84-
unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->pay);
86+
if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) {
87+
unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->callback_game, $this->pay);
8588
}
8689

8790
return parent::__call($method, $args);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities\InlineQuery;
12+
13+
use Longman\TelegramBot\Entities\InlineKeyboard;
14+
15+
/**
16+
* Class InlineQueryResultGame
17+
*
18+
* @link https://core.telegram.org/bots/api#inlinequeryresultgame
19+
*
20+
* <code>
21+
* $data = [
22+
* 'id' => '',
23+
* 'game_short_name' => '',
24+
* 'reply_markup' => <InlineKeyboard>,
25+
* ];
26+
* </code>
27+
*
28+
* @method string getType() Type of the result, must be game
29+
* @method string getId() Unique identifier for this result, 1-64 bytes
30+
* @method string getGameShortName() Short name of the game
31+
* @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message
32+
*
33+
* @method $this setId(string $id) Unique identifier for this result, 1-64 bytes
34+
* @method $this setGameShortName(string $game_short_name) Short name of the game
35+
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
36+
*/
37+
class InlineQueryResultGame extends InlineEntity implements InlineQueryResult
38+
{
39+
/**
40+
* InlineQueryResultGame constructor
41+
*
42+
* @param array $data
43+
*
44+
* @throws \Longman\TelegramBot\Exception\TelegramException
45+
*/
46+
public function __construct(array $data = [])
47+
{
48+
$data['type'] = 'game';
49+
parent::__construct($data);
50+
}
51+
}

src/Entities/Message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Longman\TelegramBot\Entities;
1212

13+
use Longman\TelegramBot\Entities\Games\Game;
1314
use Longman\TelegramBot\Entities\Payments\Invoice;
1415
use Longman\TelegramBot\Entities\Payments\SuccessfulPayment;
1516

@@ -33,6 +34,7 @@
3334
* @method string getAuthorSignature() Optional. Signature of the post author for messages in channels
3435
* @method Audio getAudio() Optional. Message is an audio file, information about the file
3536
* @method Document getDocument() Optional. Message is a general file, information about the file
37+
* @method Game getGame() Optional. Message is a game, information about the game.
3638
* @method Sticker getSticker() Optional. Message is a sticker, information about the sticker
3739
* @method Video getVideo() Optional. Message is a video, information about the video
3840
* @method Voice getVoice() Optional. Message is a voice message, information about the file
@@ -70,6 +72,7 @@ protected function subEntities()
7072
'caption_entities' => MessageEntity::class,
7173
'audio' => Audio::class,
7274
'document' => Document::class,
75+
'game' => Game::class,
7376
'photo' => PhotoSize::class,
7477
'sticker' => Sticker::class,
7578
'video' => Video::class,

src/Request.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
* @method static ServerResponse sendInvoice(array $data) Use this method to send invoices. On success, the sent Message is returned.
7575
* @method static ServerResponse answerShippingQuery(array $data) If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API will send an Update with a shipping_query field to the bot. Use this method to reply to shipping queries. On success, True is returned.
7676
* @method static ServerResponse answerPreCheckoutQuery(array $data) Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field pre_checkout_query. Use this method to respond to such pre-checkout queries. On success, True is returned.
77+
* @method static ServerResponse sendGame(array $data) Use this method to send a game. On success, the sent Message is returned.
78+
* @method static ServerResponse setGameScore(array $data) Use this method to set the score of the specified user in a game. On success, if the message was sent by the bot, returns the edited Message, otherwise returns True. Returns an error, if the new score is not greater than the user's current score in the chat and force is False.
79+
* @method static ServerResponse getGameHighScores(array $data) Use this method to get data for high score tables. Will return the score of the specified user and several of his neighbors in a game. On success, returns an Array of GameHighScore objects.
7780
*/
7881
class Request
7982
{
@@ -185,6 +188,9 @@ class Request
185188
'sendInvoice',
186189
'answerShippingQuery',
187190
'answerPreCheckoutQuery',
191+
'sendGame',
192+
'setGameScore',
193+
'getGameHighScores',
188194
];
189195

190196
/**
@@ -662,6 +668,8 @@ private static function limitTelegramRequests($action, array $data = [])
662668
'sendVenue',
663669
'sendContact',
664670
'sendInvoice',
671+
'sendGame',
672+
'setGameScore',
665673
'editMessageText',
666674
'editMessageCaption',
667675
'editMessageReplyMarkup',

structure.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ CREATE TABLE IF NOT EXISTS `message` (
8181
`entities` TEXT COMMENT 'For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text',
8282
`audio` TEXT COMMENT 'Audio object. Message is an audio file, information about the file',
8383
`document` TEXT COMMENT 'Document object. Message is a general file, information about the file',
84+
`game` TEXT COMMENT 'Game object. Message is a game, information about the game',
8485
`photo` TEXT COMMENT 'Array of PhotoSize objects. Message is a photo, available sizes of the photo',
8586
`sticker` TEXT COMMENT 'Sticker object. Message is a sticker, information about the sticker',
8687
`video` TEXT COMMENT 'Video object. Message is a video, information about the video',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `message` ADD COLUMN `game` TEXT COMMENT 'Game object. Message is a game, information about the game' AFTER `document`;

0 commit comments

Comments
 (0)