Autowiring test. 'telegram:send-message' command added.

This commit is contained in:
Alexey Skobkin 2017-01-16 02:54:19 +03:00
parent 3d481f744f
commit 5633a5146b
3 changed files with 130 additions and 54 deletions

View file

@ -0,0 +1,86 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Command;
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TelegramSendMessageCommand extends ContainerAwareCommand
{
/**
* @var MessageSender
*/
private $messenger;
/**
* @var int
*/
private $logChatId;
public function setMessenger(MessageSender $messenger): void
{
$this->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('<error>Either \'--stdin\' option or \'message\' argument should be specified.</error>');
return 1;
}
if (mb_strlen($message) > 4096) {
$output->writeln('<comment>Message is too long (>4096). Cutting the tail...</comment>');
$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;
}
}

View file

@ -9,6 +9,7 @@ services:
# HTTP client for Point API # HTTP client for Point API
app.http.point_client: app.http.point_client:
class: GuzzleHttp\Client class: GuzzleHttp\Client
autowiring_types: GuzzleHttp\ClientInterface
arguments: [ { base_uri: '%point_base_url%', timeout: 5.0 } ] arguments: [ { base_uri: '%point_base_url%', timeout: 5.0 } ]
tags: tags:
- { name: csa_guzzle.client } - { name: csa_guzzle.client }
@ -18,24 +19,19 @@ services:
# Abstract API client with common dependency # Abstract API client with common dependency
app.point.abstract_api: app.point.abstract_api:
abstract: true abstract: true
arguments: autowire: true
- '@app.http.point_client'
- '@jms_serializer'
- '@logger'
# User # User
app.point.api_user: app.point.api_user:
class: Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi class: Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi
parent: app.point.abstract_api parent: app.point.abstract_api
arguments: autowire: true
- '@app.point.user_factory'
tags: tags:
- { name: monolog.logger, channel: point_user_api } - { name: monolog.logger, channel: point_user_api }
# Post # Post
app.point.api_post: app.point.api_post:
class: Skobkin\Bundle\PointToolsBundle\Service\Api\PostApi class: Skobkin\Bundle\PointToolsBundle\Service\Api\PostApi
parent: app.point.abstract_api parent: app.point.abstract_api
arguments: autowire: true
- '@app.point.post_factory'
tags: tags:
- { name: monolog.logger, channel: point_post_api } - { name: monolog.logger, channel: point_post_api }
@ -43,6 +39,7 @@ services:
# Point subscription manager # Point subscription manager
app.point.subscriptions_manager: app.point.subscriptions_manager:
class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager
# TODO deal with autowire for EventDispatcherInterface
arguments: arguments:
- '@event_dispatcher' - '@event_dispatcher'
- '@logger' - '@logger'
@ -56,6 +53,7 @@ services:
# Subsribers update # Subsribers update
app.point.update_subscribers_command: app.point.update_subscribers_command:
class: Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand class: Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand
#autowire: []
calls: calls:
- [setLogger, ['@logger']] - [setLogger, ['@logger']]
- [setEntityManager, ['@doctrine.orm.entity_manager']] - [setEntityManager, ['@doctrine.orm.entity_manager']]
@ -69,11 +67,20 @@ services:
# Webhook management # Webhook management
app.telegram.webhook_command: app.telegram.webhook_command:
class: Skobkin\Bundle\PointToolsBundle\Command\TelegramWebHookCommand class: Skobkin\Bundle\PointToolsBundle\Command\TelegramWebHookCommand
#autowire: []
calls: calls:
- [setClient, ['@app.telegram.bot_client']] - [setClient, ['@app.telegram.bot_client']]
- [setRouter, ['@router']] - [setRouter, ['@router']]
- [setToken, ['%telegram_token%']] - [setToken, ['%telegram_token%']]
tags: [{ name: console.command }] 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 # Entity repositories as services
@ -123,46 +130,37 @@ services:
# Abstract factory # Abstract factory
app.point.abstract_factory: app.point.abstract_factory:
abstract: true abstract: true
arguments: [ '@logger' ] autowire: true
# User # User
app.point.user_factory: app.point.user_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory
parent: app.point.abstract_factory parent: app.point.abstract_factory
arguments: [ '@app.point.user_repository' ] autowire: true
# Comment # Comment
app.point.comment_factory: app.point.comment_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\CommentFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\CommentFactory
parent: app.point.abstract_factory parent: app.point.abstract_factory
arguments: autowire: true
- '@app.point.comment_repository'
- '@app.point.post_repository'
- '@app.point.user_factory'
# Tag # Tag
app.point.tag_factory: app.point.tag_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\TagFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\TagFactory
parent: app.point.abstract_factory parent: app.point.abstract_factory
arguments: [ '@app.point.tag_repository' ] autowire: true
# File # File
app.point.file_factory: app.point.file_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\FileFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\FileFactory
parent: app.point.abstract_factory parent: app.point.abstract_factory
arguments: [ '@app.point.file_repository' ] autowire: true
# Post # Post
app.point.post_factory: app.point.post_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory
parent: app.point.abstract_factory parent: app.point.abstract_factory
arguments: autowire: true
- '@doctrine.orm.entity_manager'
- '@app.point.post_repository'
- '@app.point.user_factory'
- '@app.point.file_factory'
- '@app.point.comment_factory'
- '@app.point.tag_factory'
# Telegram account # Telegram account
app.telegram.telegram_account_factory: app.telegram.telegram_account_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory
parent: app.point.abstract_factory parent: app.point.abstract_factory
arguments: ['@app.point.telegram_account_repository'] autowire: true
# Custom Markdown parser # Custom Markdown parser
@ -173,22 +171,23 @@ services:
- { name: markdown.parser } - { name: markdown.parser }
# Event listener # Event listeners
# User name changed in Doctrine
app.event_listener.users_updated: app.event_listener.users_updated:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersUpdatedSubscriber class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersUpdatedSubscriber
arguments: ['@event_dispatcher'] arguments: ['@event_dispatcher']
tags: tags:
- { name: doctrine.event_subscriber, connection: default } - { name: doctrine.event_subscriber, connection: default }
# User renaming
app.event_listener.users_renamed_notifier: app.event_listener.users_renamed_notifier:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersRenamedListener class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersRenamedListener
arguments: ['@app.telegram.notifier'] autowire: true
tags: tags:
- { name: kernel.event_listener, event: app.users.renamed } - { name: kernel.event_listener, event: app.users.renamed }
# User subscribers updated
app.event_listener.user_subscribers_updated: app.event_listener.user_subscribers_updated:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UserSubscribersUpdatedListener class: Skobkin\Bundle\PointToolsBundle\EventListener\UserSubscribersUpdatedListener
arguments: ['@app.telegram.notifier'] autowire: true
tags: tags:
- { name: kernel.event_listener, event: app.user.subscribers_updated } - { name: kernel.event_listener, event: app.user.subscribers_updated }
@ -209,8 +208,8 @@ services:
# Bot API client # Bot API client
app.telegram.bot_client: app.telegram.bot_client:
class: unreal4u\TelegramAPI\TgLog class: unreal4u\TelegramAPI\TgLog
autowiring_types: unreal4u\TelegramAPI\TgLog
arguments: ['%telegram_token%', '@logger', '@app.http.telegram_client'] arguments: ['%telegram_token%', '@logger', '@app.http.telegram_client']
# Logger API client # Logger API client
app.telegram.logger_client: app.telegram.logger_client:
class: unreal4u\TelegramAPI\TgLog class: unreal4u\TelegramAPI\TgLog
@ -224,40 +223,28 @@ services:
- '%telegram_log_chat_id%' - '%telegram_log_chat_id%'
- 'error' - 'error'
# Message sender
app.telegram.message_sender:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender
arguments: ['@app.telegram.bot_client', '@twig']
# User notifier # User notifier
app.telegram.notifier: app.telegram.notifier:
class: Skobkin\Bundle\PointToolsBundle\Service\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 # Common incoming message processor
app.telegram.update_dispatcher: app.telegram.update_dispatcher:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher
arguments: autowire: true
- '@app.telegram.private_message_processor'
- '@app.telegram.inline_query_processor'
# InlineQuery processor # InlineQuery processor
app.telegram.inline_query_processor: app.telegram.inline_query_processor:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\InlineQueryProcessor class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\InlineQueryProcessor
lazy: true lazy: true
arguments: ['@app.point.user_repository', '@app.telegram.bot_client'] autowire: true
# Private message processor # Private message processor
app.telegram.private_message_processor: app.telegram.private_message_processor:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\PrivateMessageProcessor class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\PrivateMessageProcessor
lazy: true lazy: true
arguments: autowire: true
- '@doctrine.orm.entity_manager' calls:
- '@app.point.user_repository' - [setPointUserId, ['%point_id%']]
- '@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%'

View file

@ -75,8 +75,7 @@ class PrivateMessageProcessor
SubscriptionEventRepository $subscriptionRecordRepository, SubscriptionEventRepository $subscriptionRecordRepository,
MessageSender $messageSender, MessageSender $messageSender,
UserApi $userApi, UserApi $userApi,
AccountFactory $accountFactory, AccountFactory $accountFactory
int $pointUserId
) { ) {
$this->em = $em; $this->em = $em;
$this->userRepo = $userRepository; $this->userRepo = $userRepository;
@ -86,6 +85,10 @@ class PrivateMessageProcessor
$this->messenger = $messageSender; $this->messenger = $messageSender;
$this->userApi = $userApi; $this->userApi = $userApi;
$this->accountFactory = $accountFactory; $this->accountFactory = $accountFactory;
}
public function setPointUserId(int $pointUserId)
{
$this->pointUserId = $pointUserId; $this->pointUserId = $pointUserId;
} }