Completed
Push — master ( 7139d3...7268ad )
by Danilo
03:30
created

Inline::answerInlineQuery()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 5
crap 20
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Core;
20
21
trait Inline
22
{
23
24
    abstract protected function execRequest(string $url);
25
26
    /**
27
     * \addtogroup Core Core(Internal)
28
     * @{
29
     */
30
31
    /** \brief Store ID of the callback query received. */
32
    protected $_callback_query_id;
33
34
    /** \brief Store ID of the inline query received. */
35
    protected $_inline_query_id;
36
37
    /** @} */
38
39
    /**
40
     * \addtogroup API API Methods
41
     * @{
42
     */
43
44
    /** \brief Answer a callback query.
45
     * \details Remove the 'updating' circle icon on an inline keyboard button showing a message/alert to the user.
46
     * It'll always answer the current callback query. [Api reference](https://core.telegram.org/bots/api#answercallbackquery)
47
     * @param string $text <i>Optional</i>. Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters.
48
     * @param bool $show_alert <i>Optional</i>. If true, an alert will be shown by the client instead of a notification at the top of the chat screen.
49
     * @param string $url <i>Optional</i>. URL that will be opened by the user's client. If you have created a Game and accepted the conditions via @Botfather, specify the URL that opens your game – note that this will only work if the query comes from a callback_game button.
50
     * Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter.
51
     * @return bool True on success.
52
     */
53
    public function answerCallbackQuery($text = '', $show_alert = false, string $url = '') : bool
54
    {
55
        if (!isset($this->_callback_query_id)) {
56
            throw new BotException("Callback query id not set, wrong update");
57
        }
58
59
        $parameters = [
60
            'callback_query_id' => $this->_callback_query_id,
61
            'text' => $text,
62
            'show_alert' => $show_alert,
63
            'url' => $url
64
        ];
65
66
        return $this->execRequest('answerCallbackQuery?' . http_build_query($parameters));
67
    }
68
69
70
    /**
71
     * \brief Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the
72
     * private chat with the bot, on the top of the results. [Api reference](https://core.telegram.org/bots/api#answerinlinequery)
73
     * @param string $results A JSON-serialized array of results for the inline query. Use PhpBotFramework\Entities\InlineQueryResult class to create and get them.
74
     * @param string $switch_pm_text If passed, clients will display a button with specified text that switches the user to a private chat with the bot and sends the bot a start message with the parameter $switch_pm_parameter.
75
     * @param bool $is_personal Pass True, if results may be cached on the server side only for the user that sent the query. By default, results may be returned to any user who sends the same query.
76
     * @param int $cache_time The maximum amount of time in seconds that the result of the inline query may be cached on the server. Defaults to 300.
77
     * @return bool True on success.
78
     */
79
    public function answerInlineQuery(string $results = '', string $switch_pm_text = '', $switch_pm_parameter = '', bool $is_personal = true, int $cache_time = 300) : bool
80
    {
81
        if (!isset($this->_inline_query_id)) {
82
            throw new BotException("Inline query id not set, wrong update");
83
        }
84
85
        $parameters = [
86
            'inline_query_id' => $this->_inline_query_id,
87
            'switch_pm_text' => $switch_pm_text,
88
            'is_personal' => $is_personal,
89
            'switch_pm_parameter' => $switch_pm_parameter,
90
            'cache_time' => $cache_time
91
        ];
92
93
        if (isset($results) && $results !== '')
94
        {
95
            $parameters['results'] = $results;
96
        }
97
98
        return $this->execRequest('answerInlineQuery?' . http_build_query($parameters));
99
    }
100
101
    /** @} */
102
}
103