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 sendDocument method #20

Merged
merged 1 commit into from
Jun 14, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- New #17: Add `getUserProfilePhotos` method and `UserProfilePhotos` type.
- New #18: Add `sendPoll` method and `InputPollOption` type.
- New #19: Add `sendAudio` method.
- New #20: Add `sendDocument` method.

## 0.1.0 June 10, 2024

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

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithFilesInterface;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\ForceReply;
use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\InputFile;
use Vjik\TelegramBot\Api\Type\Message;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\ReplyKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\ReplyKeyboardRemove;
use Vjik\TelegramBot\Api\Type\ReplyParameters;

/**
* @see https://core.telegram.org/bots/api#senddocument
*/
final readonly class SendDocument implements
TelegramRequestWithResultPreparingInterface,
TelegramRequestWithFilesInterface
{
/**
* @param MessageEntity[]|null $captionEntities
*/
public function __construct(
private int|string $chatId,
private string|InputFile $document,
private ?string $businessConnectionId = null,
private ?int $messageThreadId = null,
private string|InputFile|null $thumbnail = null,
private ?string $caption = null,
private ?string $parseMode = null,
private ?array $captionEntities = null,
private ?bool $disableContentTypeDetection = null,
private ?bool $disableNotification = null,
private ?bool $protectContent = null,
private ?string $messageEffectId = null,
private ?ReplyParameters $replyParameters = null,
private InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $replyMarkup = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'business_connection_id' => $this->businessConnectionId,
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'document' => is_string($this->document) ? $this->document : null,
'thumbnail' => is_string($this->thumbnail) ? $this->thumbnail : null,
'caption' => $this->caption,
'parse_mode' => $this->parseMode,
'caption_entities' => $this->captionEntities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->captionEntities,
),
'disable_content_type_detection' => $this->disableContentTypeDetection,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}

public function getFiles(): array
{
return array_filter(
[
'document' => $this->document instanceof InputFile ? $this->document : null,
'thumbnail' => $this->thumbnail instanceof InputFile ? $this->thumbnail : null,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): Message
{
return Message::fromTelegramResult($result);
}
}
44 changes: 44 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Vjik\TelegramBot\Api\Method\SendChatAction;
use Vjik\TelegramBot\Api\Method\SendContact;
use Vjik\TelegramBot\Api\Method\SendDice;
use Vjik\TelegramBot\Api\Method\SendDocument;
use Vjik\TelegramBot\Api\Method\SendLocation;
use Vjik\TelegramBot\Api\Method\SendMessage;
use Vjik\TelegramBot\Api\Method\SendPhoto;
Expand Down Expand Up @@ -382,6 +383,49 @@ public function sendDice(
);
}

/**
* @param MessageEntity[]|null $captionEntities
*
* @see https://core.telegram.org/bots/api#senddocument
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function sendDocument(
int|string $chatId,
string|InputFile $document,
?string $businessConnectionId = null,
?int $messageThreadId = null,
string|InputFile|null $thumbnail = null,
?string $caption = null,
?string $parseMode = null,
?array $captionEntities = null,
?bool $disableContentTypeDetection = null,
?bool $disableNotification = null,
?bool $protectContent = null,
?string $messageEffectId = null,
?ReplyParameters $replyParameters = null,
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $replyMarkup = null,
): FailResult|Message {
return $this->send(
new SendDocument(
$chatId,
$document,
$businessConnectionId,
$messageThreadId,
$thumbnail,
$caption,
$parseMode,
$captionEntities,
$disableContentTypeDetection,
$disableNotification,
$protectContent,
$messageEffectId,
$replyParameters,
$replyMarkup,
)
);
}

/**
* @see https://core.telegram.org/bots/api#sendlocation
*
Expand Down
99 changes: 99 additions & 0 deletions tests/Method/SendDocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\SendDocument;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\ForceReply;
use Vjik\TelegramBot\Api\Type\InputFile;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\ReplyParameters;

final class SendDocumentTest extends TestCase
{
public function testBase(): void
{
$method = new SendDocument(12, 'https://example.com/file.doc');

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('sendDocument', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 12,
'document' => 'https://example.com/file.doc',
],
$method->getData(),
);
$this->assertSame([], $method->getFiles());
}

public function testFull(): void
{
$document = new InputFile((new StreamFactory())->createStream('test'), 'test.doc');
$thumbnail = new InputFile((new StreamFactory())->createStream('test'), 'test.jpg');
$entity = new MessageEntity('bold', 0, 5);
$replyParameters = new ReplyParameters(23);
$replyMarkup = new ForceReply();
$method = new SendDocument(
12,
$document,
'bcid1',
99,
$thumbnail,
'Caption',
'HTML',
[$entity],
false,
true,
false,
'meID',
$replyParameters,
$replyMarkup,
);

$this->assertSame(
[
'business_connection_id' => 'bcid1',
'chat_id' => 12,
'message_thread_id' => 99,
'caption' => 'Caption',
'parse_mode' => 'HTML',
'caption_entities' => [$entity->toRequestArray()],
'disable_content_type_detection' => false,
'disable_notification' => true,
'protect_content' => false,
'message_effect_id' => 'meID',
'reply_parameters' => $replyParameters->toRequestArray(),
'reply_markup' => $replyMarkup->toRequestArray(),
],
$method->getData(),
);
$this->assertSame(
[
'document' => $document,
'thumbnail' => $thumbnail,
],
$method->getFiles(),
);
}

public function testPrepareResult(): void
{
$method = new SendDocument(12, 'https://example.com/file.doc');

$preparedResult = $method->prepareResult([
'message_id' => 7,
'date' => 1620000000,
'chat' => [
'id' => 1,
'type' => 'private',
],
]);

$this->assertSame(7, $preparedResult->messageId);
}
}
20 changes: 20 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,26 @@ public function testSendDice(): void
$this->assertSame(7, $result->messageId);
}

public function testSendDocument(): void
{
$api = $this->createApi([
'ok' => true,
'result' => [
'message_id' => 7,
'date' => 1620000000,
'chat' => [
'id' => 1,
'type' => 'private',
],
],
]);

$result = $api->sendDocument(12, 'https://example.com/file.doc');

$this->assertInstanceOf(Message::class, $result);
$this->assertSame(7, $result->messageId);
}

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