php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.87k stars 951 forks source link

How update message? #310

Closed linbis closed 7 years ago

linbis commented 7 years ago
### 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]);
        }
chuv1 commented 7 years ago

Check API editMessageText docs and corresponding Request::editMesageText method

rh9bka commented 7 years ago

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

jacklul commented 7 years ago

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();
        }
    }
}
rh9bka commented 7 years ago

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?

jacklul commented 7 years ago

That'll work

rh9bka commented 7 years ago

ok, thank you very much!