Skip to content

Commit fcbdbc3

Browse files
committed
Initial implementation of the rules approval, to allow posting in the group.
1 parent c31d5c0 commit fcbdbc3

6 files changed

+121
-23
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
55

66
## [Unreleased]
77
### Added
8+
- Rules must be agreed to before allowing a user to post in the group.
89
### Changed
910
### Deprecated
1011
### Removed

commands/CallbackqueryCommand.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Longman\TelegramBot\Commands\SystemCommand;
1515
use Longman\TelegramBot\Commands\UserCommands\DonateCommand;
16+
use Longman\TelegramBot\Commands\UserCommands\RulesCommand;
1617
use Longman\TelegramBot\Entities\ServerResponse;
1718

1819
/**
@@ -47,18 +48,17 @@ class CallbackqueryCommand extends SystemCommand
4748
public function execute(): ServerResponse
4849
{
4950
$callback_query = $this->getCallbackQuery();
50-
parse_str($callback_query->getData(), $data);
51+
parse_str($callback_query->getData(), $callback_data);
5152

52-
if ('donate' === $data['command']) {
53+
if ('donate' === $callback_data['command']) {
5354
DonateCommand::createPaymentInvoice(
5455
$callback_query->getFrom()->getId(),
55-
$data['amount'],
56-
$data['currency']
56+
$callback_data['amount'],
57+
$callback_data['currency']
5758
);
5859

59-
return $callback_query->answer([
60-
'text' => 'Awesome, an invoice has been sent to you.',
61-
]);
60+
if ('rules' === $callback_data['command']) {
61+
return RulesCommand::handleCallbackQuery($callback_query, $callback_data);
6262
}
6363

6464
return $callback_query->answer();

commands/NewchatmembersCommand.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
namespace Longman\TelegramBot\Commands\SystemCommands;
1515

16+
use LitEmoji\LitEmoji;
1617
use Longman\TelegramBot\Commands\SystemCommand;
1718
use Longman\TelegramBot\Entities\ChatMember;
19+
use Longman\TelegramBot\Entities\InlineKeyboard;
1820
use Longman\TelegramBot\Entities\ServerResponse;
1921
use Longman\TelegramBot\Entities\User;
2022
use Longman\TelegramBot\Exception\TelegramException;
@@ -39,7 +41,7 @@ class NewchatmembersCommand extends SystemCommand
3941
/**
4042
* @var string
4143
*/
42-
protected $version = '0.4.0';
44+
protected $version = '0.5.0';
4345

4446
/**
4547
* @var int
@@ -94,11 +96,20 @@ private function refreshWelcomeMessage(array $new_users): ServerResponse
9496
return '<a href="tg://user?id=' . $new_user->getId() . '">' . filter_var($new_user->getFirstName(), FILTER_SANITIZE_SPECIAL_CHARS) . '</a>';
9597
}, $new_users));
9698

97-
$text = "Welcome {$new_users_text} to the <b>{$this->group_name}</b> group\n";
98-
$text .= 'Please remember that this is <b>NOT</b> the Telegram Support Chat.' . PHP_EOL;
99-
$text .= 'Read the <a href="https://t.me/PHP_Telegram_Support_Bot?start=rules">Rules</a> that apply here.';
100-
101-
$welcome_message_sent = $this->replyToChat($text, ['parse_mode' => 'HTML', 'disable_web_page_preview' => true]);
99+
$text = ":wave: Welcome {$new_users_text} to the <b>{$this->group_name}</b> group\n\n";
100+
$text .= 'Please read and agree to the rules before posting here, thank you!';
101+
102+
$welcome_message_sent = $this->replyToChat(
103+
LitEmoji::encodeUnicode($text),
104+
[
105+
'parse_mode' => 'HTML',
106+
'disable_web_page_preview' => true,
107+
'disable_notification' => true,
108+
'reply_markup' => new InlineKeyboard([
109+
['text' => LitEmoji::encodeUnicode(':orange_book: Read the Rules'), 'url' => 'https://t.me/PHP_Telegram_Support_Bot?start=rules'],
110+
]),
111+
]
112+
);
102113
if (!$welcome_message_sent->isOk()) {
103114
return Request::emptyResponse();
104115
}

commands/RulesCommand.php

+90-10
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313

1414
namespace Longman\TelegramBot\Commands\UserCommands;
1515

16+
use LitEmoji\LitEmoji;
1617
use Longman\TelegramBot\Commands\UserCommand;
18+
use Longman\TelegramBot\DB;
19+
use Longman\TelegramBot\Entities\CallbackQuery;
20+
use Longman\TelegramBot\Entities\ChatPermissions;
21+
use Longman\TelegramBot\Entities\InlineKeyboard;
1722
use Longman\TelegramBot\Entities\ServerResponse;
23+
use Longman\TelegramBot\Request;
1824

1925
/**
2026
* User "/rules" command
@@ -46,28 +52,102 @@ class RulesCommand extends UserCommand
4652
*/
4753
protected $private_only = true;
4854

55+
public static function handleCallbackQuery(CallbackQuery $callback_query, array $callback_data): ?ServerResponse
56+
{
57+
if ('agree' === $callback_data['action'] ?? null) {
58+
$message = $callback_query->getMessage();
59+
$chat_id = $message->getChat()->getId();
60+
$clicked_user_id = $callback_query->getFrom()->getId();
61+
62+
// If the user is already activated, keep the initial activation date.
63+
$activated = DB::getPdo()->prepare("
64+
UPDATE " . TB_USER . "
65+
SET `activated_at` = ?
66+
WHERE `id` = ?
67+
AND `activated_at` IS NULL
68+
")->execute([date('Y-m-d H:i:s'), $clicked_user_id]);
69+
70+
if (!$activated) {
71+
return $callback_query->answer([
72+
'text' => 'Something went wrong, please try again later.',
73+
'show_alert' => true,
74+
]);
75+
}
76+
77+
Request::editMessageReplyMarkup([
78+
'chat_id' => $chat_id,
79+
'message_id' => $message->getMessageId(),
80+
'reply_markup' => new InlineKeyboard([
81+
['text' => LitEmoji::encodeUnicode(':white_check_mark: Ok! Go to Bot Support group...'), 'url' => 'https://t.me/PHP_Telegram_Bot_Support'],
82+
]),
83+
]);
84+
85+
Request::restrictChatMember([
86+
'chat_id' => getenv('TG_SUPPORT_GROUP_ID'),
87+
'user_id' => $clicked_user_id,
88+
'permissions' => new ChatPermissions([
89+
'can_send_messages' => true,
90+
'can_send_media_messages' => true,
91+
'can_add_web_page_previews' => true,
92+
'can_invite_users' => true,
93+
]),
94+
]);
95+
96+
return $callback_query->answer([
97+
'text' => 'Thanks for agreeing to the rules. You may now post in the support group.',
98+
'show_alert' => true,
99+
]);
100+
}
101+
102+
return $callback_query->answer();
103+
}
104+
49105
/**
50106
* @inheritdoc
51107
*/
52108
public function execute(): ServerResponse
53109
{
54-
$text = <<<EOT
110+
$text = "
55111
Rules: `English only | No Spamming or Nonsense Posting | No Bots`
56112
57-
¬ **English only**
58-
Please keep your conversations in english inside this chatroom, otherwise your message will be deleted
113+
**:uk: English only**
114+
Please keep your conversations in English inside this chatroom, otherwise your message will be deleted.
59115
60-
¬ **No Spamming or Nonsense Posting**
61-
Don't spam Stickers or send Messages with useless Content. When repeated you may be kicked or banned
116+
**:do_not_litter: No Spamming or Nonsense Posting**
117+
Don't spam or send Messages with useless Content. When repeated you may be kicked or banned.
62118
63-
¬ **No Bots**
119+
**:robot: No Bots**
64120
Please do not add a Bot inside this Chat without asking the Admins first. Feel free to mention the Bot in a Message
65121
66-
Also keep in mind that this PHP Telegram Bot Support Chat applies only for the PHP Telegram Bot library
67-
https://github.com/php-telegram-bot/core
122+
Also keep in mind that the [PHP Telegram Bot Support Chat](https://t.me/PHP_Telegram_Bot_Support) applies only for the [PHP Telegram Bot library](https://github.com/php-telegram-bot/core).
123+
";
124+
125+
$data = [
126+
'parse_mode' => 'markdown',
127+
'disable_web_page_preview' => true,
128+
];
68129

69-
EOT;
130+
if (!self::userHasAgreedToRules($this->getMessage()->getFrom()->getId())) {
131+
$text .= PHP_EOL . 'You **must agree** to these rules to post in the support group. Simply click the button below.';
132+
$data['reply_markup'] = new InlineKeyboard([
133+
['text' => LitEmoji::encodeUnicode(':+1: I Agree to the Rules'), 'callback_data' => 'command=rules&action=agree'],
134+
]);
135+
}
70136

71-
return $this->replyToChat($text, ['parse_mode' => 'markdown']);
137+
return $this->replyToChat(LitEmoji::encodeUnicode($text), $data);
138+
}
139+
140+
protected static function userHasAgreedToRules(int $user_id): bool
141+
{
142+
$statement = DB::getPdo()->prepare('
143+
SELECT `activated_at`
144+
FROM `' . TB_USER . '`
145+
WHERE `id` = ?
146+
AND `activated_at` IS NOT NULL
147+
');
148+
$statement->execute([$user_id]);
149+
$agreed = $statement->fetchAll(\PDO::FETCH_COLUMN, 0);
150+
151+
return !empty($agreed);
72152
}
73153
}

commands/StartCommand.php

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class StartCommand extends SystemCommand
5353
*/
5454
public function execute(): ServerResponse
5555
{
56+
if ('activate' === $this->getMessage()->getText(true)) {
57+
return $this->getTelegram()->executeCommand('activate');
58+
}
59+
5660
if ('rules' === $this->getMessage()->getText(true)) {
5761
return $this->getTelegram()->executeCommand('rules');
5862
}

structure.sql

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ CREATE TABLE IF NOT EXISTS `simple_options` (
88
PRIMARY KEY (`id`),
99
UNIQUE KEY `name` (`name`)
1010
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
11+
12+
ALTER TABLE `user` ADD COLUMN `activated_at` TIMESTAMP NULL COMMENT 'Timestamp when the user has agreed to the rules and has been allowed to post in the support group.';

0 commit comments

Comments
 (0)