Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add answerInlineQuery method and needed types #79

Merged
merged 8 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
- New #76: Add `editMessageReplyMarkup` method.
- New #77: Add `stopPoll` method.
- New #78: Add `deleteMessage` and `deleteMessages` methods.
- New #79: Add `answerInlineQuery` method, `InlineQueryResultsButton`, `InlineQueryResult*`, `InputMessageContent*` and
`LabeledPrice` types.
- 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
64 changes: 64 additions & 0 deletions src/Method/Inline/AnswerInlineQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method\Inline;

use Vjik\TelegramBot\Api\ParseResult\ValueHelper;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\Inline\InlineQueryResult;
use Vjik\TelegramBot\Api\Type\Inline\InlineQueryResultsButton;

/**
* @see https://core.telegram.org/bots/api#answerinlinequery
*/
final readonly class AnswerInlineQuery implements TelegramRequestWithResultPreparingInterface
{
/**
* @param InlineQueryResult[] $results
*/
public function __construct(
private string $inlineQueryId,
private array $results,
private ?int $cacheTime = null,
private ?bool $isPersonal = null,
private ?string $nextOffset = null,
private ?InlineQueryResultsButton $button = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'inline_query_id' => $this->inlineQueryId,
'results' => array_map(
static fn(InlineQueryResult $result) => $result->toRequestArray(),
$this->results,
),
'cache_time' => $this->cacheTime,
'is_personal' => $this->isPersonal,
'next_offset' => $this->nextOffset,
'button' => $this->button?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
23 changes: 23 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Vjik\TelegramBot\Api\Method\GetUserChatBoosts;
use Vjik\TelegramBot\Api\Method\GetUserProfilePhotos;
use Vjik\TelegramBot\Api\Method\HideGeneralForumTopic;
use Vjik\TelegramBot\Api\Method\Inline\AnswerInlineQuery;
use Vjik\TelegramBot\Api\Method\LeaveChat;
use Vjik\TelegramBot\Api\Method\LogOut;
use Vjik\TelegramBot\Api\Method\Payment\GetStarTransactions;
Expand Down Expand Up @@ -133,6 +134,8 @@
use Vjik\TelegramBot\Api\Type\File;
use Vjik\TelegramBot\Api\Type\ForceReply;
use Vjik\TelegramBot\Api\Type\ForumTopic;
use Vjik\TelegramBot\Api\Type\Inline\InlineQueryResult;
use Vjik\TelegramBot\Api\Type\Inline\InlineQueryResultsButton;
use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\InputFile;
use Vjik\TelegramBot\Api\Type\InputMedia;
Expand Down Expand Up @@ -236,6 +239,26 @@ public function answerCallbackQuery(
);
}

/**
* @see https://core.telegram.org/bots/api#answerinlinequery
*
* @param InlineQueryResult[] $results
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function answerInlineQuery(
string $inlineQueryId,
array $results,
?int $cacheTime = null,
?bool $isPersonal = null,
?string $nextOffset = null,
?InlineQueryResultsButton $button = null,
): FailResult|true {
return $this->send(
new AnswerInlineQuery($inlineQueryId, $results, $cacheTime, $isPersonal, $nextOffset, $button)
);
}

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

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type\Inline;

/**
* @see https://core.telegram.org/bots/api#inlinequeryresult
*/
interface InlineQueryResult
{
public function getType(): string;

public function toRequestArray(): array;
}
52 changes: 52 additions & 0 deletions src/Type/Inline/InlineQueryResultArticle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type\Inline;

use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;

/**
* @see https://core.telegram.org/bots/api#inlinequeryresultarticle
*/
final readonly class InlineQueryResultArticle implements InlineQueryResult
{
public function __construct(
public string $id,
public string $title,
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,
public ?int $thumbnailHeight = null
) {
}

public function getType(): string
{
return 'article';
}

public function toRequestArray(): array
{
return array_filter(
[
'type' => $this->getType(),
'id' => $this->id,
'title' => $this->title,
'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,
'thumbnail_height' => $this->thumbnailHeight,
],
static fn(mixed $value): bool => $value !== null,
);
}
}
59 changes: 59 additions & 0 deletions src/Type/Inline/InlineQueryResultAudio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type\Inline;

use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\MessageEntity;

/**
* @see https://core.telegram.org/bots/api#inlinequeryresultaudio
*/
final readonly class InlineQueryResultAudio implements InlineQueryResult
{
/**
* @param MessageEntity[]|null $captionEntities
*/
public function __construct(
public string $id,
public string $audioUrl,
public string $title,
public ?string $caption = null,
public ?string $parseMode = null,
public ?array $captionEntities = null,
public ?string $performer = null,
public ?int $audioDuration = null,
public ?InlineKeyboardMarkup $replyMarkup = null,
public ?InputMessageContent $inputMessageContent = null,
) {
}

public function getType(): string
{
return 'audio';
}

public function toRequestArray(): array
{
return array_filter(
[
'type' => $this->getType(),
'id' => $this->id,
'audio_url' => $this->audioUrl,
'title' => $this->title,
'caption' => $this->caption,
'parse_mode' => $this->parseMode,
'caption_entities' => $this->captionEntities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->captionEntities,
),
'performer' => $this->performer,
'audio_duration' => $this->audioDuration,
'reply_markup' => $this->replyMarkup?->toRequestArray(),
'input_message_content' => $this->inputMessageContent?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}
}
53 changes: 53 additions & 0 deletions src/Type/Inline/InlineQueryResultCachedAudio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type\Inline;

use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\MessageEntity;

/**
* @see https://core.telegram.org/bots/api#inlinequeryresultcachedaudio
*/
final readonly class InlineQueryResultCachedAudio implements InlineQueryResult
{
/**
* @param MessageEntity[]|null $captionEntities
*/
public function __construct(
public string $id,
public string $audioFileId,
public ?string $caption = null,
public ?string $parseMode = null,
public ?array $captionEntities = null,
public ?InlineKeyboardMarkup $replyMarkup = null,
public ?InputMessageContent $inputMessageContent = null,
) {
}

public function getType(): string
{
return 'audio';
}

public function toRequestArray(): array
{
return array_filter(
[
'type' => $this->getType(),
'id' => $this->id,
'audio_file_id' => $this->audioFileId,
'caption' => $this->caption,
'parse_mode' => $this->parseMode,
'caption_entities' => $this->captionEntities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->captionEntities,
),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
'input_message_content' => $this->inputMessageContent?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}
}
57 changes: 57 additions & 0 deletions src/Type/Inline/InlineQueryResultCachedDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type\Inline;

use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\MessageEntity;

/**
* @see https://core.telegram.org/bots/api#inlinequeryresultcacheddocument
*/
final readonly class InlineQueryResultCachedDocument implements InlineQueryResult
{
/**
* @param MessageEntity[]|null $captionEntities
*/
public function __construct(
public string $id,
public string $title,
public string $documentFileId,
public ?string $description = null,
public ?string $caption = null,
public ?string $parseMode = null,
public ?array $captionEntities = null,
public ?InlineKeyboardMarkup $replyMarkup = null,
public ?InputMessageContent $inputMessageContent = null,
) {
}

public function getType(): string
{
return 'document';
}

public function toRequestArray(): array
{
return array_filter(
[
'type' => $this->getType(),
'id' => $this->id,
'title' => $this->title,
'document_file_id' => $this->documentFileId,
'description' => $this->description,
'caption' => $this->caption,
'parse_mode' => $this->parseMode,
'caption_entities' => $this->captionEntities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->captionEntities,
),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
'input_message_content' => $this->inputMessageContent?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}
}
Loading
Loading