|
13 | 13 |
|
14 | 14 | namespace Longman\TelegramBot\Commands\UserCommands;
|
15 | 15 |
|
| 16 | +use LitEmoji\LitEmoji; |
16 | 17 | 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; |
17 | 22 | use Longman\TelegramBot\Entities\ServerResponse;
|
| 23 | +use Longman\TelegramBot\Request; |
18 | 24 |
|
19 | 25 | /**
|
20 | 26 | * User "/rules" command
|
@@ -46,28 +52,102 @@ class RulesCommand extends UserCommand
|
46 | 52 | */
|
47 | 53 | protected $private_only = true;
|
48 | 54 |
|
| 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 | + |
49 | 105 | /**
|
50 | 106 | * @inheritdoc
|
51 | 107 | */
|
52 | 108 | public function execute(): ServerResponse
|
53 | 109 | {
|
54 |
| - $text = <<<EOT |
| 110 | + $text = " |
55 | 111 | Rules: `English only | No Spamming or Nonsense Posting | No Bots`
|
56 | 112 |
|
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. |
59 | 115 |
|
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. |
62 | 118 |
|
63 |
| -¬ **No Bots** |
| 119 | +**:robot: No Bots** |
64 | 120 | Please do not add a Bot inside this Chat without asking the Admins first. Feel free to mention the Bot in a Message
|
65 | 121 |
|
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 | + ]; |
68 | 129 |
|
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 | + } |
70 | 136 |
|
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); |
72 | 152 | }
|
73 | 153 | }
|
0 commit comments