Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
vendor
4 changes: 2 additions & 2 deletions src/Methods/MethodsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Botify\Request\Client;
use Botify\TelegramAPI;
use Botify\Types\Map;
use Botify\Utils\Button;
use Botify\Utils\ReplyMarkup;
use Botify\Utils\FallbackResponse;
use Botify\Utils\Logger\Logger;
use Exception;
Expand Down Expand Up @@ -218,7 +218,7 @@ private function bindAttributes(&$attributes)
$replyMarkup = &$attributes['reply_markup'];

if (is_array($replyMarkup)) {
$replyMarkup = Button::make($replyMarkup);
$replyMarkup = ReplyMarkup::make($replyMarkup);
}
}

Expand Down
30 changes: 27 additions & 3 deletions src/Utils/Button.php → src/Utils/ReplyMarkup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Botify\Utils;

use function Botify\array_some;
use function Botify\{array_some, config, data_get, value};

class Button
class ReplyMarkup
{
private static array $keyboards = [];

/**
* @var array
*/
Expand All @@ -31,7 +33,7 @@ public function __construct(array $options = [])
* @param bool $json
* @return mixed
*/
public static function make($rows, array $options = [], $json = true): mixed
public static function make($rows, array $options = [], bool $json = true): mixed
{
$keyboard = new static($options);

Expand Down Expand Up @@ -109,4 +111,26 @@ public static function remove(): string
'remove_keyboard' => true,
]);
}

public static function generate(?string $key = null, ...$args)
{
static::$keyboards ??= require_once config('telegram.keyboards_path', function () {
throw new \Exception('You must set keyboards_path key in config/telegram.php');
});

if (isset($args['remove']) && $args['remove'] === true) {
return static::remove();
}

$json = $args['json'] ?? true;
$options = $args['options'] ?? [];
$default = $args['default'] ?? null;
unset($args['json'], $args['options'], $args['default']);

if (is_array($value = value(data_get(static::$keyboards, $key, $default), ... $args))) {
return ReplyMarkup::make($value, $options, $json);
}

return $default;
}
}
77 changes: 6 additions & 71 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Amp\Promise;
use ArrayAccess;
use Botify\Exceptions\RetryException;
use Botify\Utils\Button;
use Botify\Utils\ReplyMarkup;
use Botify\Utils\Collection;
use Botify\Utils\Config;
use Botify\Utils\Dotty;
Expand Down Expand Up @@ -396,44 +396,6 @@ function config($id = null, $default = null)
}
}

if (!function_exists('Botify\\repeat')) {
/**
* Repeat a code n times
* @param int $times
* @param callable $callback
* @param array $iterable
* @param ...$args
* @return array
*/
function repeat(int $times, callable $callback, array $iterable = [], ...$args): array
{
$returns = [];

$iterable = array_pad($iterable, $times, '*');

foreach ($iterable as $index => $item) {
$returns[] = $callback($item, $index, ... $args);
}
return $returns;
}
}

if (!function_exists('Botify\\arepeat')) {
/**
* Asynchronous version of repeat
*
* @param int $times
* @param callable $callback
* @param array $iterable
* @param mixed ...$args
* @return Promise
*/
function arepeat(int $times, callable $callback, array $iterable = [], ...$args): Promise
{
return gather(repeat($times, $callback, $iterable, ... $args));
}
}

if (!function_exists('Botify\\abs_path')) {
/**
* Get absolute path of a path
Expand Down Expand Up @@ -508,28 +470,15 @@ function array_every(array $array, ?callable $fn = null): bool
}
}

if (!function_exists('Botify\\button')) {
if (!function_exists('Botify\\keyboard')) {
/**
* @param $id
* @param null $key
* @param mixed ...$args
* @return mixed
*/
function button($id = null, ...$args): mixed
{
static $keyboards = null;
$keyboards ??= require_once base_path('utils/keyboards.php');
$json = $args['json'] ?? true;
$options = $args['options'] ?? [];
$default = $args['default'] ?? null;
unset($args['json'], $args['options'], $args['default']);

if (isset($args['remove']) && $args['remove'] === true) {
return Button::remove();
} elseif (is_array($value = value(data_get($keyboards, $id, $default), ... $args))) {
return Button::make($value, $options, $json);
}

return $default;
function keyboard($key = null, ...$args): mixed
{
return ReplyMarkup::generate($key, ... $args);
}
}

Expand Down Expand Up @@ -665,18 +614,4 @@ function array_sole(array $array, callable $fn): mixed

return false;
}
}

if (!function_exists('Botify\\file_get_contents')) {
function file_get_contents(string $filename): Promise
{
return read($filename);
}
}

if (!function_exists('Botify\\file_put_contents')) {
function file_put_contents(string $filename, string $data): Promise
{
return write($filename, $data);
}
}