1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBotFramework\Database; |
4
|
|
|
|
5
|
|
|
trait User { |
6
|
|
|
|
7
|
|
|
abstract function getChat($chat_id); |
|
|
|
|
8
|
|
|
|
9
|
|
|
abstract function setChatID($chat_id); |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* \addtogroup Bot Bot |
13
|
|
|
* @{ |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* \addtogroup Users-handle Users handling |
18
|
|
|
* \brief Handle bot users on the database. |
19
|
|
|
* @{ |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** Pdo connection to the database. */ |
23
|
|
|
public $pdo; |
24
|
|
|
|
25
|
|
|
/** \brief Table contaning bot users data in the sql database. */ |
26
|
|
|
public $user_table = '"User"'; |
27
|
|
|
|
28
|
|
|
/** \brief Name of the column that represents the user id in the sql database */ |
29
|
|
|
public $id_column = 'chat_id'; |
30
|
|
|
|
31
|
|
|
/** \brief Add a user to the database. |
32
|
|
|
* \details Add a user to the database in Bot::$user_table table and Bot::$id_column column using Bot::$pdo connection. |
33
|
|
|
* @param int $chat_id chat_id of the user to add. |
34
|
|
|
* @return bool True on success. |
35
|
|
|
*/ |
36
|
|
|
public function addUser($chat_id) : bool { |
37
|
|
|
|
38
|
|
|
// Is there database connection? |
39
|
|
|
if (!isset($this->pdo)) { |
40
|
|
|
|
41
|
|
|
throw new BotException("Database connection not set"); |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Create insertion query and initialize variable |
46
|
|
|
$query = "INSERT INTO $this->user_table ($this->id_column) VALUES (:chat_id)"; |
47
|
|
|
|
48
|
|
|
// Prepare the query |
49
|
|
|
$sth = $this->pdo->prepare($query); |
50
|
|
|
|
51
|
|
|
// Add the chat_id to the query |
52
|
|
|
$sth->bindParam(':chat_id', $chat_id); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
|
56
|
|
|
$sth->execute(); |
57
|
|
|
$success = true; |
58
|
|
|
|
59
|
|
|
} catch (PDOException $e) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
echo $e->getMessage(); |
62
|
|
|
|
63
|
|
|
$success = false; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Close statement |
68
|
|
|
$sth = null; |
|
|
|
|
69
|
|
|
|
70
|
|
|
// Return result |
71
|
|
|
return $success; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* \brief Broadcast a message to all user registred on the database. |
77
|
|
|
* \details Send a message to all users subscribed, change Bot::$user_table and Bot::$id_column to match your database structure is. |
78
|
|
|
* This method requires Bot::$pdo connection set. |
79
|
|
|
* All parameters are the same as CoreBot::sendMessage. |
80
|
|
|
* Because a limitation of Telegram Bot API the bot will have a delay after 20 messages sent in different chats. |
81
|
|
|
* @see CoreBot::sendMessage |
82
|
|
|
*/ |
83
|
|
|
public function broadcastMessage(string $text, string $reply_markup = null, string $parse_mode = 'HTML', bool $disable_web_preview = true, bool $disable_notification = false) { |
84
|
|
|
|
85
|
|
|
// Is there database connection? |
86
|
|
|
if (!isset($this->pdo)) { |
87
|
|
|
|
88
|
|
|
throw new BotException("Database connection not set"); |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Prepare the query to get all chat_id from the database |
93
|
|
|
$sth = $this->pdo->prepare("SELECT $this->id_column FROM $this->user_table"); |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
|
97
|
|
|
$sth->execute(); |
98
|
|
|
|
99
|
|
|
} catch (PDOException $e) { |
|
|
|
|
100
|
|
|
|
101
|
|
|
echo $e->getMessage(); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Iterate over all the row got |
106
|
|
|
while ($user = $sth->fetch()) { |
107
|
|
|
|
108
|
|
|
// Call getChat to know that this users haven't blocked the bot |
109
|
|
|
$user_data = $this->getChat($user[$this->id_column]); |
110
|
|
|
|
111
|
|
|
// Did they block it? |
112
|
|
|
if ($user_data !== false) { |
113
|
|
|
|
114
|
|
|
// Change the chat_id for the next API method |
115
|
|
|
$this->setChatID($user[$this->id_column]); |
116
|
|
|
|
117
|
|
|
// Send the message |
118
|
|
|
$this->sendMessage($text, $reply_markup, null, $parse_mode, $disable_web_preview, $disable_notification); |
|
|
|
|
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Close statement |
125
|
|
|
$sth = null; |
|
|
|
|
126
|
|
|
|
127
|
|
|
} |
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.