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
|
|
|
* \addtogroup Modules |
25
|
|
|
* @{ |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** \class User |
29
|
|
|
*/ |
30
|
|
|
trait User |
31
|
|
|
{ |
32
|
|
|
/** @} */ |
33
|
|
|
|
34
|
|
|
abstract function getChat($chat_id); |
|
|
|
|
35
|
|
|
|
36
|
|
|
abstract function setChatID($chat_id); |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** PDO connection to the database. */ |
39
|
|
|
public $pdo; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* \addtogroup Bot Bot |
43
|
|
|
* @{ |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* \addtogroup Users-handle Users handling |
48
|
|
|
* \brief Handle bot users on the database. |
49
|
|
|
* @{ |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
/** \brief Table contaning bot users data in the SQL database. */ |
53
|
|
|
public $user_table = '"User"'; |
54
|
|
|
|
55
|
|
|
/** \brief Name of the column that represents the user id in the sql database */ |
56
|
|
|
public $id_column = 'chat_id'; |
57
|
|
|
|
58
|
|
|
/** \brief Add a user to the database. |
59
|
|
|
* \details Add a user to the database in Bot::$user_table table and Bot::$id_column column using Bot::$pdo connection. |
60
|
|
|
* @param string|int $chat_id chat ID of the user to add. |
61
|
|
|
* @return bool True on success. |
62
|
|
|
*/ |
63
|
|
|
public function addUser($chat_id) : bool |
64
|
|
|
{ |
65
|
|
|
if (!isset($this->pdo)) { |
66
|
|
|
throw new BotException("Database connection not set"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Create insertion query and initialize variable |
70
|
|
|
$query = "INSERT INTO $this->user_table ($this->id_column) VALUES (:chat_id)"; |
71
|
|
|
|
72
|
|
|
$sth = $this->pdo->prepare($query); |
73
|
|
|
$sth->bindParam(':chat_id', $chat_id); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$sth->execute(); |
77
|
|
|
$success = true; |
78
|
|
|
} catch (\PDOException $e) { |
79
|
|
|
echo $e->getMessage(); |
80
|
|
|
|
81
|
|
|
$success = false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$sth = null; |
|
|
|
|
85
|
|
|
return $success; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* \brief Send a message to every user available on the database. |
90
|
|
|
* \details Send a message to all subscribed users, change Bot::$user_table and Bot::$id_column to match your database structure. |
91
|
|
|
* This method requires Bot::$pdo connection set. |
92
|
|
|
* All parameters are the same as CoreBot::sendMessage. |
93
|
|
|
* Because a limitation of Telegram Bot API the bot will have a delay after 20 messages sent in different chats. |
94
|
|
|
* @return int How many messages were sent. |
95
|
|
|
* @see CoreBot::sendMessage |
96
|
|
|
*/ |
97
|
1 |
|
public function broadcastMessage( |
98
|
|
|
string $text, |
99
|
|
|
string $reply_markup = null, |
100
|
|
|
string $parse_mode = 'HTML', |
101
|
|
|
bool $disable_web_preview = true, |
102
|
|
|
bool $disable_notification = false |
103
|
|
|
) : int { |
104
|
1 |
|
if (!isset($this->pdo)) { |
105
|
|
|
throw new BotException("Database connection not set"); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$sth = $this->pdo->prepare("SELECT $this->id_column FROM $this->user_table"); |
109
|
|
|
|
110
|
|
|
try { |
111
|
1 |
|
$sth->execute(); |
112
|
1 |
|
} catch (\PDOException $e) { |
113
|
1 |
|
echo $e->getMessage(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Iterate over all the row got |
117
|
1 |
|
while ($user = $sth->fetch()) { |
118
|
|
|
$user_data = $this->getChat($user[$this->id_column]); |
119
|
|
|
|
120
|
|
|
if ($user_data !== false) { |
121
|
|
|
// Change the chat_id for the next API method |
122
|
|
|
$this->setChatID($user[$this->id_column]); |
123
|
|
|
$this->sendMessage($text, $reply_markup, null, $parse_mode, $disable_web_preview, $disable_notification); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return $sth->rowCount(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** @} */ |
131
|
|
|
|
132
|
|
|
/** @} */ |
133
|
|
|
} |
134
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.