1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Database; |
20
|
|
|
|
21
|
|
|
use PhpBotFramework\Exceptions\BotException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* \class LongPolling |
25
|
|
|
* \brief Use getUpdates saving and getting offset in Redis and database. |
26
|
|
|
*/ |
27
|
|
|
trait LongPolling |
28
|
|
|
{ |
29
|
|
|
abstract public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60); |
30
|
|
|
|
31
|
|
|
abstract protected function initCommands(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* \addtogroup Database |
35
|
|
|
* @{ |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @internal |
40
|
|
|
* \brief Get first update offset in Redis. |
41
|
|
|
* \details Called by getUpdatesRedis in order to get the saved offset in Redis or retrieve it from Telegram and save it. |
42
|
|
|
* @param string $offset_key Name of the variable where the offset is saved on Redis. |
43
|
|
|
* @return int Id of the first update to process. |
44
|
|
|
*/ |
45
|
|
|
protected function getUpdateOffsetRedis(string $offset_key) : int |
46
|
|
|
{ |
47
|
|
|
$redis = $this->getRedis(); |
|
|
|
|
48
|
|
|
if ($redis->exists($offset_key)) { |
49
|
|
|
return $redis->get($offset_key); |
50
|
|
|
} else { |
51
|
|
|
// Get offset by first update. |
52
|
|
|
do { |
53
|
|
|
$update = $this->getUpdates(0, 1); |
54
|
|
|
} while (empty($update)); |
55
|
|
|
|
56
|
|
|
$offset = $update[0]['update_id']; |
57
|
|
|
|
58
|
|
|
$redis->set($offset_key, $offset); |
59
|
|
|
return $offset; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* \brief Get updates received by the bot, and use Redis to save and get the last offset. |
65
|
|
|
* \details It check if an offset exists on Redis: then get it or call getUpdates to set it. |
66
|
|
|
* Then it start a loop where it process updates and update the offset on Redis. |
67
|
|
|
* Each update is surrounded by a try/catch. |
68
|
|
|
* @see getUpdates |
69
|
|
|
* @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
70
|
|
|
* @param int $timeout <i>Optional</i>. Timeout (in seconds) for long polling. |
71
|
|
|
* @param string $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis. |
72
|
|
|
*/ |
73
|
|
|
public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') |
74
|
|
|
{ |
75
|
|
|
$redis = $this->getRedis(); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$offset = $this->getUpdateOffsetRedis($offset_key); |
78
|
|
|
$this->initCommands(); |
79
|
|
|
|
80
|
|
|
// Process all updates received |
81
|
|
View Code Duplication |
while (true) { |
|
|
|
|
82
|
|
|
$updates = $this->getUpdates($offset, $limit, $timeout); |
83
|
|
|
|
84
|
|
|
foreach ($updates as $key => $update) { |
85
|
|
|
try { |
86
|
|
|
$this->processUpdate($update); |
|
|
|
|
87
|
|
|
} catch (BotException $e) { |
88
|
|
|
echo $e->getMessage(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$offset = $offset + count($updates); |
93
|
|
|
// Update the offset in redis |
94
|
|
|
$this->redis->set($offset_key, $offset); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @internal |
100
|
|
|
* \brief Get first update offset in database. |
101
|
|
|
* \details Called by getUpdatesDatabase to get the offset saved in database. |
102
|
|
|
* If the offset is not saved: it retrieve the offset from Telegram and save it on the database. |
103
|
|
|
* @param string $table_name Name of the table where offset is saved in the database. |
104
|
|
|
* @param string $column_name Name of the column where the offset is saved in the database. |
105
|
|
|
* @return int Id of the first update to process. |
106
|
|
|
*/ |
107
|
|
|
protected function getUpdateOffsetDatabase(string $table_name, string $column_name) : int |
108
|
|
|
{ |
109
|
|
|
$sth = $this->pdo->prepare('SELECT ' . $column_name . ' FROM ' . $table_name); |
|
|
|
|
110
|
|
|
|
111
|
|
|
try { |
112
|
|
|
$sth->execute(); |
113
|
|
|
} catch (\PDOException $e) { |
114
|
|
|
echo $e->getMessage(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$offset = $sth->fetchColumn(); |
118
|
|
|
|
119
|
|
|
$sth = null; |
|
|
|
|
120
|
|
|
|
121
|
|
|
// Get the offset from the first update to update. |
122
|
|
|
if ($offset === false) { |
123
|
|
|
do { |
124
|
|
|
$update = $this->getUpdates(0, 1); |
125
|
|
|
} while (empty($update)); |
126
|
|
|
|
127
|
|
|
$offset = $update[0]['update_id']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $offset; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* \brief Get updates received by the bot, using the SQL database to store and get the last offset. |
135
|
|
|
* \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
136
|
|
|
* Then it start a loop where it process updates and update the offset on Redis. |
137
|
|
|
* Each update is surrounded by a try/catch. |
138
|
|
|
* @see getUpdates |
139
|
|
|
* @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
140
|
|
|
* @param int $timeout <i>Optional</i>. Timeout (in seconds) for long polling. |
141
|
|
|
* @param string $table_name <i>Optional</i>. Name of the table where offset is saved in the database. |
142
|
|
|
* @param string $column_name <i>Optional</i>. Name of the column where the offset is saved in the database. |
143
|
|
|
*/ |
144
|
|
|
public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset') |
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
if (!isset($this->_database)) { |
148
|
|
|
throw new BotException("Database connection is not set"); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$offset = $this->getUpdateOffsetDatabase($table_name, $column_name); |
152
|
|
|
$this->initCommands(); |
153
|
|
|
|
154
|
|
|
// Prepare the query for updating the offset in the database |
155
|
|
|
$sth = $this->pdo->prepare('UPDATE ' . $table_name . ' SET ' . $column_name . ' = :new_offset'); |
156
|
|
|
|
157
|
|
View Code Duplication |
while (true) { |
|
|
|
|
158
|
|
|
$updates = $this->getUpdates($offset, $limit, $timeout); |
159
|
|
|
|
160
|
|
|
foreach ($updates as $key => $update) { |
161
|
|
|
try { |
162
|
|
|
$this->processUpdate($update); |
|
|
|
|
163
|
|
|
} catch (BotException $e) { |
164
|
|
|
echo $e->getMessage(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$offset = $offset + count($updates); |
169
|
|
|
// Update the offset on the database |
170
|
|
|
$sth->bindParam(':new_offset', $offset); |
171
|
|
|
$sth->execute(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** @} */ |
176
|
|
|
} |
177
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.