Advertisement
Guest User

Untitled

a guest
Aug 31st, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.68 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Telegram\Commands;
  4.  
  5. use App\Models\User;
  6. use App\Services\MenuService;
  7. use App\Services\SubscriptionService;
  8. use Egulias\EmailValidator\EmailValidator;
  9. use Egulias\EmailValidator\Validation\RFCValidation;
  10. use Longman\TelegramBot\Conversation;
  11. use Longman\TelegramBot\Entities\Keyboard;
  12. use Longman\TelegramBot\Entities\KeyboardButton;
  13. use Longman\TelegramBot\Request;
  14.  
  15. class RequestDataCommand extends GenericSystemCommand {
  16.  
  17.     public function execute()
  18.     {
  19.         $message = $this->getMessage();
  20.         $chat    = $message->getChat();
  21.         $user    = $this->getUser();
  22.         $text    = trim($message->getText(true));
  23.         $chat_id = $chat->getId();
  24.  
  25.         $data = [
  26.             'chat_id' => $chat_id,
  27.         ];
  28.  
  29.         $conversation = new Conversation($user->id, $chat->getId(), 'reqdata');
  30.  
  31.         $notes = &$conversation->notes;
  32.         !is_array($notes) && $notes = [];
  33.  
  34.         $state = 0;
  35.         if (isset($notes['state'])) {
  36.             $state = $notes['state'];
  37.         }
  38.  
  39.         $result = Request::emptyResponse();
  40.  
  41.         switch ($state) {
  42.             case 0:
  43.                 if($user->email !== null || $user->phone !== null) {
  44.                     return Request::sendMessage([
  45.                         'chat_id' => $chat_id,
  46.                         'text' => 'Данные уже добавлены. Для сброса данных используйте /reset'
  47.                     ]);
  48.                 }
  49.  
  50.                 $data['text'] = <<<REQTEXT
  51. Напишите, пожалуйста, ваш e-mail (необходим для проверки оплаты) или нажмите “поделиться контактом”
  52. REQTEXT;
  53.                 $data['reply_markup'] = (new Keyboard(
  54.                     (new KeyboardButton('Поделиться контактом'))
  55.                         ->setRequestContact(true)
  56.                 ))
  57.                     ->setOneTimeKeyboard(true)
  58.                     ->setResizeKeyboard(true)
  59.                     ->setSelective(true);
  60.  
  61.                 $notes['state'] = 1;
  62.                 $conversation->update();
  63.                 $result = Request::sendMessage($data);
  64.                 break;
  65.  
  66.             case 1:
  67.                 if($message->getContact() !== null && $this->isPhoneValid($message->getContact()->getPhoneNumber())) {
  68.                     $this->getUser()->update(['phone' => $this->filterPhone($message->getContact()->getPhoneNumber())]);
  69.                     return $this->endConversation($conversation, $user->fresh());
  70.                 }
  71.  
  72.                 if($this->isEmailValid($text)) {
  73.                     $this->getUser()->update(['email' => strtolower($text)]);
  74.                     return $this->endConversation($conversation, $user->fresh());
  75.                 }
  76.  
  77.                 return Request::sendMessage([
  78.                     'chat_id' => $user->id,
  79.                     'text' => 'Неверные данные. Проверьте правильность введённых данных.'
  80.                 ]);
  81.         }
  82.         return $result;
  83.     }
  84.  
  85.     protected function endConversation(Conversation $conversation, User $user)
  86.     {
  87.         $conversation->stop();
  88.         $this->getSubscriptionService()->setUser($user->fresh())->attachSubscription();
  89.         $subscription = $this->getSubscriptionService()->checkActiveSubscription();
  90.         Request::sendMessage([
  91.             'chat_id' => $user->id,
  92.             'text' => 'Проверяем ваш платёж...',
  93.             'reply_markup' => Keyboard::remove(),
  94.             'parse_mode' => 'Markdown'
  95.         ]);
  96.         sleep(1);
  97.         Request::sendMessage($this->getData($user->id, $subscription));
  98.         if($subscription) {
  99.             $this->sendWelcomeMessages($user->id);
  100.         }
  101.     }
  102.  
  103.     protected function isEmailValid(string $email)
  104.     {
  105.         $validator = app(EmailValidator::class);
  106.         return $validator->isValid($email, new RFCValidation()) && User::query()->where('email', $email)->doesntExist();
  107.     }
  108.  
  109.     protected function filterPhone(string $phone)
  110.     {
  111.         return preg_replace('/[^0-9]/', '', $phone);
  112.     }
  113.  
  114.     protected function isPhoneValid(string $phone)
  115.     {
  116.         return User::query()->where('phone', $this->filterPhone($phone))->doesntExist();
  117.     }
  118.  
  119.     protected function getData(int $chatId, bool $subscription)
  120.     {
  121.         return [
  122.             'chat_id' => $chatId,
  123.             'text' => $this->getText($subscription),
  124.             'reply_markup' => $this->getMenuKeyboard($subscription),
  125.             'parse_mode' => 'Markdown'
  126.         ];
  127.     }
  128.  
  129.     protected function sendWelcomeMessages(int $chatId)
  130.     {
  131.         sleep(1);
  132.  
  133.         Request::sendMessage([
  134.             'chat_id' => $chatId,
  135.             'text' => 'Теперь я каждый день буду присылать тебе по 1 заданию. А также сообщать о закрытых мероприятиях.',
  136.             'parse_mode' => 'Markdown'
  137.         ]);
  138.     }
  139.  
  140.     protected function getText(bool $subscription)
  141.     {
  142.         if($subscription) {
  143.             return $this->getMenuService()->getSuccessfulPaymentMessage();
  144.         }
  145.  
  146.         return $this->getMenuService()->getNotFoundPaymentMessage();
  147.     }
  148.  
  149.     protected function getMenuKeyboard(bool $subscription)
  150.     {
  151.         if($subscription) {
  152.             return Keyboard::remove();
  153.         }
  154.  
  155.         return $this->getMenuService()->getUnsubscribedMenu();
  156.     }
  157.  
  158.     protected function getSubscriptionService()
  159.     {
  160.         return app(SubscriptionService::class);
  161.     }
  162.  
  163.     protected function getMenuService()
  164.     {
  165.         return app(MenuService::class);
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement