1 | <?php |
||
5 | trait LongPolling { |
||
6 | |||
7 | /** |
||
8 | * \addtogroup LongPollingDatabase Long polling With Database |
||
9 | * \brief Use getUpdates saving and getting offset in redis/sql-database. |
||
10 | * @{ |
||
11 | */ |
||
12 | |||
13 | /** |
||
14 | * \brief (<i>Internal</i>)Get first update offset in redis. |
||
15 | * \details Called by getUpdatesRedis to get the offset saved in redis or to get it from telegram and save it in redis. |
||
16 | * @param $offset_key Name of the variable where the offset is saved on Redis |
||
17 | * @return Id of the first update to process. |
||
18 | */ |
||
19 | protected function getUpdateOffsetRedis(string $offset_key) : int { |
||
45 | |||
46 | /** |
||
47 | * \brief Get updates received by the bot, using redis to save and get the last offset. |
||
48 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
49 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
50 | * Each update is surrounded by a try/catch. |
||
51 | * @see getUpdates |
||
52 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
53 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
54 | * @param $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis |
||
55 | */ |
||
56 | public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') { |
||
99 | |||
100 | /** |
||
101 | * \brief (<i>Internal</i>) Get first update offset in database. |
||
102 | * \details Called by getUpdatesDatabase to get the offset saved in database or to get it from telegram and save it in database. |
||
103 | * @param $table_name Name of the table where offset is saved in the database |
||
104 | * @param $column_name Name of the column where the offset is saved in the database |
||
105 | * @return Id of the first update to process. |
||
106 | */ |
||
107 | protected function getUpdateOffsetDatabase(string $table_name, string $column_name) : int { |
||
141 | |||
142 | /** |
||
143 | * \brief Get updates received by the bot, using the sql database to store and get the last offset. |
||
144 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
145 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
146 | * Each update is surrounded by a try/catch. |
||
147 | * @see getUpdates |
||
148 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
149 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
150 | * @param $table_name <i>Optional</i>. Name of the table where offset is saved in the database |
||
151 | * @param $column_name <i>Optional</i>. Name of the column where the offset is saved in the database |
||
152 | */ |
||
153 | public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset') { |
||
199 | |||
200 | /** @} */ |
||
201 | |||
202 | } |
||
203 |
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: