Skip to content

Commit

Permalink
Telegram Bot API 8.2 support (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jan 2, 2025
1 parent d998207 commit 67527f8
Show file tree
Hide file tree
Showing 18 changed files with 467 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Telegram Bot API for PHP Change Log

## 0.5.0 January 02, 2025

- New #129: Add `verifyUser`, `verifyChat`, `removeUserVerification` and `removeChatVerification` methods.
- New #129: Add `upgradeStarCount` field to `Gift` type.
- New #129: Add `payForUpgrade` parameter to `SendGift` method.
- Chg #129: Remove `hideUrl` field from `InlineQueryResultArticle` type.

## 0.4.7.1 December 18, 2024

- Bug #128: Add a check for a seekable response body and rewind in `PsrTelegramClient`.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

The package provides a simple and convenient way to interact with the Telegram Bot API.

✔️ Telegram Bot API 8.1 (December 4, 2024) is **fully supported**.
✔️ Telegram Bot API 8.2 (January 1, 2025) is **fully supported**.

## Requirements

Expand Down
43 changes: 43 additions & 0 deletions src/Method/RemoveChatVerification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\TrueValue;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;

/**
* @see https://core.telegram.org/bots/api#removechatverification
*
* @template-implements TelegramRequestWithResultPreparingInterface<TrueValue>
*/
final readonly class RemoveChatVerification implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
) {}

public function getHttpMethod(): HttpMethod
{
return HttpMethod::POST;
}

public function getApiMethod(): string
{
return 'removeChatVerification';
}

public function getData(): array
{
return [
'chat_id' => $this->chatId,
];
}

public function getResultType(): TrueValue
{
return new TrueValue();
}
}
43 changes: 43 additions & 0 deletions src/Method/RemoveUserVerification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\TrueValue;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;

/**
* @see https://core.telegram.org/bots/api#removeuserverification
*
* @template-implements TelegramRequestWithResultPreparingInterface<TrueValue>
*/
final readonly class RemoveUserVerification implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int $userId,
) {}

public function getHttpMethod(): HttpMethod
{
return HttpMethod::POST;
}

public function getApiMethod(): string
{
return 'removeUserVerification';
}

public function getData(): array
{
return [
'user_id' => $this->userId,
];
}

public function getResultType(): TrueValue
{
return new TrueValue();
}
}
2 changes: 2 additions & 0 deletions src/Method/Sticker/SendGift.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
private ?string $text = null,
private ?string $textParseMode = null,
private ?array $textEntities = null,
private ?bool $payForUpgrade = null,
) {}

public function getHttpMethod(): HttpMethod
Expand All @@ -43,6 +44,7 @@ public function getData(): array
[
'user_id' => $this->userId,
'gift_id' => $this->giftId,
'pay_for_upgrade' => $this->payForUpgrade,
'text' => $this->text,
'text_parse_mode' => $this->textParseMode,
'text_entities' => $this->textEntities === null
Expand Down
48 changes: 48 additions & 0 deletions src/Method/VerifyChat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\TrueValue;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;

/**
* @see https://core.telegram.org/bots/api#verifychat
*
* @template-implements TelegramRequestWithResultPreparingInterface<TrueValue>
*/
final readonly class VerifyChat implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
private ?string $customDescription = null,
) {}

public function getHttpMethod(): HttpMethod
{
return HttpMethod::POST;
}

public function getApiMethod(): string
{
return 'verifyChat';
}

public function getData(): array
{
return array_filter(
[
'chat_id' => $this->chatId,
'custom_description' => $this->customDescription,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function getResultType(): TrueValue
{
return new TrueValue();
}
}
48 changes: 48 additions & 0 deletions src/Method/VerifyUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\TrueValue;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;

/**
* @see https://core.telegram.org/bots/api#verifyuser
*
* @template-implements TelegramRequestWithResultPreparingInterface<TrueValue>
*/
final readonly class VerifyUser implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int $userId,
private ?string $customDescription = null,
) {}

public function getHttpMethod(): HttpMethod
{
return HttpMethod::POST;
}

public function getApiMethod(): string
{
return 'verifyUser';
}

public function getData(): array
{
return array_filter(
[
'user_id' => $this->userId,
'custom_description' => $this->customDescription,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function getResultType(): TrueValue
{
return new TrueValue();
}
}
46 changes: 46 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
use Vjik\TelegramBot\Api\Method\Payment\SendInvoice;
use Vjik\TelegramBot\Api\Method\PinChatMessage;
use Vjik\TelegramBot\Api\Method\PromoteChatMember;
use Vjik\TelegramBot\Api\Method\RemoveChatVerification;
use Vjik\TelegramBot\Api\Method\RemoveUserVerification;
use Vjik\TelegramBot\Api\Method\ReopenForumTopic;
use Vjik\TelegramBot\Api\Method\ReopenGeneralForumTopic;
use Vjik\TelegramBot\Api\Method\RestrictChatMember;
Expand Down Expand Up @@ -136,6 +138,8 @@
use Vjik\TelegramBot\Api\Method\UpdatingMessage\EditMessageText;
use Vjik\TelegramBot\Api\Method\UpdatingMessage\StopMessageLiveLocation;
use Vjik\TelegramBot\Api\Method\UpdatingMessage\StopPoll;
use Vjik\TelegramBot\Api\Method\VerifyChat;
use Vjik\TelegramBot\Api\Method\VerifyUser;
use Vjik\TelegramBot\Api\ParseResult\ResultFactory;
use Vjik\TelegramBot\Api\ParseResult\TelegramParseResultException;
use Vjik\TelegramBot\Api\ParseResult\ValueProcessor\ValueProcessorInterface;
Expand Down Expand Up @@ -1303,6 +1307,26 @@ public function refundStarPayment(int $userId, string $telegramPaymentChargeId):
);
}

/**
* @see https://core.telegram.org/bots/api#removechatverification
*/
public function removeChatVerification(int|string $chatId): FailResult|true
{
return $this->send(
new RemoveChatVerification($chatId),
);
}

/**
* @see https://core.telegram.org/bots/api#removeuserverification
*/
public function removeUserVerification(int $userId): FailResult|true
{
return $this->send(
new RemoveUserVerification($userId),
);
}

/**
* @see https://core.telegram.org/bots/api#reopenforumtopic
*/
Expand Down Expand Up @@ -1655,6 +1679,7 @@ public function sendGift(
?string $text = null,
?string $textParseMode = null,
?array $textEntities = null,
?bool $payForUpgrade = null,
): FailResult|true {
return $this->send(
new SendGift(
Expand All @@ -1663,6 +1688,7 @@ public function sendGift(
$text,
$textParseMode,
$textEntities,
$payForUpgrade,
),
);
}
Expand Down Expand Up @@ -2607,6 +2633,26 @@ public function uploadStickerFile(int $userId, InputFile $sticker, string $stick
);
}

/**
* @see https://core.telegram.org/bots/api#verifychat
*/
public function verifyChat(int|string $chatId, ?string $customDescription = null): FailResult|true
{
return $this->send(
new VerifyChat($chatId, $customDescription),
);
}

/**
* @see https://core.telegram.org/bots/api#verifyuser
*/
public function verifyUser(int $userId, ?string $customDescription = null): FailResult|true
{
return $this->send(
new VerifyUser($userId, $customDescription),
);
}

/**
* @psalm-template TClass as object
* @psalm-template TValue as mixed
Expand Down
2 changes: 0 additions & 2 deletions src/Type/Inline/InlineQueryResultArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function __construct(
public InputMessageContent $inputMessageContent,
public ?InlineKeyboardMarkup $replyMarkup = null,
public ?string $url = null,
public ?bool $hideUrl = null,
public ?string $description = null,
public ?string $thumbnailUrl = null,
public ?int $thumbnailWidth = null,
Expand All @@ -39,7 +38,6 @@ public function toRequestArray(): array
'input_message_content' => $this->inputMessageContent->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
'url' => $this->url,
'hide_url' => $this->hideUrl,
'description' => $this->description,
'thumbnail_url' => $this->thumbnailUrl,
'thumbnail_width' => $this->thumbnailWidth,
Expand Down
1 change: 1 addition & 0 deletions src/Type/Sticker/Gift.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public function __construct(
public int $starCount,
public ?int $totalCount = null,
public ?int $remainingCount = null,
public ?int $upgradeStarCount = null,
) {}
}
36 changes: 36 additions & 0 deletions tests/Method/RemoveChatVerificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\RemoveChatVerification;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Tests\Support\TestHelper;

final class RemoveChatVerificationTest extends TestCase
{
public function testBase(): void
{
$method = new RemoveChatVerification(7);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('removeChatVerification', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 7,
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new RemoveChatVerification('id5');

$preparedResult = TestHelper::createSuccessStubApi(true)->send($method);

$this->assertTrue($preparedResult);
}
}
Loading

0 comments on commit 67527f8

Please sign in to comment.