Skip to content

Commit

Permalink
Add answerShippingQuery, answerPreCheckoutQuery and `ShippingOpti…
Browse files Browse the repository at this point in the history
…on` (#83)
  • Loading branch information
vjik authored Jun 29, 2024
1 parent ef17720 commit e2128fc
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- New #80: Add `answerWebAppQuery` method and `SentWebAppMessage` type.
- New #81: Add `sendInvoice` method.
- New #82: Add `createInvoiceLink` method.
- New #83: Add `answerShippingQuery` and `answerPreCheckoutQuery` methods, and `ShippingOption` type.
- 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`.
Expand Down
50 changes: 50 additions & 0 deletions src/Method/Payment/AnswerPreCheckoutQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method\Payment;

use Vjik\TelegramBot\Api\ParseResult\ValueHelper;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;

/**
* @see https://core.telegram.org/bots/api#answerprecheckoutquery
*/
final readonly class AnswerPreCheckoutQuery implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private string $preCheckoutQueryId,
private bool $ok,
private ?string $errorMessage = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'pre_checkout_query_id' => $this->preCheckoutQueryId,
'ok' => $this->ok,
'error_message' => $this->errorMessage,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
59 changes: 59 additions & 0 deletions src/Method/Payment/AnswerShippingQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method\Payment;

use Vjik\TelegramBot\Api\ParseResult\ValueHelper;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\Payment\ShippingOption;

/**
* @see https://core.telegram.org/bots/api#answershippingquery
*/
final readonly class AnswerShippingQuery implements TelegramRequestWithResultPreparingInterface
{
/**
* @param ShippingOption[]|null $shippingOptions
*/
public function __construct(
private string $shippingQueryId,
private bool $ok,
private ?array $shippingOptions = null,
private ?string $errorMessage = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'shipping_query_id' => $this->shippingQueryId,
'ok' => $this->ok,
'shipping_options' => $this->shippingOptions === null ? null : array_map(
static fn(ShippingOption $option) => $option->toRequestArray(),
$this->shippingOptions,
),
'error_message' => $this->errorMessage,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
36 changes: 36 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
use Vjik\TelegramBot\Api\Method\Inline\AnswerWebAppQuery;
use Vjik\TelegramBot\Api\Method\LeaveChat;
use Vjik\TelegramBot\Api\Method\LogOut;
use Vjik\TelegramBot\Api\Method\Payment\AnswerPreCheckoutQuery;
use Vjik\TelegramBot\Api\Method\Payment\AnswerShippingQuery;
use Vjik\TelegramBot\Api\Method\Payment\CreateInvoiceLink;
use Vjik\TelegramBot\Api\Method\Payment\GetStarTransactions;
use Vjik\TelegramBot\Api\Method\Payment\SendInvoice;
Expand Down Expand Up @@ -154,6 +156,7 @@
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\MessageId;
use Vjik\TelegramBot\Api\Type\Payment\LabeledPrice;
use Vjik\TelegramBot\Api\Type\Payment\ShippingOption;
use Vjik\TelegramBot\Api\Type\Payment\StarTransactions;
use Vjik\TelegramBot\Api\Type\Poll;
use Vjik\TelegramBot\Api\Type\ReactionType;
Expand Down Expand Up @@ -264,6 +267,39 @@ public function answerInlineQuery(
);
}

/**
* @see https://core.telegram.org/bots/api#answerprecheckoutquery
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function answerPreCheckoutQuery(
string $preCheckoutQueryId,
bool $ok,
?string $errorMessage = null,
): FailResult|true {
return $this->send(
new AnswerPreCheckoutQuery($preCheckoutQueryId, $ok, $errorMessage)
);
}

/**
* @see https://core.telegram.org/bots/api#answershippingquery
*
* @param ShippingOption[]|null $shippingOptions
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function answerShippingQuery(
string $shippingQueryId,
bool $ok,
?array $shippingOptions = null,
?string $errorMessage = null,
): FailResult|true {
return $this->send(
new AnswerShippingQuery($shippingQueryId, $ok, $shippingOptions, $errorMessage)
);
}

/**
* @see https://core.telegram.org/bots/api#answerwebappquery
*
Expand Down
33 changes: 33 additions & 0 deletions src/Type/Payment/ShippingOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type\Payment;

/**
* @see https://core.telegram.org/bots/api#shippingoption
*/
final readonly class ShippingOption
{
/**
* @param LabeledPrice[] $prices
*/
public function __construct(
public string $id,
public string $title,
public array $prices
) {
}

public function toRequestArray(): array
{
return [
'id' => $this->id,
'title' => $this->title,
'prices' => array_map(
static fn(LabeledPrice $price) => $price->toRequestArray(),
$this->prices
),
];
}
}
62 changes: 62 additions & 0 deletions tests/Method/Payment/AnswerPreCheckoutQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method\Payment;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Payment\AnswerPreCheckoutQuery;
use Vjik\TelegramBot\Api\Request\HttpMethod;

final class AnswerPreCheckoutQueryTest extends TestCase
{
public function testBase(): void
{
$method = new AnswerPreCheckoutQuery(
'qid',
true,
);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('answerPreCheckoutQuery', $method->getApiMethod());
$this->assertSame(
[
'pre_checkout_query_id' => 'qid',
'ok' => true,
],
$method->getData(),
);
}

public function testFull(): void
{
$method = new AnswerPreCheckoutQuery(
'qid',
true,
'error message',
);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('answerPreCheckoutQuery', $method->getApiMethod());
$this->assertSame(
[
'pre_checkout_query_id' => 'qid',
'ok' => true,
'error_message' => 'error message',
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new AnswerPreCheckoutQuery(
'qid',
true,
);

$preparedResult = $method->prepareResult(true);

$this->assertTrue($preparedResult);
}
}
66 changes: 66 additions & 0 deletions tests/Method/Payment/AnswerShippingQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method\Payment;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Payment\AnswerShippingQuery;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\Payment\ShippingOption;

final class AnswerShippingQueryTest extends TestCase
{
public function testBase(): void
{
$method = new AnswerShippingQuery(
'qid',
true,
);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('answerShippingQuery', $method->getApiMethod());
$this->assertSame(
[
'shipping_query_id' => 'qid',
'ok' => true,
],
$method->getData(),
);
}

public function testFull(): void
{
$option = new ShippingOption('', '', []);
$method = new AnswerShippingQuery(
'qid',
true,
[$option],
'error message',
);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('answerShippingQuery', $method->getApiMethod());
$this->assertSame(
[
'shipping_query_id' => 'qid',
'ok' => true,
'shipping_options' => [$option->toRequestArray()],
'error_message' => 'error message',
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new AnswerShippingQuery(
'qid',
true,
);

$preparedResult = $method->prepareResult(true);

$this->assertTrue($preparedResult);
}
}
24 changes: 24 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ public function testAnswerInlineQuery(): void
$this->assertTrue($result);
}

public function testAnswerPreCheckoutQuery(): void
{
$api = $this->createApi([
'ok' => true,
'result' => true,
]);

$result = $api->answerPreCheckoutQuery('id', true);

$this->assertTrue($result);
}

public function testAnswerShippingQuery(): void
{
$api = $this->createApi([
'ok' => true,
'result' => true,
]);

$result = $api->answerShippingQuery('id', true);

$this->assertTrue($result);
}

public function testAnswerWebAppQuery(): void
{
$api = $this->createApi([
Expand Down
Loading

0 comments on commit e2128fc

Please sign in to comment.