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
|
|
|
public function __construct(MessageSender $messenger) |
16
|
|
|
{ |
17
|
|
|
$this->messenger = $messenger; |
18
|
|
|
|
19
|
|
|
parent::__construct(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
|
|
->setName('telegram:send-message') |
29
|
|
|
->setDescription('Send message via Telegram') |
30
|
|
|
->addOption('chat-id', 'c', InputOption::VALUE_OPTIONAL, 'ID of the chat') |
31
|
|
|
->addOption('stdin', 'i', InputOption::VALUE_NONE, 'Read message from stdin instead of option') |
32
|
|
|
->addArgument('message', InputArgument::OPTIONAL, 'Text of the message') |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
40
|
|
|
{ |
41
|
|
|
$output->writeln('Sending message...'); |
42
|
|
|
|
43
|
|
|
if ($input->getOption('stdin')) { |
44
|
|
|
$message = file_get_contents('php://stdin'); |
45
|
|
|
} elseif (null !== $input->getArgument('message')) { |
46
|
|
|
$message = $input->getArgument('message'); |
47
|
|
|
} else { |
48
|
|
|
$output->writeln('<error>Either \'--stdin\' option or \'message\' argument should be specified.</error>'); |
49
|
|
|
|
50
|
|
|
return 1; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (mb_strlen($message) > 4096) { |
54
|
|
|
$output->writeln('<comment>Message is too long (>4096). Cutting the tail...</comment>'); |
55
|
|
|
$message = mb_substr($message, 0, 4090).PHP_EOL.'...'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$this->messenger->sendMessageToChat( |
60
|
|
|
(int) $input->getOption('chat-id'), |
61
|
|
|
$message |
|
|
|
|
62
|
|
|
); |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
$output->writeln('Error: '.$e->getMessage()); |
65
|
|
|
|
66
|
|
|
return 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return 0; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.