InlinequeryCommand.php 2.66 KB
Newer Older
MBoretto's avatar
MBoretto committed
1
<?php
2
/**
MBoretto's avatar
MBoretto committed
3 4 5 6 7 8
 * This file is part of the TelegramBot package.
 *
 * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
9 10
 */

11
namespace Longman\TelegramBot\Commands\SystemCommands;
MBoretto's avatar
MBoretto committed
12

13
use Longman\TelegramBot\Commands\SystemCommand;
14 15
use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultArticle;
use Longman\TelegramBot\Entities\InputMessageContent\InputTextMessageContent;
16
use Longman\TelegramBot\Request;
MBoretto's avatar
MBoretto committed
17

18 19 20
/**
 * Inline query command
 */
21
class InlinequeryCommand extends SystemCommand
MBoretto's avatar
MBoretto committed
22
{
23 24
    /**
     * @var string
25
     */
MBoretto's avatar
MBoretto committed
26
    protected $name = 'inlinequery';
27 28 29 30

    /**
     * @var string
     */
MBoretto's avatar
MBoretto committed
31
    protected $description = 'Reply to inline query';
32 33 34 35

    /**
     * @var string
     */
36
    protected $version = '1.1.0';
MBoretto's avatar
MBoretto committed
37

38
    /**
39 40 41
     * Command execute method
     *
     * @return mixed
42
     * @throws \Longman\TelegramBot\Exception\TelegramException
43
     */
MBoretto's avatar
MBoretto committed
44 45
    public function execute()
    {
46
        $update       = $this->getUpdate();
MBoretto's avatar
MBoretto committed
47
        $inline_query = $update->getInlineQuery();
48
        $query        = $inline_query->getQuery();
MBoretto's avatar
MBoretto committed
49

50
        $data    = ['inline_query_id' => $inline_query->getId()];
51 52 53 54
        $results = [];

        if ($query !== '') {
            $articles = [
55 56 57 58
                [
                    'id'                    => '001',
                    'title'                 => 'https://core.telegram.org/bots/api#answerinlinequery',
                    'description'           => 'you enter: ' . $query,
59
                    'input_message_content' => new InputTextMessageContent(['message_text' => ' ' . $query]),
60 61 62 63 64
                ],
                [
                    'id'                    => '002',
                    'title'                 => 'https://core.telegram.org/bots/api#answerinlinequery',
                    'description'           => 'you enter: ' . $query,
65
                    'input_message_content' => new InputTextMessageContent(['message_text' => ' ' . $query]),
66 67 68 69 70
                ],
                [
                    'id'                    => '003',
                    'title'                 => 'https://core.telegram.org/bots/api#answerinlinequery',
                    'description'           => 'you enter: ' . $query,
71
                    'input_message_content' => new InputTextMessageContent(['message_text' => ' ' . $query]),
72
                ],
73 74 75 76 77
            ];

            foreach ($articles as $article) {
                $results[] = new InlineQueryResultArticle($article);
            }
78 79
        }

80
        $data['results'] = '[' . implode(',', $results) . ']';
MBoretto's avatar
MBoretto committed
81

82
        return Request::answerInlineQuery($data);
MBoretto's avatar
MBoretto committed
83 84
    }
}