Skip to content

Commit

Permalink
Last bits of missing methods and types
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Jan 7, 2017
1 parent 6d4421b commit f1f0841
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 17 deletions.
65 changes: 65 additions & 0 deletions src/Telegram/Methods/GetGameScore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\Exceptions\InvalidResultType;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\GameHighScoreArray;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;
use unreal4u\TelegramAPI\Telegram\Types\Message;

/**
* 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.
*
* This method will currently return scores for the target user, plus two of his closest neighbors on each side. Will
* also return the top three users if the user and his neighbors are not among them. Please note that this behavior is
* subject to change.
*
* Objects defined as-is January 2017
*
* @see https://core.telegram.org/bots/api#getgamescore
*/
class GetGameScore extends TelegramMethods
{
/**
* Target user id
* @var int
*/
public $user_id = 0;

/**
* Required if inline_message_id is not specified. Unique identifier for the target chat
* @var int
*/
public $chat_id;

/**
* Required if inline_message_id is not specified. Identifier of the sent message
* @var int
*/
public $message_id;

/**
* Required if chat_id and message_id are not specified. Identifier of the inline message
* @var string
*/
public $inline_message_id;

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new GameHighScoreArray($data->getResult(), $logger);
}

public function getMandatoryFields(): array
{
// user_id and score are always mandatory
$returnValue[] = 'user_id';
return $this->mandatoryUserOrInlineMessageId($returnValue);
}
}
21 changes: 5 additions & 16 deletions src/Telegram/Methods/SetGameScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@
use unreal4u\TelegramAPI\Telegram\Types\Message;

/**
* Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update
* for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of
* an unsuccessful request, we will give up after a reasonable amount of attempts. Returns true.
* 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
*
* If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL,
* e.g. https://www.example.com/<token>. Since nobody else knows your bot‘s token, you can be pretty sure it’s us.
* Objects defined as-is January 2017
*
* Notes
* <ul>
* <li>You will not be able to receive updates using getUpdates for as long as an outgoing webhook is set up.</li>
* <li>To use a self-signed certificate, you need to upload your public key certificate using certificate parameter.
* Please upload as InputFile, sending a String will not work.</li>
* <li>Ports currently supported for Webhooks: 443, 80, 88, 8443.</li>
* </ul>
*
* Objects defined as-is December 2016
*
* @see https://core.telegram.org/bots/api#setwebhook
* @see https://core.telegram.org/bots/api#setgamescore
*/
class SetGameScore extends TelegramMethods
{
Expand Down
40 changes: 40 additions & 0 deletions src/Telegram/Types/Custom/GameHighScoreArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Custom;

use unreal4u\TelegramAPI\Abstracts\CustomType;
use unreal4u\TelegramAPI\Telegram\Types\ChatMember;
use unreal4u\TelegramAPI\Interfaces\CustomArrayType;
use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Telegram\Types\GameHighScore;

/**
* Mockup class to generate a real telegram GameHighScore representation
*/
class GameHighScoreArray extends CustomType implements CustomArrayType
{
public $data = [];

public function __construct(array $data = null, LoggerInterface $logger = null)
{
if (!empty($data)) {
foreach ($data as $id => $gameHighScore) {
$this->data[$id] = new GameHighScore($gameHighScore, $logger);
}
}
}

/**
* Traverses through our $data, yielding the result set
*
* @return ChatMember[]
*/
public function traverseObject()
{
foreach ($this->data as $chatMember) {
yield $chatMember;
}
}
}
47 changes: 47 additions & 0 deletions src/Telegram/Types/GameHighScore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types;

use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\Telegram\Types\Custom\MessageEntityArray;
use unreal4u\TelegramAPI\Telegram\Types\Custom\PhotoSizeArray;

/**
* This object represents one row of the high scores table for a game
*
* Objects defined as-is January 2017
*
* @see https://core.telegram.org/bots/api#gamehighscore
*/
class GameHighScore extends TelegramTypes
{
/**
* Position in high score table for the game
* @var string
*/
public $position = 0;

/**
* User
* @var string
*/
public $user;

/**
* Score
* @var PhotoSize[]
*/
public $score = 0;

public function mapSubObjects(string $key, array $data): TelegramTypes
{
switch ($key) {
case 'user':
return new User($data, $this->logger);
}

return parent::mapSubObjects($key, $data);
}
}
2 changes: 1 addition & 1 deletion src/Telegram/Types/Inline/Query/Result/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class Game extends Result
{
/**
* Type of the result, must be gif
* Type of the result, must be game
* @var string
*/
public $type = 'game';
Expand Down

0 comments on commit f1f0841

Please sign in to comment.