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