1 | <?php |
||
14 | class BaseBot extends CoreBot { |
||
15 | |||
16 | /** |
||
17 | * \addtogroup Core Core(Internal) |
||
18 | * @{ |
||
19 | */ |
||
20 | |||
21 | /** \brief Is the bot using webhook? */ |
||
22 | protected $_is_webhook; |
||
23 | |||
24 | /** |
||
25 | * \brief Construct an empty base bot. |
||
26 | * \details Construct a base bot that can handle updates. |
||
27 | */ |
||
28 | public function __construct(string $token) { |
||
43 | |||
44 | /** @} */ |
||
45 | |||
46 | /** |
||
47 | * \addtogroup Bot |
||
48 | * @{ |
||
49 | */ |
||
50 | |||
51 | /** |
||
52 | * \brief Get update and process it. |
||
53 | * \details Call this method if you are using webhook. |
||
54 | * It will get update from php::\input, check it and then process it using processUpdate. |
||
55 | */ |
||
56 | public function processWebhookUpdate() { |
||
70 | |||
71 | /** |
||
72 | * \brief Get updates received by the bot, and hold the offset in $offset. |
||
73 | * \details Get the update_id of the first update to parse, set it in $offset and |
||
74 | * then it start an infinite loop where it processes updates and keep $offset on the update_id of the last update received. |
||
75 | * Each processUpdate() method call is surrounded by a try/catch. |
||
76 | * @see getUpdates |
||
77 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
78 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
79 | */ |
||
80 | public function getUpdatesLocal(int $limit = 100, int $timeout = 60) { |
||
139 | |||
140 | /** @} */ |
||
141 | |||
142 | /** |
||
143 | * \addtogroup Core Core(Internal) |
||
144 | * @{ |
||
145 | */ |
||
146 | |||
147 | /** |
||
148 | * \brief Dispatch each update to the right method (processMessage, processCallbackQuery, etc). |
||
149 | * \details Set $chat_id for each update, $text, $data and $query are set for each update that contains them. |
||
150 | * @param $update Reference to the update received. |
||
151 | * @return The id of the update processed. |
||
152 | */ |
||
153 | protected function processUpdate(array $update) : int { |
||
187 | |||
188 | /** @} */ |
||
189 | |||
190 | /** |
||
191 | * \addtogroup Bot Bot |
||
192 | * @{ |
||
193 | */ |
||
194 | |||
195 | /** |
||
196 | * \brief Called every message received by the bot. |
||
197 | * \details Override it to script the bot answer for each message. |
||
198 | * <code>$chat_id</code> and <code>$text</code>, if the message contains text(use getMessageText() to access it), set inside of this function. |
||
199 | * @param $message Reference to the message received. |
||
200 | */ |
||
201 | protected function processMessage($message) { |
||
204 | |||
205 | /** |
||
206 | * \brief Called every callback query received by the bot. |
||
207 | * \details Override it to script the bot answer for each callback. |
||
208 | * <code>$chat_id</code> and <code>$data</code>, if set in the callback query(use getCallbackData() to access it) set inside of this function. |
||
209 | * @param $callback_query Reference to the callback query received. |
||
210 | */ |
||
211 | protected function processCallbackQuery($callback_query) { |
||
214 | |||
215 | /** |
||
216 | * \brief Called every inline query received by the bot. |
||
217 | * \details Override it to script the bot answer for each inline query. |
||
218 | * $chat_id and $query(use getInlineQuery() to access it) set inside of this function. |
||
219 | * @param $inline_query Reference to the inline query received. |
||
220 | */ |
||
221 | protected function processInlineQuery($inline_query) { |
||
224 | |||
225 | /** |
||
226 | * \brief Called every chosen inline result received by the bot. |
||
227 | * \details Override it to script the bot answer for each chosen inline result. |
||
228 | * <code>$chat_id</code> set inside of this function. |
||
229 | * @param $chosen_inline_result Reference to the chosen inline result received. |
||
230 | */ |
||
231 | protected function processChosenInlineResult($chosen_inline_result) { |
||
234 | |||
235 | /** |
||
236 | * \brief Called every chosen edited message received by the bot. |
||
237 | * \details Override it to script the bot answer for each edited message. |
||
238 | * <code>$chat_id</code> set inside of this function. |
||
239 | * @param $edited_message The message edited by the user. |
||
240 | */ |
||
241 | protected function processEditedMessage($edited_message) { |
||
244 | |||
245 | /** |
||
246 | * \brief Called every new post in the channel where the bot is in. |
||
247 | * \details Override it to script the bot answer for each post sent in a channel. |
||
248 | * <code>$chat_id</code> set inside of this function. |
||
249 | * @param $post The message sent in the channel. |
||
250 | */ |
||
251 | protected function processChannelPost($post) { |
||
254 | |||
255 | /** |
||
256 | * \brief Called every time a post get edited in the channel where the bot is in. |
||
257 | * \details Override it to script the bot answer for each post edited in a channel. |
||
258 | * <code>$chat_id</code> set inside of this function. |
||
259 | * @param $post The message edited in the channel. |
||
260 | */ |
||
261 | protected function processEditedChannelPost($edited_post) { |
||
264 | |||
265 | /** @} */ |
||
266 | |||
267 | } |
||
268 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: