1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBotFramework\Core; |
4
|
|
|
|
5
|
|
|
trait LongPolling { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* \brief Get updates received by the bot, using redis to save and get the last offset. |
9
|
|
|
* \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
10
|
|
|
* Then it start an infinite loop where it process updates and update the offset on redis. |
11
|
|
|
* Each update is surrounded by a try/catch. |
12
|
|
|
* @see getUpdates |
13
|
|
|
* @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
14
|
|
|
* @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
15
|
|
|
* @param $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis |
16
|
|
|
*/ |
17
|
|
|
public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') { |
|
|
|
|
18
|
|
|
|
19
|
|
|
// Check redis connection |
20
|
|
|
if (!isset($this->redis)) { |
21
|
|
|
|
22
|
|
|
throw new BotException("Redis connection is not set"); |
23
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// If offset is already set in redis |
27
|
|
|
if ($this->redis->exists($variable_name)) { |
28
|
|
|
|
29
|
|
|
// just set $offset as the same value |
30
|
|
|
$offset = $this->redis->get($variable_name); |
|
|
|
|
31
|
|
|
|
32
|
|
View Code Duplication |
} else { |
|
|
|
|
33
|
|
|
// Else get the offset from the id from the first update received |
34
|
|
|
|
35
|
|
|
do { |
36
|
|
|
|
37
|
|
|
$update = $this->getUpdates(0, 1); |
|
|
|
|
38
|
|
|
|
39
|
|
|
} while (empty($update)); |
40
|
|
|
|
41
|
|
|
$offset = $update[0]['update_id']; |
42
|
|
|
|
43
|
|
|
$this->redis->set($variable_name, $offset); |
44
|
|
|
|
45
|
|
|
$update = null; |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->initBot(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
// Process all updates received |
52
|
|
|
while (true) { |
53
|
|
|
|
54
|
|
|
$updates = $this->getUpdates($offset, $limit, $timeout); |
|
|
|
|
55
|
|
|
|
56
|
|
|
// Parse all updates received |
57
|
|
|
foreach ($updates as $key => $update) { |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
|
61
|
|
|
$this->processUpdate($update); |
|
|
|
|
62
|
|
|
|
63
|
|
|
} catch (BotException $e) { |
|
|
|
|
64
|
|
|
|
65
|
|
|
echo $e->getMessage(); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Update the offset in redis |
72
|
|
|
$this->redis->set($variable_name, $offset + count($updates)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* \brief Get updates received by the bot, and hold the offset in $offset. |
79
|
|
|
* \details Get the update_id of the first update to parse, set it in $offset and |
80
|
|
|
* then it start an infinite loop where it processes updates and keep $offset on the update_id of the last update received. |
81
|
|
|
* Each processUpdate() method call is surrounded by a try/catch. |
82
|
|
|
* @see getUpdates |
83
|
|
|
* @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
84
|
|
|
* @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
85
|
|
|
*/ |
86
|
|
|
public function getUpdatesLocal(int $limit = 100, int $timeout = 60) { |
87
|
|
|
|
88
|
|
|
$update = []; |
|
|
|
|
89
|
|
|
|
90
|
|
|
// While there aren't updates to process |
91
|
|
|
do { |
92
|
|
|
|
93
|
|
|
// Get updates from telegram |
94
|
|
|
$update = $this->getUpdates(0, 1); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// While in the array received there aren't updates |
97
|
|
|
} while (empty($update)); |
98
|
|
|
|
99
|
|
|
// Set the offset to the first update recevied |
100
|
|
|
$offset = $update[0]['update_id']; |
101
|
|
|
|
102
|
|
|
$update = null; |
103
|
|
|
|
104
|
|
|
$this->initBot(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
// Process all updates |
107
|
|
|
while (true) { |
108
|
|
|
|
109
|
|
|
// Set parameter for the url call |
110
|
|
|
$parameters = [ |
111
|
|
|
'offset' => $offset, |
112
|
|
|
'limit' => $limit, |
113
|
|
|
'timeout' => $timeout |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$updates = $this->exec_curl_request($this->_api_url . 'getUpdates?' . http_build_query($parameters)); |
|
|
|
|
117
|
|
|
|
118
|
|
|
// Parse all update to receive |
119
|
|
|
foreach ($updates as $key => $update) { |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
|
123
|
|
|
// Process one at a time |
124
|
|
|
$this->processUpdate($update); |
|
|
|
|
125
|
|
|
|
126
|
|
|
} catch (BotException $e) { |
|
|
|
|
127
|
|
|
|
128
|
|
|
echo $e->getMessage(); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Update the offset |
135
|
|
|
$offset += sizeof($updates); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* \brief Get updates received by the bot, using the sql database to store and get the last offset. |
143
|
|
|
* \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
144
|
|
|
* Then it start an infinite loop where it process updates and update the offset on redis. |
145
|
|
|
* Each update is surrounded by a try/catch. |
146
|
|
|
* @see getUpdates |
147
|
|
|
* @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
148
|
|
|
* @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
149
|
|
|
* @param $table_name <i>Optional</i>. Name of the table where offset is saved in the database |
150
|
|
|
* @param $column_name <i>Optional</i>. Name of the column where the offset is saved in the database |
151
|
|
|
*/ |
152
|
|
|
public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset') { |
153
|
|
|
|
154
|
|
|
if (!isset($this->_database)) { |
155
|
|
|
|
156
|
|
|
throw new BotException("Database connection is not set"); |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Get the offset from the database |
161
|
|
|
$sth = $this->pdo->prepare('SELECT ' . $column_name . ' FROM ' . $table_name); |
|
|
|
|
162
|
|
|
|
163
|
|
|
try { |
164
|
|
|
|
165
|
|
|
$sth->execute(); |
166
|
|
|
|
167
|
|
|
} catch (PDOException $e) { |
|
|
|
|
168
|
|
|
|
169
|
|
|
echo $e->getMessage(); |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$offset = $sth->fetchColumn(); |
174
|
|
|
$sth = null; |
|
|
|
|
175
|
|
|
|
176
|
|
|
// Get the offset from the first update to update |
177
|
|
View Code Duplication |
if ($offset === false) { |
|
|
|
|
178
|
|
|
|
179
|
|
|
do { |
180
|
|
|
|
181
|
|
|
$update = $this->getUpdates(0, 1); |
|
|
|
|
182
|
|
|
|
183
|
|
|
} while (empty($update)); |
184
|
|
|
|
185
|
|
|
$offset = $update[0]['update_id']; |
186
|
|
|
|
187
|
|
|
$update = null; |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Prepare the query for updating the offset in the database |
192
|
|
|
$sth = $this->pdo->prepare('UPDATE "' . $table_name . '" SET "' . $column_name . '" = :new_offset'); |
193
|
|
|
|
194
|
|
|
$this->initBot(); |
|
|
|
|
195
|
|
|
|
196
|
|
|
while (true) { |
197
|
|
|
|
198
|
|
|
$updates = $this->getUpdates($offset, $limit, $timeout); |
|
|
|
|
199
|
|
|
|
200
|
|
|
foreach ($updates as $key => $update) { |
201
|
|
|
|
202
|
|
|
try { |
203
|
|
|
|
204
|
|
|
$this->processUpdate($update); |
|
|
|
|
205
|
|
|
|
206
|
|
|
} catch (BotException $e) { |
|
|
|
|
207
|
|
|
|
208
|
|
|
echo $e->getMessage(); |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// Update the offset on the database |
215
|
|
|
$sth->bindParam(':new_offset', $offset + sizeof($updates)); |
216
|
|
|
$sth->execute(); |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.