irazasyed/telegram-bot-sdk

Command class should be an instance of "Telegram\Bot\Commands\CommandInterface"

IVoyt opened this issue · 0 comments

<?php
declare(strict_types=1);

namespace Telegram\Commands;

use Telegram\Bot\Actions;
use Telegram\Bot\Commands\Command;

/**
 * This command can be triggered in two ways:
 * /start and /join due to the alias.
 */
class StartCommand extends Command
{
    protected string $name        = 'start';
    protected array  $aliases     = ['join'];
    protected string $description = 'Start Command to get you started';
    protected string $pattern     = '{username}
    {age: \d+}';

    public function handle(): void
    {
        # username from Update object to be used as fallback.
        $fallbackUsername = $this->getUpdate()->getMessage()->from->username;

        # Get the username argument if the user provides,
        # (optional) fallback to username from Update object as the default.
        $username = $this->argument(
            'username',
            $fallbackUsername
        );

        $this->replyWithMessage(
            [
                'text' => "Hello {$username}! Welcome to our bot, Here are our available commands:",
            ]
        );

        # This will update the chat status to "typing..."
        $this->replyWithChatAction(['action' => Actions::TYPING]);

        # Get all the registered commands.
        $commands = $this->getTelegram()->getCommands();

        $response = '';
        foreach ($commands as $name => $command) {
            $response .= sprintf('/%s - %s' . PHP_EOL, $name, $command->getDescription());
        }

        $this->replyWithMessage(['text' => $response]);
    }
}