php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.9k stars 955 forks source link

how can find inline_message_id of inline query that exist in chat to edit this response message? #1022

Closed alirezadp10 closed 4 years ago

alirezadp10 commented 4 years ago

hey guys I try to implement telegram bot that behave like @imdb but unfortunately when I get the $this->getChosenInlineResult() method in the ChoseninlineresultCommand.php I could not find the message_id of the inline message that created with input_message_content parameter of InlineQueryResultArticle method

the main question is how can I represent the result of item selected in specific chat?

here is my code:

InlinequeryCommand.php

class InlinequeryCommand extends SystemCommand
{
    /**
     * @var string
     */
    protected $name = 'inlinequery';

    /**
     * @var string
     */
    protected $description = 'Reply to inline query';

    /**
     * @var string
     */
    protected $version = '1.1.1';

    /**
     * Command execute method
     *
     * @return \Longman\TelegramBot\Entities\ServerResponse
     * @throws \Longman\TelegramBot\Exception\TelegramException
     */
    public function execute()
    {
        file_put_contents('inline.log',json_encode($this->getInlineQuery()));
        $inline_query = $this->getInlineQuery();
        $query        = $inline_query->getQuery();
        $data         = ['inline_query_id' => $inline_query->getId()];
        $results = [];
        if ($query !== '') {
            $articles = [
                [
                    'id'                    => "01,{$inline_query->getId()}",
                    'title'                 => 'آهنگ Stay از Rihanna',
                    'description'           => 'ترجمه از: کیارش',
                    'thumb_url'             => 'http://ziba1.ir/images/15326.jpg',
                    'input_message_content' => new InputTextMessageContent(['message_text' => "ترجمه آهنگ Stay از Rihanna"]),
                ],
                [
                    'id'                    => '02',
                    'title'                 => 'آهنگ Never Really Over از Katy Perry',
                    'description'           => 'ترجمه از: عرفان',
                    'thumb_url'             => 'http://ziba1.ir/images/21454.png',
                    'input_message_content' => new InputTextMessageContent(['message_text' => "ترجمه آهنگ Never Really Over از Katy Perry"]),
                ],
            ];
            foreach ($articles as $article) {
                $results[] 
                    = new InlineQueryResultArticle($article);
            }
        }
        $data['results'] = '[' . implode(',',$results) . ']';
        return Request::answerInlineQuery($data);
    }
}

ChoseninlineresultCommand.php

class ChoseninlineresultCommand extends SystemCommand
{
    /**
     * @var string
     */
    protected $name = 'choseninlineresult';

    /**
     * @var string
     */
    protected $description = 'Chosen result query';

    /**
     * @var string
     */
    protected $version = '1.1.1';

    /**
     * Command execute method
     *
     * @return \Longman\TelegramBot\Entities\ServerResponse
     * @throws \Longman\TelegramBot\Exception\TelegramException
     */
    public function execute()
    {
        file_put_contents('result.log',json_encode($this->getChosenInlineResult(),TRUE));
        $inlineQuery = $this->getChosenInlineResult();
        $query = $inlineQuery->getQuery();
        return parent::execute();
    }
}
noplanman commented 4 years ago

You can retrieve the unique id from the chosen result, which should be used to get the content you need (from the array of articles generated). $unique_id = $this->getChosenInlineResult()->getResultId();

Does that get you what you need? You'll need to extract the logic that generates the articles array, to be able to access it from the ChoseninlineresultCommand as well.

noplanman commented 4 years ago

@alirezadp10G Did you manage to get it to work?