From 6c198053d54a479fca57ff0edca0748d9877a3ea Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Fri, 6 Jan 2017 22:19:44 +0300 Subject: [PATCH] Editable Telegram account settings. Some PrivateMessageProcessor refactoring. --- .../Entity/Telegram/Account.php | 14 ++ .../last_global_subscriptions.md.twig | 2 +- .../Resources/views/Telegram/settings.md.twig | 9 + .../Service/Telegram/MessageSender.php | 6 +- .../Telegram/PrivateMessageProcessor.php | 177 +++++++++++++----- 5 files changed, 160 insertions(+), 48 deletions(-) create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Telegram/Account.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Telegram/Account.php index 20d4b48..10f25bd 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Telegram/Account.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Telegram/Account.php @@ -245,6 +245,13 @@ class Account return $this; } + public function toggleSubscriberNotification(): self + { + $this->subscriberNotification = !$this->subscriberNotification; + + return $this; + } + public function isSubscriberNotification(): bool { return $this->subscriberNotification; @@ -257,6 +264,13 @@ class Account return $this; } + public function toggleRenameNotification(): self + { + $this->renameNotification = !$this->renameNotification; + + return $this; + } + public function isRenameNotification(): bool { return $this->renameNotification; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig index ab13fe1..7882853 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig @@ -1,4 +1,4 @@ -Last global subscription events: +*Last global subscription events:* {% set subscription = constant('\\Skobkin\\Bundle\\PointToolsBundle\\Entity\\SubscriptionEvent::ACTION_SUBSCRIBE') %} {# @var event \Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent #} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig new file mode 100644 index 0000000..ed59685 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig @@ -0,0 +1,9 @@ +{# @var account \Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account #} +*Your current settings:* + +User renaming notifications: {% if account.renameNotification %}enabled{% else %}disabled{% endif %} + +Subscribers notifications: {% if account.subscriberNotification %}enabled{% else %}disabled{% endif %} + + +Choose what to change. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/MessageSender.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/MessageSender.php index ee42910..185a99d 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/MessageSender.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/MessageSender.php @@ -4,8 +4,8 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram; use GuzzleHttp\Exception\ClientException; use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account; +use unreal4u\TelegramAPI\Abstracts\KeyboardMethods; use unreal4u\TelegramAPI\Telegram\Methods\SendMessage; -use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup; use unreal4u\TelegramAPI\TgLog; /** @@ -34,7 +34,7 @@ class MessageSender Account $account, string $text, string $parseMode = self::PARSE_MODE_NOPARSE, - ReplyKeyboardMarkup $keyboardMarkup = null, + KeyboardMethods $keyboardMarkup = null, bool $disableWebPreview = false, bool $disableNotifications = false ): bool @@ -46,7 +46,7 @@ class MessageSender int $chatId, string $text, string $parseMode = self::PARSE_MODE_NOPARSE, - ReplyKeyboardMarkup $keyboardMarkup = null, + KeyboardMethods $keyboardMarkup = null, bool $disableWebPreview = false, bool $disableNotifications = false ): bool diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php index 9691731..ce61753 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php @@ -11,8 +11,10 @@ use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository; use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository; use Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory; use Skobkin\Bundle\PointToolsBundle\Service\UserApi; +use unreal4u\TelegramAPI\Abstracts\KeyboardMethods; use unreal4u\TelegramAPI\Telegram\Types\Message; use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup; +use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardRemove; /** * Processes all private messages @@ -112,69 +114,43 @@ class PrivateMessageProcessor switch ($words[0]) { case '/link': case 'link': - if (array_key_exists(2, $words)) { - if ($this->linkAccount($account, $words[1], $words[2])) { - // Saving linking status - $this->em->flush(); - $this->sendAccountLinked($account); - } else { - $this->sendError($account, 'Account linking error', 'Check login and password or try again later.'); - } - } else { - $this->sendError($account, 'Login/Password error', 'You need to specify login and password separated by space after /link (example: `/link mylogin MypASSw0rd`)'); - } + $this->processLink($account, $words); break; case '/me': case 'me': - if ($user = $account->getUser()) { - $this->sendUserEvents($account, $user); - } else { - $this->sendError($account, 'Account not linked', 'You must /link your account first to be able to use this command.'); - } + $this->processMe($account); break; case '/last': case 'last': - if (array_key_exists(1, $words)) { - if (null !== $user = $this->userRepo->findUserByLogin($words[1])) { - $this->sendUserEvents($account, $user); - } else { - $this->sendError($account, 'User not found'); - } - } else { - $this->sendGlobalEvents($account); - } - + $this->processLast($account, $words); break; case '/sub': case 'sub': - if (array_key_exists(1, $words)) { - if (null !== $user = $this->userRepo->findUserByLogin($words[1])) { - $this->sendUserSubscribers($account, $user); - } else { - $this->sendError($account, 'User not found'); - } - } else { - if ($user = $account->getUser()) { - $this->sendUserSubscribers($account, $user); - } else { - $this->sendError($account, 'Account not linked', 'You must /link your account first to be able to use this command.'); - } - } - + $this->processSub($account, $words); break; case '/stats': case 'stats': - $this->sendStats($account); + $this->processStats($account); + break; + // Settings menu + case '/set': + $this->processSet($account, $words); + break; + + // Exit from any menu and remove keyboard + case '/exit': + case 'exit': + $this->processExit($account); break; case '/help': default: - $this->sendHelp($account); + $this->processHelp($account); break; } } catch (CommandProcessingException $e) { @@ -206,6 +182,119 @@ class PrivateMessageProcessor return false; } + private function processLink(Account $account, array $words) + { + if (array_key_exists(2, $words)) { + if ($this->linkAccount($account, $words[1], $words[2])) { + // Saving linking status + $this->em->flush(); + $this->sendAccountLinked($account); + } else { + $this->sendError($account, 'Account linking error', 'Check login and password or try again later.'); + } + } else { + $this->sendError($account, 'Login/Password error', 'You need to specify login and password separated by space after /link (example: `/link mylogin MypASSw0rd`)'); + } + } + + private function processMe(Account $account) + { + if ($user = $account->getUser()) { + $this->sendUserEvents($account, $user); + } else { + $this->sendError($account, 'Account not linked', 'You must /link your account first to be able to use this command.'); + } + } + + private function processLast(Account $account, array $words) + { + if (array_key_exists(1, $words)) { + if (null !== $user = $this->userRepo->findUserByLogin($words[1])) { + $this->sendUserEvents($account, $user); + } else { + $this->sendError($account, 'User not found'); + } + } else { + $this->sendGlobalEvents($account); + } + } + + private function processSub(Account $account, array $words) + { + if (array_key_exists(1, $words)) { + if (null !== $user = $this->userRepo->findUserByLogin($words[1])) { + $this->sendUserSubscribers($account, $user); + } else { + $this->sendError($account, 'User not found'); + } + } else { + if ($user = $account->getUser()) { + $this->sendUserSubscribers($account, $user); + } else { + $this->sendError($account, 'Account not linked', 'You must /link your account first to be able to use this command.'); + } + } + } + + private function processStats(Account $account) + { + $this->sendStats($account); + } + + private function processSet(Account $account, array $words) + { + $keyboard = new ReplyKeyboardMarkup(); + + if (array_key_exists(1, $words)) { + if (array_key_exists(2, $words)) { + if ('renames' === $words[2]) { + $account->toggleRenameNotification(); + $this->em->flush(); + + $this->sendPlainTextMessage($account, 'Renaming notifications are turned '.($account->isRenameNotification() ? 'on' : 'off')); + } elseif ('subscribers' === $words[2]) { + $account->toggleSubscriberNotification(); + $this->em->flush(); + + $this->sendPlainTextMessage($account, 'Subscribers notifications are turned '.($account->isSubscriberNotification() ? 'on' : 'off')); + } else { + $this->sendError($account, 'Notification type does not exist.'); + } + } else { + $keyboard->keyboard = [ + ['/set notifications renames'], + ['/set notifications subscribers'], + ['exit'], + ]; + + $this->sendPlainTextMessage($account, 'Choose which notification type to toggle', $keyboard); + } + + } else { + $keyboard->keyboard = [ + ['/set notifications'], + ['exit'], + ]; + + $this->sendTemplatedMessage($account, '@SkobkinPointTools/Telegram/settings.md.twig', ['account' => $account], $keyboard); + } + } + + /** + * Processes exit from keyboard menus and removes the keyboard + */ + private function processExit(Account $account) + { + $keyboardRemove = new ReplyKeyboardRemove(); + + $this->sendPlainTextMessage($account, 'Done', $keyboardRemove); + } + + private function processHelp(Account $account) + { + $this->sendHelp($account); + } + private function sendAccountLinked(Account $account) { $this->messenger->sendMessageToUser($account, 'Account linked. Try using /me now.'); @@ -283,7 +372,7 @@ class PrivateMessageProcessor Account $account, string $template, array $templateData = [], - ReplyKeyboardMarkup $keyboardMarkup = null, + KeyboardMethods $keyboardMarkup = null, bool $disableWebPreview = true, string $format = MessageSender::PARSE_MODE_MARKDOWN ): bool @@ -293,7 +382,7 @@ class PrivateMessageProcessor return $this->messenger->sendMessageToUser($account, $text, $format, $keyboardMarkup, $disableWebPreview, false); } - private function sendPlainTextMessage(Account $account, string $text, ReplyKeyboardMarkup $keyboardMarkup = null, bool $disableWebPreview = true): bool + private function sendPlainTextMessage(Account $account, string $text, KeyboardMethods $keyboardMarkup = null, bool $disableWebPreview = true): bool { return $this->messenger->sendMessageToUser($account, $text, MessageSender::PARSE_MODE_NOPARSE, $keyboardMarkup, $disableWebPreview); }