Skip to content

Commit e80e551

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

File tree

14 files changed

+277
-15
lines changed

14 files changed

+277
-15
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: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,28 @@
1010

1111
namespace Longman\TelegramBot\Entities;
1212

13+
use Longman\TelegramBot\Entities\Games\CallbackGame;
1314
use Longman\TelegramBot\Exception\TelegramException;
1415

1516
/**
1617
* Class InlineKeyboardButton
1718
*
1819
* @link https://core.telegram.org/bots/api#inlinekeyboardbutton
1920
*
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.
21+
* @method string getText() Label text on the button
22+
* @method string getUrl() Optional. HTTP url to be opened when button is pressed
23+
* @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
24+
* @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.
25+
* @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.
26+
* @method CallbackGame getCallbackGame() Optional. Description of the game that will be launched when the user presses the button.
27+
* @method bool getPay() Optional. Specify True, to send a Pay button.
2628
*
2729
* @method $this setText(string $text) Label text on the button
2830
* @method $this setUrl(string $url) Optional. HTTP url to be opened when button is pressed
2931
* @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
3032
* @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.
3133
* @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.
34+
* @method $this setCallbackGame(CallbackGame $callback_game) Optional. Description of the game that will be launched when the user presses the button.
3235
* @method $this setPay(bool $pay) Optional. Specify True, to send a Pay button.
3336
*/
3437
class InlineKeyboardButton extends KeyboardButton
@@ -48,6 +51,7 @@ public static function couldBe($data)
4851
array_key_exists('callback_data', $data) ||
4952
array_key_exists('switch_inline_query', $data) ||
5053
array_key_exists('switch_inline_query_current_chat', $data) ||
54+
array_key_exists('callback_game', $data) ||
5155
array_key_exists('pay', $data)
5256
);
5357
}
@@ -63,14 +67,14 @@ protected function validate()
6367

6468
$num_params = 0;
6569

66-
foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat', 'pay'] as $param) {
70+
foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat', 'callback_game', 'pay'] as $param) {
6771
if ($this->getProperty($param, '') !== '') {
6872
$num_params++;
6973
}
7074
}
7175

7276
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!');
77+
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!');
7478
}
7579
}
7680

@@ -80,8 +84,8 @@ protected function validate()
8084
public function __call($method, $args)
8185
{
8286
// 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);
87+
if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) {
88+
unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->callback_game, $this->pay);
8589
}
8690

8791
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,

0 commit comments

Comments
 (0)