1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBotFramework\Database; |
4
|
|
|
|
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 { |
29
|
|
|
|
30
|
|
|
// If offset is already set in redis |
31
|
|
|
if ($this->redis->exists($offset_key)) { |
32
|
|
|
|
33
|
|
|
// return the value saved |
34
|
|
|
return $this->redis->get($offset_key); |
|
|
|
|
35
|
|
|
|
36
|
|
|
// Else get the offset from the id from the first update received |
37
|
|
|
} else { |
38
|
|
|
|
39
|
|
|
do { |
40
|
|
|
|
41
|
|
|
$update = $this->getUpdates(0, 1); |
42
|
|
|
|
43
|
|
|
} while (empty($update)); |
44
|
|
|
|
45
|
|
|
$offset = $update[0]['update_id']; |
46
|
|
|
|
47
|
|
|
$this->redis->set($offset_key, $offset); |
48
|
|
|
|
49
|
|
|
return $offset_key; |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
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') { |
66
|
|
|
|
67
|
|
|
// Check redis connection |
68
|
|
|
if (!isset($this->redis)) { |
69
|
|
|
|
70
|
|
|
throw new BotException("Redis connection is not set"); |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$offset = $this->getUpdateOffsetRedis(); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->initCommands(); |
77
|
|
|
|
78
|
|
|
// Process all updates received |
79
|
|
|
while (true) { |
80
|
|
|
|
81
|
|
|
$updates = $this->getUpdates($offset, $limit, $timeout); |
82
|
|
|
|
83
|
|
|
// Parse all updates received |
84
|
|
|
foreach ($updates as $key => $update) { |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
|
88
|
|
|
$this->processUpdate($update); |
|
|
|
|
89
|
|
|
|
90
|
|
|
} catch (BotException $e) { |
|
|
|
|
91
|
|
|
|
92
|
|
|
echo $e->getMessage(); |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Update the offset in redis |
99
|
|
|
$this->redis->set($offset_key, $offset + count($updates)); |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
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 { |
113
|
|
|
|
114
|
|
|
// Get the offset from the database |
115
|
|
|
$sth = $this->pdo->prepare('SELECT ' . $column_name . ' FROM ' . $table_name); |
|
|
|
|
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
|
119
|
|
|
$sth->execute(); |
120
|
|
|
|
121
|
|
|
} catch (PDOException $e) { |
|
|
|
|
122
|
|
|
|
123
|
|
|
echo $e->getMessage(); |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$offset = $sth->fetchColumn(); |
128
|
|
|
|
129
|
|
|
$sth = null; |
|
|
|
|
130
|
|
|
|
131
|
|
|
// Get the offset from the first update to update |
132
|
|
|
if ($offset === false) { |
133
|
|
|
|
134
|
|
|
do { |
135
|
|
|
|
136
|
|
|
$update = $this->getUpdates(0, 1); |
137
|
|
|
|
138
|
|
|
} while (empty($update)); |
139
|
|
|
|
140
|
|
|
$offset = $update[0]['update_id']; |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $offset; |
145
|
|
|
|
146
|
|
|
} |
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') { |
160
|
|
|
|
161
|
|
|
if (!isset($this->_database)) { |
162
|
|
|
|
163
|
|
|
throw new BotException("Database connection is not set"); |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Get offset from database |
168
|
|
|
$this->getUpdateOffsetDatabase($table_name, $column_name); |
169
|
|
|
|
170
|
|
|
$this->initCommands(); |
171
|
|
|
|
172
|
|
|
// Prepare the query for updating the offset in the database |
173
|
|
|
$sth = $this->pdo->prepare('UPDATE "' . $table_name . '" SET "' . $column_name . '" = :new_offset'); |
174
|
|
|
|
175
|
|
|
while (true) { |
176
|
|
|
|
177
|
|
|
$updates = $this->getUpdates($offset, $limit, $timeout); |
|
|
|
|
178
|
|
|
|
179
|
|
|
foreach ($updates as $key => $update) { |
180
|
|
|
|
181
|
|
|
try { |
182
|
|
|
|
183
|
|
|
$this->processUpdate($update); |
|
|
|
|
184
|
|
|
|
185
|
|
|
} catch (BotException $e) { |
|
|
|
|
186
|
|
|
|
187
|
|
|
echo $e->getMessage(); |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Update the offset on the database |
194
|
|
|
$sth->bindParam(':new_offset', $offset + sizeof($updates)); |
195
|
|
|
$sth->execute(); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
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: