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 keyboard function to messages #183

Merged
merged 9 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions src/TelegramFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function file(mixed $file, string $type, string $filename = null): self
'contents' => is_resource($file) ? $file : fopen($file, 'rb'),
];

if (null !== $filename) {
if ($filename !== null) {
abbasudo marked this conversation as resolved.
Show resolved Hide resolved
$this->payload['file']['filename'] = $filename;
}

Expand Down Expand Up @@ -191,7 +191,7 @@ public function toMultipart(): array
{
$data = [];
foreach ($this->payload as $name => $contents) {
$data[] = ('file' === $name) ? $contents : compact('name', 'contents');
$data[] = ($name === 'file') ? $contents : compact('name', 'contents');
}

return $data;
Expand Down
48 changes: 43 additions & 5 deletions src/Traits/HasSharedLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ trait HasSharedLogic
/** @var array Params payload. */
protected array $payload = [];

/** @var array Keyboard Buttons. */
protected array $keyboards = [];

/** @var array Inline Keyboard Buttons. */
protected array $buttons = [];

Expand All @@ -32,6 +35,41 @@ public function to(int|string $chatId): self
return $this;
}

/**
* sets reply markup for payload
*
*
* @return static
*
* @throws \JsonException
*/
public function reply(array $markup): self
abbasudo marked this conversation as resolved.
Show resolved Hide resolved
{
$this->payload['reply_markup'] = json_encode($markup, JSON_THROW_ON_ERROR);

return $this;
}

/**
* Add an inline button with callback_data.
abbasudo marked this conversation as resolved.
Show resolved Hide resolved
*
* @return static
*
* @throws \JsonException
*/
public function keyboard(string $text, int $columns = 2, bool $request_contact = false, bool $request_location = false): self
{
$this->keyboards[] = compact('text', 'request_contact', 'request_location');

$this->reply([
'keyboard' => array_chunk($this->keyboards, $columns),
'one_time_keyboard' => true, // Hide the keyboard after the user makes a selection
'resize_keyboard' => true, // Allow the keyboard to be resized
]);

return $this;
}

/**
* Add an inline button.
*
Expand All @@ -43,9 +81,9 @@ public function button(string $text, string $url, int $columns = 2): self
{
$this->buttons[] = compact('text', 'url');

$this->payload['reply_markup'] = json_encode([
$this->reply([
'inline_keyboard' => array_chunk($this->buttons, $columns),
], JSON_THROW_ON_ERROR);
]);

return $this;
}
Expand All @@ -61,9 +99,9 @@ public function buttonWithCallback(string $text, string $callback_data, int $col
{
$this->buttons[] = compact('text', 'callback_data');

$this->payload['reply_markup'] = json_encode([
$this->reply([
'inline_keyboard' => array_chunk($this->buttons, $columns),
], JSON_THROW_ON_ERROR);
]);

return $this;
}
Expand Down Expand Up @@ -99,7 +137,7 @@ public function token(string $token): self
*/
public function hasToken(): bool
{
return null !== $this->token;
return $this->token !== null;
}

/**
Expand Down