Editable Telegram account settings. Some PrivateMessageProcessor refactoring.
This commit is contained in:
parent
3e42ded305
commit
6c198053d5
|
@ -245,6 +245,13 @@ class Account
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function toggleSubscriberNotification(): self
|
||||||
|
{
|
||||||
|
$this->subscriberNotification = !$this->subscriberNotification;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function isSubscriberNotification(): bool
|
public function isSubscriberNotification(): bool
|
||||||
{
|
{
|
||||||
return $this->subscriberNotification;
|
return $this->subscriberNotification;
|
||||||
|
@ -257,6 +264,13 @@ class Account
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function toggleRenameNotification(): self
|
||||||
|
{
|
||||||
|
$this->renameNotification = !$this->renameNotification;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function isRenameNotification(): bool
|
public function isRenameNotification(): bool
|
||||||
{
|
{
|
||||||
return $this->renameNotification;
|
return $this->renameNotification;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Last global subscription events:
|
*Last global subscription events:*
|
||||||
|
|
||||||
{% set subscription = constant('\\Skobkin\\Bundle\\PointToolsBundle\\Entity\\SubscriptionEvent::ACTION_SUBSCRIBE') %}
|
{% set subscription = constant('\\Skobkin\\Bundle\\PointToolsBundle\\Entity\\SubscriptionEvent::ACTION_SUBSCRIBE') %}
|
||||||
{# @var event \Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent #}
|
{# @var event \Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent #}
|
||||||
|
|
|
@ -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.
|
|
@ -4,8 +4,8 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram;
|
||||||
|
|
||||||
use GuzzleHttp\Exception\ClientException;
|
use GuzzleHttp\Exception\ClientException;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
|
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
|
||||||
|
use unreal4u\TelegramAPI\Abstracts\KeyboardMethods;
|
||||||
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage;
|
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage;
|
||||||
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup;
|
|
||||||
use unreal4u\TelegramAPI\TgLog;
|
use unreal4u\TelegramAPI\TgLog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,7 +34,7 @@ class MessageSender
|
||||||
Account $account,
|
Account $account,
|
||||||
string $text,
|
string $text,
|
||||||
string $parseMode = self::PARSE_MODE_NOPARSE,
|
string $parseMode = self::PARSE_MODE_NOPARSE,
|
||||||
ReplyKeyboardMarkup $keyboardMarkup = null,
|
KeyboardMethods $keyboardMarkup = null,
|
||||||
bool $disableWebPreview = false,
|
bool $disableWebPreview = false,
|
||||||
bool $disableNotifications = false
|
bool $disableNotifications = false
|
||||||
): bool
|
): bool
|
||||||
|
@ -46,7 +46,7 @@ class MessageSender
|
||||||
int $chatId,
|
int $chatId,
|
||||||
string $text,
|
string $text,
|
||||||
string $parseMode = self::PARSE_MODE_NOPARSE,
|
string $parseMode = self::PARSE_MODE_NOPARSE,
|
||||||
ReplyKeyboardMarkup $keyboardMarkup = null,
|
KeyboardMethods $keyboardMarkup = null,
|
||||||
bool $disableWebPreview = false,
|
bool $disableWebPreview = false,
|
||||||
bool $disableNotifications = false
|
bool $disableNotifications = false
|
||||||
): bool
|
): bool
|
||||||
|
|
|
@ -11,8 +11,10 @@ use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
|
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory;
|
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
|
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
|
||||||
|
use unreal4u\TelegramAPI\Abstracts\KeyboardMethods;
|
||||||
use unreal4u\TelegramAPI\Telegram\Types\Message;
|
use unreal4u\TelegramAPI\Telegram\Types\Message;
|
||||||
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup;
|
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup;
|
||||||
|
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardRemove;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes all private messages
|
* Processes all private messages
|
||||||
|
@ -112,69 +114,43 @@ class PrivateMessageProcessor
|
||||||
switch ($words[0]) {
|
switch ($words[0]) {
|
||||||
case '/link':
|
case '/link':
|
||||||
case 'link':
|
case 'link':
|
||||||
if (array_key_exists(2, $words)) {
|
$this->processLink($account, $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`)');
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '/me':
|
case '/me':
|
||||||
case 'me':
|
case 'me':
|
||||||
if ($user = $account->getUser()) {
|
$this->processMe($account);
|
||||||
$this->sendUserEvents($account, $user);
|
|
||||||
} else {
|
|
||||||
$this->sendError($account, 'Account not linked', 'You must /link your account first to be able to use this command.');
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '/last':
|
case '/last':
|
||||||
case 'last':
|
case 'last':
|
||||||
if (array_key_exists(1, $words)) {
|
$this->processLast($account, $words);
|
||||||
if (null !== $user = $this->userRepo->findUserByLogin($words[1])) {
|
|
||||||
$this->sendUserEvents($account, $user);
|
|
||||||
} else {
|
|
||||||
$this->sendError($account, 'User not found');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->sendGlobalEvents($account);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '/sub':
|
case '/sub':
|
||||||
case 'sub':
|
case 'sub':
|
||||||
if (array_key_exists(1, $words)) {
|
$this->processSub($account, $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.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '/stats':
|
case '/stats':
|
||||||
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;
|
break;
|
||||||
|
|
||||||
case '/help':
|
case '/help':
|
||||||
default:
|
default:
|
||||||
$this->sendHelp($account);
|
$this->processHelp($account);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (CommandProcessingException $e) {
|
} catch (CommandProcessingException $e) {
|
||||||
|
@ -206,6 +182,119 @@ class PrivateMessageProcessor
|
||||||
return false;
|
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)
|
private function sendAccountLinked(Account $account)
|
||||||
{
|
{
|
||||||
$this->messenger->sendMessageToUser($account, 'Account linked. Try using /me now.');
|
$this->messenger->sendMessageToUser($account, 'Account linked. Try using /me now.');
|
||||||
|
@ -283,7 +372,7 @@ class PrivateMessageProcessor
|
||||||
Account $account,
|
Account $account,
|
||||||
string $template,
|
string $template,
|
||||||
array $templateData = [],
|
array $templateData = [],
|
||||||
ReplyKeyboardMarkup $keyboardMarkup = null,
|
KeyboardMethods $keyboardMarkup = null,
|
||||||
bool $disableWebPreview = true,
|
bool $disableWebPreview = true,
|
||||||
string $format = MessageSender::PARSE_MODE_MARKDOWN
|
string $format = MessageSender::PARSE_MODE_MARKDOWN
|
||||||
): bool
|
): bool
|
||||||
|
@ -293,7 +382,7 @@ class PrivateMessageProcessor
|
||||||
return $this->messenger->sendMessageToUser($account, $text, $format, $keyboardMarkup, $disableWebPreview, false);
|
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);
|
return $this->messenger->sendMessageToUser($account, $text, MessageSender::PARSE_MODE_NOPARSE, $keyboardMarkup, $disableWebPreview);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue