-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathNewchatmembersCommand.php
More file actions
180 lines (148 loc) · 4.68 KB
/
NewchatmembersCommand.php
File metadata and controls
180 lines (148 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* This file is part of the PHP Telegram Support Bot.
*
* (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpTelegramBot\Core\Commands\SystemCommands;
use PhpTelegramBot\Core\Commands\SystemCommand;
use PhpTelegramBot\Core\Entities\ChatMember;
use PhpTelegramBot\Core\Entities\ServerResponse;
use PhpTelegramBot\Core\Entities\User;
use PhpTelegramBot\Core\Exception\TelegramException;
use PhpTelegramBot\Core\Request;
use TelegramBot\SupportBot\Helpers;
/**
* New chat members command
*/
class NewchatmembersCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'newchatmembers';
/**
* @var string
*/
protected $description = 'New Chat Members';
/**
* @var string
*/
protected $version = '0.4.0';
/**
* @var int
*/
private $chat_id;
/**
* @var int
*/
private $user_id;
/**
* @var string
*/
private $group_name = 'PHP Telegram Support Bot';
/**
* @inheritdoc
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$this->chat_id = $message->getChat()->getId();
$this->user_id = $message->getFrom()->getId();
$this->group_name = $message->getChat()->getTitle();
['users' => $new_users, 'bots' => $new_bots] = $this->getNewUsersAndBots();
// Kick bots if they weren't added by an admin.
$this->kickDisallowedBots($new_bots);
return $this->refreshWelcomeMessage($new_users);
}
/**
* Remove existing and send new welcome message.
*
* @param array $new_users
*
* @return ServerResponse
* @throws TelegramException
*/
private function refreshWelcomeMessage(array $new_users): ServerResponse
{
if (empty($new_users)) {
return Request::emptyResponse();
}
$new_users_text = implode(', ', array_map(static function (User $new_user) {
return '<a href="tg://user?id=' . $new_user->getId() . '">' . filter_var($new_user->getFirstName(), FILTER_SANITIZE_SPECIAL_CHARS) . '</a>';
}, $new_users));
$text = "Welcome {$new_users_text} to the <b>{$this->group_name}</b> group\n";
$text .= 'Please remember that this is <b>NOT</b> the Telegram Support Chat.' . PHP_EOL;
$text .= 'Read the <a href="https://t.me/PHP_Telegram_Bot_Support/5526">Rules</a> that apply here.';
$welcome_message_sent = $this->replyToChat($text, ['parse_mode' => 'HTML', 'disable_web_page_preview' => true]);
if (!$welcome_message_sent->isOk()) {
return Request::emptyResponse();
}
$welcome_message = $welcome_message_sent->getResult();
$new_message_id = $welcome_message->getMessageId();
$chat_id = $welcome_message->getChat()->getId();
if ($new_message_id && $chat_id) {
Helpers::saveLatestWelcomeMessage($new_message_id);
Helpers::deleteOldWelcomeMessages();
}
return $welcome_message_sent;
}
/**
* Check if the bot has been added by an admin.
*
* @return bool
*/
private function isUserAllowedToAddBot(): bool
{
$chat_member = Request::getChatMember([
'chat_id' => $this->chat_id,
'user_id' => $this->user_id,
])->getResult();
if ($chat_member instanceof ChatMember) {
return in_array($chat_member->getStatus(), ['creator', 'administrator'], true);
}
return false;
}
/**
* Get an array of all newly added users and bots.
*
* @return array
*/
private function getNewUsersAndBots(): array
{
$users = [];
$bots = [];
foreach ($this->getMessage()->getNewChatMembers() as $member) {
if ($member->getIsBot()) {
$bots[] = $member;
continue;
}
$users[] = $member;
}
return compact('users', 'bots');
}
/**
* Kick bots that weren't added by an admin.
*
* @todo: Maybe notify the admins / user that tried to add the bot(s)?
*
* @param array $bots
*/
private function kickDisallowedBots(array $bots): void
{
if (empty($bots) || $this->isUserAllowedToAddBot()) {
return;
}
foreach ($bots as $bot) {
Request::kickChatMember([
'chat_id' => $this->chat_id,
'user_id' => $bot->getId(),
]);
}
}
}