1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Command; |
4
|
|
|
|
5
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\{InputArgument, InputInterface, InputOption}; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class TelegramSendMessageCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** @var MessageSender */ |
13
|
|
|
private $messenger; |
14
|
|
|
|
15
|
|
|
/** @var int */ |
16
|
|
|
private $logChatId; |
17
|
|
|
|
18
|
|
|
public function __construct(MessageSender $messenger, int $logChatId) |
19
|
|
|
{ |
20
|
|
|
$this->messenger = $messenger; |
21
|
|
|
$this->logChatId = $logChatId; |
22
|
|
|
|
23
|
|
|
parent::__construct(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setName('telegram:send-message') |
33
|
|
|
->setDescription('Send message via Telegram') |
34
|
|
|
->addOption('chat-id', 'c', InputOption::VALUE_OPTIONAL, 'ID of the chat') |
35
|
|
|
->addOption('stdin', 'i', InputOption::VALUE_NONE, 'Read message from stdin instead of option') |
36
|
|
|
->addArgument('message', InputArgument::OPTIONAL, 'Text of the message') |
37
|
|
|
; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
44
|
|
|
{ |
45
|
|
|
$output->writeln('Sending message...'); |
46
|
|
|
|
47
|
|
|
if ($input->getOption('stdin')) { |
48
|
|
|
$message = file_get_contents('php://stdin'); |
49
|
|
|
} elseif (null !== $input->getArgument('message')) { |
50
|
|
|
$message = $input->getArgument('message'); |
51
|
|
|
} else { |
52
|
|
|
$output->writeln('<error>Either \'--stdin\' option or \'message\' argument should be specified.</error>'); |
53
|
|
|
|
54
|
|
|
return 1; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (mb_strlen($message) > 4096) { |
58
|
|
|
$output->writeln('<comment>Message is too long (>4096). Cutting the tail...</comment>'); |
59
|
|
|
$message = mb_substr($message, 0, 4090).PHP_EOL.'...'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$this->messenger->sendMessageToChat( |
64
|
|
|
(int) $input->getOption('chat-id') ?: $this->logChatId, |
65
|
|
|
$message |
66
|
|
|
); |
67
|
|
|
} catch (\Exception $e) { |
68
|
|
|
$output->writeln('Error: '.$e->getMessage()); |
69
|
|
|
|
70
|
|
|
return 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return 0; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|