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

Executing Command #19

Closed
MyZik opened this issue Nov 1, 2019 · 2 comments
Closed

Executing Command #19

MyZik opened this issue Nov 1, 2019 · 2 comments
Labels

Comments

@MyZik
Copy link

MyZik commented Nov 1, 2019

Hi, everyone!

I want to execute a command from another command with sendMessage method. /start calls /clients command and they have Inline Keyboard buttons.

It works well, but now StartCommand calls ClientsCommands via editMessageText method...

I mean:
StartCommand -> ClientsCommand (sendMessage)
ClientsCommands -> ClientsCommand (editMessageText, normal use)

How can I do this?

Thanks in advance.

StartCommand

...

public function execute(BotApi $api, Update $update)
    {
        $index = isset($update) ? $this->getIndex($update) : 'start';

        $messageId = $chatId = null;

        if ($update->getCallbackQuery()) {
            $chat = $update->getCallbackQuery()->getMessage()->getChat();
            $messageId = $update->getCallbackQuery()->getMessage()->getMessageId();
        } else {
            $chat = $update->getMessage()->getChat();
        }

        $this->showSection($api, $index, $chat->getId(), $messageId);
    }

    /**
     * @param Update $update
     * @return bool
     */
    public function isApplicable(Update $update)
    {
        if (parent::isApplicable($update)) {
            return true;
        }
        return $this->getIndex($update) !== null;
    }

    /**
     * @param Update $update
     * @return mixed|null
     */
    private function getIndex(Update $update)
    {
        if ($update->getMessage() && preg_match(self::REGEX_INDEX, $update->getMessage()->getText(), $matches)) {
            return $matches[1];
        }
        if ($update->getCallbackQuery() && preg_match(self::REGEX_INDEX, $update->getCallbackQuery()->getData(), $matches)) {
            return $matches[1];
        }
        return null;
    }

    /**
     * @param BotApi $api
     * @param $index
     * @param $chatId
     * @param null $messageId
     * @throws \TelegramBot\Api\Exception
     * @throws \TelegramBot\Api\InvalidArgumentException
     */
    private function showSection(BotApi $api, $index, $chatId, $messageId = null) {
        $replyMarkup = new InlineKeyboardMarkup([
            [['text' => 'Über uns', 'callback_data' => '/start_about']],
            [['text' => 'Kunden', 'callback_data' => '/clients_0']]
        ]);

        switch ($index) {
            case 'about':
                $text = 'Text from *About* section';
                break;

            case 'clients':
                $text = 'Text from *Clients* section';
                break;

            default:
                $text = "Hello :)" .
                    "❓ How can I help you?";
        }

        if ($messageId) {
            $api->editMessageText(
                $chatId,
                $messageId,
                $text,
                'markdown',
                false,
                $replyMarkup
            );
        } else {
            $api->sendMessage(
                $chatId,
                $text,
                'markdown',
                false,
                null,
                $replyMarkup
            );
        }
    }

ClientsCommand

... 

public function execute(BotApi $api, Update $update)
    {
        $clients = $this->repository->findAll();
        $index = (int)$this->getIndex($update);
        $index = isset($clients[$index]) ? $index : 0;

        $messageId = $chatId = null;

        if ($update->getCallbackQuery()) {
            $chat = $update->getCallbackQuery()->getMessage()->getChat();
            $messageId = $update->getCallbackQuery()->getMessage()->getMessageId();
        } else {
            $chat = $update->getMessage()->getChat();
        }

        $this->paginateClients($api, $index, $chat->getId(), $messageId);
    }

    /**
     * @param Update $update
     * @return bool
     */
    public function isApplicable(Update $update)
    {
        if (parent::isApplicable($update)) {
            return true;
        }
        return $this->getIndex($update) !== null;
    }

    /**
     * @param Update $update
     * @return mixed|null
     */
    private function getIndex(Update $update)
    {
        if ($update->getMessage() && preg_match(self::REGEX_INDEX, $update->getMessage()->getText(), $matches)) {
            return $matches[1];
        }
        if ($update->getCallbackQuery() && preg_match(self::REGEX_INDEX, $update->getCallbackQuery()->getData(), $matches)) {
            return $matches[1];
        }

        return null;
    }

    /**
     * @param BotApi $api
     * @param $index
     * @param $chatId
     * @param null $messageId
     * @throws \TelegramBot\Api\Exception
     * @throws \TelegramBot\Api\InvalidArgumentException
     */
    private function paginateClients(BotApi $api, $index, $chatId, $messageId = null) {
        $count = count($this->repository->findAll());
        $client = $this->repository->findById($index);

        if ($index === 'execute') {
            $index = 0;
        }

        $prev = $next = null;
        if ($index - 1 >= 0) {
            $prev = $index - 1;
        }
        if ($index + 1 < $count) {
            $next = $index + 1;
        }
        $buttons = [];

        if ($prev !== null) {
            $buttons[] = ['text' => '⬅️', 'callback_data' => '/clients_' . $prev];
        }

        $buttons[] = ['text' => '🌐 Zur Website', 'url' => $client['link']];

        if ($next !== null) {
            $buttons[] = ['text' => '➡️', 'callback_data' => '/clients_' . $next];
        }

        $text = "▫️ *{$client['title']}*\n\n" .
            "📋 _{$client['description']}_";

        if ($messageId) {
            $api->editMessageText(
                $chatId,
                $messageId,
                $text,
                'markdown',
                false,
                new InlineKeyboardMarkup([$buttons])
            );
        } else {
            $api->sendMessage(
                $chatId,
                $text,
                'markdown',
                false,
                null,
                new InlineKeyboardMarkup([$buttons])
            );
        }
    }
@BoShurik
Copy link
Owner

BoShurik commented Nov 1, 2019

StartCommand calls ClientsCommands via editMessageText method

What do you mean by via editMessageText method? Maybe you have an example bot with this behavior?

@MyZik
Copy link
Author

MyZik commented Nov 2, 2019

StartCommand calls ClientsCommands via editMessageText method

What do you mean by via editMessageText method? Maybe you have an example bot with this behavior?

StartCommand -> Click "About" button -> editMessageText shows you text (a part of StartCommand
StartCommand -> Click "Clients" button -> sendMessage ClientsCommand (execute another command)

So, I made it!

In StartCommand:

$replyMarkup = new InlineKeyboardMarkup([
    [['text' => 'About', 'callback_data' => '/start_about']],
    [['text' => 'Clients', 'callback_data' => '/clients_execute']],
]);

In ClientsCommand:

public function execute(BotApi $api, Update $update)
    {
       ...

        $messageId = $chatId = null;

        if ($update->getCallbackQuery()) {
            $chat = $update->getCallbackQuery()->getMessage()->getChat();

            if ($update->getCallbackQuery()->getData() !== '/clients_execute') {
                $messageId = $update->getCallbackQuery()->getMessage()->getMessageId();
            }
        } else {
            $chat = $update->getMessage()->getChat();
        }

        $this->paginateClients($api, $index, $chat->getId(), $messageId);
    }

It works :)

@BoShurik BoShurik closed this as completed Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants