|
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\Entities; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* \addtogroup Entities Entities |
|
23
|
|
|
* @{ |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* \class InlineQueryResults InlineQueryResults |
|
28
|
|
|
* \brief Handle and store results before sending them to an answerInlineQuery api call. |
|
29
|
|
|
*/ |
|
30
|
|
|
class InlineQueryResults |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* \addtogroup InlineQueryResults InlineQueryResults |
|
34
|
|
|
* \brief Handle and store results before sending them to an answerInlineQuery api call. |
|
35
|
|
|
* \details To answer Inline Queries with results create an object of type PhpBotFramework\Entities\InlineQueryResults and add the wanted results using InlineQueryResults::addResult() method or type-based result method (InlineQueryResults::newArtile()). |
|
36
|
|
|
* |
|
37
|
|
|
* use PhpBotFramework\Entities\InlineQueryResults; |
|
38
|
|
|
* use PhpBotFramework\Entities\InlineQuery; |
|
39
|
|
|
* ~ |
|
40
|
|
|
* ~ |
|
41
|
|
|
* ~ |
|
42
|
|
|
* processInlineQuery(InlineQuery $inline_query) { |
|
43
|
|
|
* $handler = new InlineQueryResults(); |
|
44
|
|
|
* $handler->newArticle('First article', 'This is the first result'); |
|
45
|
|
|
* |
|
46
|
|
|
* $this->answerInlineQuery($handler->get()); |
|
47
|
|
|
* } |
|
48
|
|
|
* |
|
49
|
|
|
* @{ |
|
50
|
|
|
*/ |
|
51
|
|
|
|
|
52
|
|
|
/** \brief Array of the results stored. */ |
|
53
|
|
|
private $results; |
|
54
|
|
|
|
|
55
|
|
|
/** \brief Count the result so we can assign them an unique id. */ |
|
56
|
|
|
private $id_result; |
|
57
|
|
|
|
|
58
|
|
|
/** \brief Type accepted for results. */ |
|
59
|
|
|
private $accepted_type = [ |
|
60
|
|
|
'audio', |
|
61
|
|
|
'article', |
|
62
|
|
|
'photo', |
|
63
|
|
|
'gif', |
|
64
|
|
|
'mpeg4_gif', |
|
65
|
|
|
'video', |
|
66
|
|
|
'voice', |
|
67
|
|
|
'document', |
|
68
|
|
|
'location', |
|
69
|
|
|
'venue', |
|
70
|
|
|
'contact', |
|
71
|
|
|
'game' |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* \constructor Create an InlineQueryResult object. */ |
|
76
|
|
|
public function __construct() |
|
77
|
|
|
{ |
|
78
|
|
|
// Initialize the array to empty |
|
79
|
|
|
$this->results = []; |
|
80
|
|
|
|
|
81
|
|
|
$this->id_result = 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* \brief Add a result passing an array containing data. |
|
86
|
|
|
* \details Create a result of one of these 20 types: |
|
87
|
|
|
* - InlineQueryResultCachedAudio |
|
88
|
|
|
* - InlineQueryResultCachedDocument |
|
89
|
|
|
* - InlineQueryResultCachedGif |
|
90
|
|
|
* - InlineQueryResultCachedMpeg4Gif |
|
91
|
|
|
* - InlineQueryResultCachedPhoto |
|
92
|
|
|
* - InlineQueryResultCachedSticker |
|
93
|
|
|
* - InlineQueryResultCachedVideo |
|
94
|
|
|
* - InlineQueryResultCachedVoice |
|
95
|
|
|
* - InlineQueryResultArticle |
|
96
|
|
|
* - InlineQueryResultAudio |
|
97
|
|
|
* - InlineQueryResultContact |
|
98
|
|
|
* - InlineQueryResultGame |
|
99
|
|
|
* - InlineQueryResultDocument |
|
100
|
|
|
* - InlineQueryResultGif |
|
101
|
|
|
* - InlineQueryResultLocation |
|
102
|
|
|
* - InlineQueryResultMpeg4Gif |
|
103
|
|
|
* - InlineQueryResultPhoto |
|
104
|
|
|
* - InlineQueryResultVenue |
|
105
|
|
|
* - InlineQueryResultVideo |
|
106
|
|
|
* - InlineQueryResultVoice |
|
107
|
|
|
* |
|
108
|
|
|
* To add a result, create an array contaning data as showed on API Reference, 'id' parameter will be automatically genereted so there is no need to add it. |
|
109
|
|
|
* Example of adding an article result: |
|
110
|
|
|
* |
|
111
|
|
|
* $data = [ |
|
112
|
|
|
* 'type' => 'result', |
|
113
|
|
|
* 'title' => 'Example title', |
|
114
|
|
|
* 'message_text' => 'Text of the message' |
|
115
|
|
|
* ]; |
|
116
|
|
|
* $handler->addResult($data); |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $result Array containg data of the result to add. |
|
119
|
|
|
* @return int Id of the result added. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function addResult(array $result) : int |
|
122
|
|
|
{ |
|
123
|
|
|
if (array_key_exists('type', $result) || in_array($result['type'], $accepted_type)) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
throw BotException("Result has wrong or no type at all. Check that the result has a value of key 'type' that correspond to a type in the API Reference"); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// Set the id of the result to add |
|
129
|
|
|
$result['id'] = (string)$this->id_result; |
|
130
|
|
|
|
|
131
|
|
|
$this->results[] = $result; |
|
132
|
|
|
|
|
133
|
|
|
return $this->id_result++; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* \brief Add a result of type article article. |
|
138
|
|
|
* \details Add a result that will be show to the user. |
|
139
|
|
|
* @param string $title Title of the result. |
|
140
|
|
|
* @param string $message_text Text of the message to be sent. |
|
141
|
|
|
* @param string $description <i>Optional</i>. Short description of the result |
|
142
|
|
|
* @param array $reply_markup Inline keyboard object (Not JSON serialized, use getArray from InlineKeyboard class). |
|
143
|
|
|
* @param string $parse_mode <i>Optional</i>. Formattation of the message. |
|
144
|
|
|
* @param string $disable_web_preview <i>Optional</i>. Disables link previews for links in the sent message. |
|
145
|
|
|
* @return int Id the the article added. |
|
146
|
|
|
*/ |
|
147
|
|
|
public function newArticle(string $title, string $message_text, string $description = '', array $reply_markup = null, $parse_mode = 'HTML', $disable_web_preview = false) : int |
|
148
|
|
|
{ |
|
149
|
|
|
array_push($this->results, [ |
|
150
|
|
|
'type' => 'article', |
|
151
|
|
|
'id' => (string)$this->id_result, |
|
152
|
|
|
'title' => $title, |
|
153
|
|
|
'message_text' => $message_text, |
|
154
|
|
|
'description' => $description, |
|
155
|
|
|
'parse_mode' => $parse_mode, |
|
156
|
|
|
'reply_markup' => $reply_markup, |
|
157
|
|
|
'disable_web_page_preview' => $disable_web_preview |
|
158
|
|
|
]); |
|
159
|
|
|
|
|
160
|
|
|
return $this->id_result++; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* \brief Get all results created. |
|
165
|
|
|
* @return string JSON-serialized string containing the results. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function get() |
|
168
|
|
|
{ |
|
169
|
|
|
// Encode the results to get a JSON-serialized object |
|
170
|
|
|
$encoded_results = json_encode($this->results); |
|
171
|
|
|
|
|
172
|
|
|
// Clear the results |
|
173
|
|
|
$this->results = []; |
|
174
|
|
|
|
|
175
|
|
|
// Reset counter |
|
176
|
|
|
$this->id_result = 0; |
|
177
|
|
|
|
|
178
|
|
|
return $encoded_results; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** @} */ |
|
182
|
|
|
|
|
183
|
|
|
/** @} */ |
|
184
|
|
|
} |
|
185
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.