Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Telegram\Commands;
- use App\Models\User;
- use App\Services\MenuService;
- use App\Services\SubscriptionService;
- use Egulias\EmailValidator\EmailValidator;
- use Egulias\EmailValidator\Validation\RFCValidation;
- use Longman\TelegramBot\Conversation;
- use Longman\TelegramBot\Entities\Keyboard;
- use Longman\TelegramBot\Entities\KeyboardButton;
- use Longman\TelegramBot\Request;
- class RequestDataCommand extends GenericSystemCommand {
- public function execute()
- {
- $message = $this->getMessage();
- $chat = $message->getChat();
- $user = $this->getUser();
- $text = trim($message->getText(true));
- $chat_id = $chat->getId();
- $data = [
- 'chat_id' => $chat_id,
- ];
- $conversation = new Conversation($user->id, $chat->getId(), 'reqdata');
- $notes = &$conversation->notes;
- !is_array($notes) && $notes = [];
- $state = 0;
- if (isset($notes['state'])) {
- $state = $notes['state'];
- }
- $result = Request::emptyResponse();
- switch ($state) {
- case 0:
- if($user->email !== null || $user->phone !== null) {
- return Request::sendMessage([
- 'chat_id' => $chat_id,
- 'text' => 'Данные уже добавлены. Для сброса данных используйте /reset'
- ]);
- }
- $data['text'] = <<<REQTEXT
- Напишите, пожалуйста, ваш e-mail (необходим для проверки оплаты) или нажмите “поделиться контактом”
- REQTEXT;
- $data['reply_markup'] = (new Keyboard(
- (new KeyboardButton('Поделиться контактом'))
- ->setRequestContact(true)
- ))
- ->setOneTimeKeyboard(true)
- ->setResizeKeyboard(true)
- ->setSelective(true);
- $notes['state'] = 1;
- $conversation->update();
- $result = Request::sendMessage($data);
- break;
- case 1:
- if($message->getContact() !== null && $this->isPhoneValid($message->getContact()->getPhoneNumber())) {
- $this->getUser()->update(['phone' => $this->filterPhone($message->getContact()->getPhoneNumber())]);
- return $this->endConversation($conversation, $user->fresh());
- }
- if($this->isEmailValid($text)) {
- $this->getUser()->update(['email' => strtolower($text)]);
- return $this->endConversation($conversation, $user->fresh());
- }
- return Request::sendMessage([
- 'chat_id' => $user->id,
- 'text' => 'Неверные данные. Проверьте правильность введённых данных.'
- ]);
- }
- return $result;
- }
- protected function endConversation(Conversation $conversation, User $user)
- {
- $conversation->stop();
- $this->getSubscriptionService()->setUser($user->fresh())->attachSubscription();
- $subscription = $this->getSubscriptionService()->checkActiveSubscription();
- Request::sendMessage([
- 'chat_id' => $user->id,
- 'text' => 'Проверяем ваш платёж...',
- 'reply_markup' => Keyboard::remove(),
- 'parse_mode' => 'Markdown'
- ]);
- sleep(1);
- Request::sendMessage($this->getData($user->id, $subscription));
- if($subscription) {
- $this->sendWelcomeMessages($user->id);
- }
- }
- protected function isEmailValid(string $email)
- {
- $validator = app(EmailValidator::class);
- return $validator->isValid($email, new RFCValidation()) && User::query()->where('email', $email)->doesntExist();
- }
- protected function filterPhone(string $phone)
- {
- return preg_replace('/[^0-9]/', '', $phone);
- }
- protected function isPhoneValid(string $phone)
- {
- return User::query()->where('phone', $this->filterPhone($phone))->doesntExist();
- }
- protected function getData(int $chatId, bool $subscription)
- {
- return [
- 'chat_id' => $chatId,
- 'text' => $this->getText($subscription),
- 'reply_markup' => $this->getMenuKeyboard($subscription),
- 'parse_mode' => 'Markdown'
- ];
- }
- protected function sendWelcomeMessages(int $chatId)
- {
- sleep(1);
- Request::sendMessage([
- 'chat_id' => $chatId,
- 'text' => 'Теперь я каждый день буду присылать тебе по 1 заданию. А также сообщать о закрытых мероприятиях.',
- 'parse_mode' => 'Markdown'
- ]);
- }
- protected function getText(bool $subscription)
- {
- if($subscription) {
- return $this->getMenuService()->getSuccessfulPaymentMessage();
- }
- return $this->getMenuService()->getNotFoundPaymentMessage();
- }
- protected function getMenuKeyboard(bool $subscription)
- {
- if($subscription) {
- return Keyboard::remove();
- }
- return $this->getMenuService()->getUnsubscribedMenu();
- }
- protected function getSubscriptionService()
- {
- return app(SubscriptionService::class);
- }
- protected function getMenuService()
- {
- return app(MenuService::class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement