diff --git a/CHANGELOG.md b/CHANGELOG.md index 271aa2d..31d7373 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,7 @@ - New #84: Add `refundStarPayment` method. - New #85: Add `setPassportDataErrors` method and `PassportElementError*` types. - New #86: Add `sendGame` method. +- New #87: Add `setGameScore` method. - Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to `Vjik\TelegramBot\Api\Type\Update` namespace. - Chg #30: Remove `TelegramRequestWithFilesInterface`. diff --git a/src/Method/Game/SetGameScore.php b/src/Method/Game/SetGameScore.php new file mode 100644 index 0000000..ebae5dd --- /dev/null +++ b/src/Method/Game/SetGameScore.php @@ -0,0 +1,59 @@ + $this->userId, + 'score' => $this->score, + 'force' => $this->force, + 'disable_edit_message' => $this->disableEditMessage, + 'chat_id' => $this->chatId, + 'message_id' => $this->messageId, + 'inline_message_id' => $this->inlineMessageId, + ], + static fn(mixed $value): bool => $value !== null, + ); + } + + public function prepareResult(mixed $result): Message|true + { + return $result === true + ? $result + : Message::fromTelegramResult($result); + } +} diff --git a/src/TelegramBotApi.php b/src/TelegramBotApi.php index f917edf..ea14d45 100644 --- a/src/TelegramBotApi.php +++ b/src/TelegramBotApi.php @@ -31,6 +31,7 @@ use Vjik\TelegramBot\Api\Method\ForwardMessage; use Vjik\TelegramBot\Api\Method\ForwardMessages; use Vjik\TelegramBot\Api\Method\Game\SendGame; +use Vjik\TelegramBot\Api\Method\Game\SetGameScore; use Vjik\TelegramBot\Api\Method\GetBusinessConnection; use Vjik\TelegramBot\Api\Method\GetChat; use Vjik\TelegramBot\Api\Method\GetChatAdministrators; @@ -2202,6 +2203,33 @@ public function setCustomEmojiStickerSetThumbnail(string $name, ?string $customE return $this->send(new SetCustomEmojiStickerSetThumbnail($name, $customEmojiId)); } + /** + * @see https://core.telegram.org/bots/api#setgamescore + * + * @psalm-suppress MixedInferredReturnType,MixedReturnStatement + */ + public function setGameScore( + int $userId, + int $score, + ?bool $force = null, + ?bool $disableEditMessage = null, + ?int $chatId = null, + ?int $messageId = null, + ?string $inlineMessageId = null, + ): FailResult|Message|true { + return $this->send( + new SetGameScore( + $userId, + $score, + $force, + $disableEditMessage, + $chatId, + $messageId, + $inlineMessageId + ) + ); + } + /** * @see https://core.telegram.org/bots/api#setmessagereaction * diff --git a/tests/Method/Game/SetGameScoreTest.php b/tests/Method/Game/SetGameScoreTest.php new file mode 100644 index 0000000..e693265 --- /dev/null +++ b/tests/Method/Game/SetGameScoreTest.php @@ -0,0 +1,73 @@ +assertSame(HttpMethod::POST, $method->getHttpMethod()); + $this->assertSame('setGameScore', $method->getApiMethod()); + $this->assertSame( + [ + 'user_id' => 1, + 'score' => 2, + ], + $method->getData(), + ); + } + + public function testFull(): void + { + $method = new SetGameScore( + 1, + 2, + true, + false, + 10, + 20, + 'id1', + ); + + $this->assertSame( + [ + 'user_id' => 1, + 'score' => 2, + 'force' => true, + 'disable_edit_message' => false, + 'chat_id' => 10, + 'message_id' => 20, + 'inline_message_id' => 'id1', + ], + $method->getData() + ); + } + + public function testPrepareResult(): void + { + $method = new SetGameScore(1, 2); + + $preparedResult = $method->prepareResult(true); + $this->assertTrue($preparedResult); + + $preparedResult = $method->prepareResult([ + 'message_id' => 7, + 'date' => 1620000000, + 'chat' => [ + 'id' => 1, + 'type' => 'private', + ], + ]); + $this->assertInstanceOf(Message::class, $preparedResult); + $this->assertSame(7, $preparedResult->messageId); + } +} diff --git a/tests/Method/UpdatingMessage/EditMessageMediaTest.php b/tests/Method/UpdatingMessage/EditMessageMediaTest.php index 7d7a1b5..51effad 100644 --- a/tests/Method/UpdatingMessage/EditMessageMediaTest.php +++ b/tests/Method/UpdatingMessage/EditMessageMediaTest.php @@ -33,7 +33,7 @@ public function testBase(): void ); } - public function testFull1(): void + public function testFull(): void { $file = new InputFile((new StreamFactory())->createStream()); $media = new InputMediaPhoto($file); diff --git a/tests/TelegramBotApiTest.php b/tests/TelegramBotApiTest.php index 780d795..f3107e7 100644 --- a/tests/TelegramBotApiTest.php +++ b/tests/TelegramBotApiTest.php @@ -1678,6 +1678,18 @@ public function testSetCustomEmojiStickerSetThumbnail(): void $this->assertTrue($result); } + public function testSetGameScore(): void + { + $api = $this->createApi([ + 'ok' => true, + 'result' => true, + ]); + + $result = $api->setGameScore(1, 2); + + $this->assertTrue($result); + } + public function testSetMessageReaction(): void { $api = $this->createApi([