diff --git a/old/src/PointToolsBundle/Command/TelegramSendMessageCommand.php b/old/src/PointToolsBundle/Command/TelegramSendMessageCommand.php deleted file mode 100644 index 9c97df8..0000000 --- a/old/src/PointToolsBundle/Command/TelegramSendMessageCommand.php +++ /dev/null @@ -1,73 +0,0 @@ -messenger = $messenger; - - parent::__construct(); - } - - /** - * {@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'), - $message - ); - } catch (\Exception $e) { - $output->writeln('Error: '.$e->getMessage()); - - return 1; - } - - return 0; - } -} diff --git a/src/Command/TelegramSendMessageCommand.php b/src/Command/TelegramSendMessageCommand.php new file mode 100644 index 0000000..1ab3ccb --- /dev/null +++ b/src/Command/TelegramSendMessageCommand.php @@ -0,0 +1,65 @@ +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') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + if ($input->getOption('stdin')) { + $message = \file_get_contents('php://stdin'); + } elseif (null !== $input->getArgument('message')) { + $message = $input->getArgument('message'); + } else { + $io->error('Either \'--stdin\' option or \'message\' argument should be specified.') + + return Command::FAILURE; + } + + if (mb_strlen($message) > 4096) { + $io->comment('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'), + $message + ); + } catch (\Exception $e) { + $io->error($e->getMessage()); + + return Command::FAILURE; + } + + return Command::SUCCESS; + } +}