diff --git a/src/Skobkin/Bundle/PointToolsBundle/Command/TelegramSendMessageCommand.php b/src/Skobkin/Bundle/PointToolsBundle/Command/TelegramSendMessageCommand.php new file mode 100644 index 0000000..9e8ccb5 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Command/TelegramSendMessageCommand.php @@ -0,0 +1,86 @@ +messenger = $messenger; + } + + /** + * @param int $logChatId + */ + public function setLogChatId(int $logChatId): void + { + $this->logChatId = $logChatId; + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('telegram:send-message') + ->setDescription('Send message via Telegram') + ->addOption('chat-id', 'c', InputOption::VALUE_OPTIONAL, 'ID of the chat') + ->addOption('stdin', 'i', InputOption::VALUE_NONE, 'Read message from stdin instead of option') + ->addArgument('message', InputArgument::OPTIONAL, 'Text of the message') + ; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln('Sending message...'); + + if ($input->getOption('stdin')) { + $message = file_get_contents('php://stdin'); + } elseif (null !== $input->getArgument('message')) { + $message = $input->getArgument('message'); + } else { + $output->writeln('Either \'--stdin\' option or \'message\' argument should be specified.'); + + return 1; + } + + if (mb_strlen($message) > 4096) { + $output->writeln('Message is too long (>4096). Cutting the tail...'); + $message = mb_substr($message, 0, 4090).PHP_EOL.'...'; + } + + try { + $this->messenger->sendMessageToChat( + (int) $input->getOption('chat-id') ?: $this->logChatId, + $message + ); + } catch (\Exception $e) { + $output->writeln('Error: '.$e->getMessage()); + + return 1; + } + + return 0; + } +} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index 1379f3f..e58fb82 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -9,6 +9,7 @@ services: # HTTP client for Point API app.http.point_client: class: GuzzleHttp\Client + autowiring_types: GuzzleHttp\ClientInterface arguments: [ { base_uri: '%point_base_url%', timeout: 5.0 } ] tags: - { name: csa_guzzle.client } @@ -18,24 +19,19 @@ services: # Abstract API client with common dependency app.point.abstract_api: abstract: true - arguments: - - '@app.http.point_client' - - '@jms_serializer' - - '@logger' + autowire: true # User app.point.api_user: class: Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi parent: app.point.abstract_api - arguments: - - '@app.point.user_factory' + autowire: true tags: - { name: monolog.logger, channel: point_user_api } # Post app.point.api_post: class: Skobkin\Bundle\PointToolsBundle\Service\Api\PostApi parent: app.point.abstract_api - arguments: - - '@app.point.post_factory' + autowire: true tags: - { name: monolog.logger, channel: point_post_api } @@ -43,6 +39,7 @@ services: # Point subscription manager app.point.subscriptions_manager: class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager + # TODO deal with autowire for EventDispatcherInterface arguments: - '@event_dispatcher' - '@logger' @@ -56,6 +53,7 @@ services: # Subsribers update app.point.update_subscribers_command: class: Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand + #autowire: [] calls: - [setLogger, ['@logger']] - [setEntityManager, ['@doctrine.orm.entity_manager']] @@ -69,11 +67,20 @@ services: # Webhook management app.telegram.webhook_command: class: Skobkin\Bundle\PointToolsBundle\Command\TelegramWebHookCommand + #autowire: [] calls: - [setClient, ['@app.telegram.bot_client']] - [setRouter, ['@router']] - [setToken, ['%telegram_token%']] tags: [{ name: console.command }] + # Send message + app.telegram.send_message: + class: Skobkin\Bundle\PointToolsBundle\Command\TelegramSendMessageCommand + #autowire: [] + calls: + - [setMessenger, ['@app.telegram.message_sender']] + - [setLogChatId, ['%telegram_log_chat_id%']] + tags: [{ name: console.command }] # Entity repositories as services @@ -123,46 +130,37 @@ services: # Abstract factory app.point.abstract_factory: abstract: true - arguments: [ '@logger' ] + autowire: true # User app.point.user_factory: class: Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory parent: app.point.abstract_factory - arguments: [ '@app.point.user_repository' ] + autowire: true # Comment app.point.comment_factory: class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\CommentFactory parent: app.point.abstract_factory - arguments: - - '@app.point.comment_repository' - - '@app.point.post_repository' - - '@app.point.user_factory' + autowire: true # Tag app.point.tag_factory: class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\TagFactory parent: app.point.abstract_factory - arguments: [ '@app.point.tag_repository' ] + autowire: true # File app.point.file_factory: class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\FileFactory parent: app.point.abstract_factory - arguments: [ '@app.point.file_repository' ] + autowire: true # Post app.point.post_factory: class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory parent: app.point.abstract_factory - arguments: - - '@doctrine.orm.entity_manager' - - '@app.point.post_repository' - - '@app.point.user_factory' - - '@app.point.file_factory' - - '@app.point.comment_factory' - - '@app.point.tag_factory' + autowire: true # Telegram account app.telegram.telegram_account_factory: class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory parent: app.point.abstract_factory - arguments: ['@app.point.telegram_account_repository'] + autowire: true # Custom Markdown parser @@ -173,22 +171,23 @@ services: - { name: markdown.parser } - # Event listener + # Event listeners + # User name changed in Doctrine app.event_listener.users_updated: class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersUpdatedSubscriber arguments: ['@event_dispatcher'] tags: - { name: doctrine.event_subscriber, connection: default } - + # User renaming app.event_listener.users_renamed_notifier: class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersRenamedListener - arguments: ['@app.telegram.notifier'] + autowire: true tags: - { name: kernel.event_listener, event: app.users.renamed } - + # User subscribers updated app.event_listener.user_subscribers_updated: class: Skobkin\Bundle\PointToolsBundle\EventListener\UserSubscribersUpdatedListener - arguments: ['@app.telegram.notifier'] + autowire: true tags: - { name: kernel.event_listener, event: app.user.subscribers_updated } @@ -209,8 +208,8 @@ services: # Bot API client app.telegram.bot_client: class: unreal4u\TelegramAPI\TgLog + autowiring_types: unreal4u\TelegramAPI\TgLog arguments: ['%telegram_token%', '@logger', '@app.http.telegram_client'] - # Logger API client app.telegram.logger_client: class: unreal4u\TelegramAPI\TgLog @@ -224,40 +223,28 @@ services: - '%telegram_log_chat_id%' - 'error' - # Message sender - app.telegram.message_sender: - class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender - arguments: ['@app.telegram.bot_client', '@twig'] - # User notifier app.telegram.notifier: class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\Notifier - arguments: ['@app.point.telegram_account_repository', '@app.telegram.message_sender'] + autowire: true + # Message sender + app.telegram.message_sender: + class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender + autowire: true # Common incoming message processor app.telegram.update_dispatcher: class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher - arguments: - - '@app.telegram.private_message_processor' - - '@app.telegram.inline_query_processor' - + autowire: true # InlineQuery processor app.telegram.inline_query_processor: class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\InlineQueryProcessor lazy: true - arguments: ['@app.point.user_repository', '@app.telegram.bot_client'] - + autowire: true # Private message processor app.telegram.private_message_processor: class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\PrivateMessageProcessor lazy: true - arguments: - - '@doctrine.orm.entity_manager' - - '@app.point.user_repository' - - '@app.point.telegram_account_repository' - - '@app.point.subscription_repository' - - '@app.point.subscription_record_repository' - - '@app.telegram.message_sender' - - '@app.point.api_user' - - '@app.telegram.telegram_account_factory' - - '%point_id%' + autowire: true + calls: + - [setPointUserId, ['%point_id%']] diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php index 7519b6f..af81b7e 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php @@ -75,8 +75,7 @@ class PrivateMessageProcessor SubscriptionEventRepository $subscriptionRecordRepository, MessageSender $messageSender, UserApi $userApi, - AccountFactory $accountFactory, - int $pointUserId + AccountFactory $accountFactory ) { $this->em = $em; $this->userRepo = $userRepository; @@ -86,6 +85,10 @@ class PrivateMessageProcessor $this->messenger = $messageSender; $this->userApi = $userApi; $this->accountFactory = $accountFactory; + } + + public function setPointUserId(int $pointUserId) + { $this->pointUserId = $pointUserId; }