1 | <?php |
||
5 | trait LongPolling { |
||
6 | |||
7 | abstract public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60); |
||
8 | |||
9 | abstract protected function initCommands(); |
||
10 | |||
11 | /** |
||
12 | * \addtogroup Bot |
||
13 | * @{ |
||
14 | */ |
||
15 | |||
16 | /** |
||
17 | * \addtogroup LongPollingDatabase Long polling With Database |
||
18 | * \brief Use getUpdates saving and getting offset in redis/sql-database. |
||
19 | * @{ |
||
20 | */ |
||
21 | |||
22 | /** |
||
23 | * \brief (<i>Internal</i>)Get first update offset in redis. |
||
24 | * \details Called by getUpdatesRedis to get the offset saved in redis or to get it from telegram and save it in redis. |
||
25 | * @param $offset_key Name of the variable where the offset is saved on Redis |
||
26 | * @return Id of the first update to process. |
||
27 | */ |
||
28 | protected function getUpdateOffsetRedis(string $offset_key) : int { |
||
54 | |||
55 | /** |
||
56 | * \brief Get updates received by the bot, using redis to save and get the last offset. |
||
57 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
58 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
59 | * Each update is surrounded by a try/catch. |
||
60 | * @see getUpdates |
||
61 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
62 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
63 | * @param $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis |
||
64 | */ |
||
65 | public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') { |
||
104 | |||
105 | /** |
||
106 | * \brief (<i>Internal</i>) Get first update offset in database. |
||
107 | * \details Called by getUpdatesDatabase to get the offset saved in database or to get it from telegram and save it in database. |
||
108 | * @param $table_name Name of the table where offset is saved in the database |
||
109 | * @param $column_name Name of the column where the offset is saved in the database |
||
110 | * @return Id of the first update to process. |
||
111 | */ |
||
112 | protected function getUpdateOffsetDatabase(string $table_name, string $column_name) : int { |
||
147 | |||
148 | /** |
||
149 | * \brief Get updates received by the bot, using the sql database to store and get the last offset. |
||
150 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
151 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
152 | * Each update is surrounded by a try/catch. |
||
153 | * @see getUpdates |
||
154 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
155 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
156 | * @param $table_name <i>Optional</i>. Name of the table where offset is saved in the database |
||
157 | * @param $column_name <i>Optional</i>. Name of the column where the offset is saved in the database |
||
158 | */ |
||
159 | public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset') { |
||
200 | |||
201 | /** @} */ |
||
202 | |||
203 | /** @} */ |
||
204 | |||
205 | } |
||
206 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: