Core: How update message?

Created on 7 Oct 2016  路  6Comments  路  Source: php-telegram-bot/core

### Required Information

- PHP version: 5.6
- PHP Telegram Bot version: 0.35.0
- Using MySQL database: yes 
- Update Method: Webhook
- Self-signed certificate: no
- RAW update (if available):

Expected behaviour

When I call a callback_data on InlineKeyboardButton I have to edit previous chat message

Actual behaviour

$new_update = new Update(
                [
                    'update_id' => time(), 
                    //'message' => $new_message_array,
                    'edited_message' => $new_message_array
                ],
                $callback_query->getBotName()
            );

If uncommented message chatting a new message. If uncommented edited_message nothing happens

Steps to reproduce

Command class

$inline_keyboard = [];
$inline_keyboard[] = new InlineKeyboardButton(['text' => json_decode('"\u25C0"'), 'callback_data' => 'search_prev']);
$inline_keyboard[] = new InlineKeyboardButton(['text' => json_decode('"\u25B6"'), 'callback_data' => 'search_next']);

$data['reply_markup'] = new InlineKeyboardMarkup(['inline_keyboard' => [$inline_keyboard]]);
$result = Request::sendMessage($data);

CallbackqueryCommand.php

$update = $this->getUpdate();
        $callback_query = $update->getCallbackQuery();
        $callback_query_id = $callback_query->getId();
        $callback_data = $callback_query->getData();

        //$message = $this->getMessage();
        //$chat_id = $message->getChat()->getId();

        $data = [
            //'chat_id' => $chat_id,
            'callback_query_id' => $callback_query_id,
        ];

        list($command, $text) = explode('_', $callback_data);

        if (isset($this->commands[$command]) && $this->getTelegram()->getCommandObject($this->commands[$command])) {
            $cmdobj = $this->getTelegram()->getCommandObject($this->commands[$command]);

            $new_message_array = json_decode($callback_query->getMessage()->toJson(), true);

            $new_message_array['from'] = $new_message_array['chat'];
            $new_message_array['text'] = $text;

            $new_update = new Update(
                [
                    'update_id' => time(), 
                    //'message' => $new_message_array,
                    'edited_message' => $new_message_array
                ],
                $callback_query->getBotName()
            );
            $cmdobj->setUpdate($new_update)->preExecute();

            return Request::answerCallbackQuery(['callback_query_id' => $callback_query_id]);
        }

Most helpful comment

Example usage:

<?php

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;

class TestCommand extends UserCommand
{
    public function execute()
    {
        $message = $this->getMessage();

        $data = [
            'chat_id' => $message->getChat()->getId(),
            'text' => 'This message will be edited!'
        ];

        $result = Request::sendMessage($data);

        if ($result->isOk()) {
            $data = [
                'chat_id' => $message->getChat()->getId(),
                'message_id' => $result->getResult()->getMessageId(),
                'text' => 'This message was edited.'
            ];

            Request::editMessageText($data);
        } else {
            Request::emptyResponse();
        }
    }
}

All 6 comments

Check API editMessageText docs and corresponding Request::editMesageText method

Can someone tell, in what way I can edit message text?
or in 0.38.1 I can't do this?

Example usage:

<?php

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;

class TestCommand extends UserCommand
{
    public function execute()
    {
        $message = $this->getMessage();

        $data = [
            'chat_id' => $message->getChat()->getId(),
            'text' => 'This message will be edited!'
        ];

        $result = Request::sendMessage($data);

        if ($result->isOk()) {
            $data = [
                'chat_id' => $message->getChat()->getId(),
                'message_id' => $result->getResult()->getMessageId(),
                'text' => 'This message was edited.'
            ];

            Request::editMessageText($data);
        } else {
            Request::emptyResponse();
        }
    }
}

thank you!
and last question - if I need edit message after inlinebutton pressed, I must send empty answerCallbackQuery and then editMessageText like in your example?

That'll work

ok, thank you very much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcolino7 picture marcolino7  路  3Comments

Recouse picture Recouse  路  3Comments

noplanman picture noplanman  路  3Comments

smaznet picture smaznet  路  4Comments

ttvd94 picture ttvd94  路  4Comments