1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slack-cli package. |
5
|
|
|
* |
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
13
|
|
|
|
14
|
|
|
use CL\Slack\Payload\ChatPostMessagePayload; |
15
|
|
|
use CL\Slack\Payload\ChatPostMessagePayloadResponse; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Cas Leentfaar <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ChatPostMessageCommand extends AbstractApiCommand |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritDoc} |
27
|
|
|
*/ |
28
|
1 |
|
protected function configure() |
29
|
|
|
{ |
30
|
1 |
|
parent::configure(); |
31
|
|
|
|
32
|
1 |
|
$this->setName('chat:post-message'); |
33
|
1 |
|
$this->setAliases(['chat.postMessage']); |
34
|
1 |
|
$this->setDescription('Sends a message to a Slack channel of your choice'); |
35
|
1 |
|
$this->addArgument('channel', InputArgument::REQUIRED, 'The channel to send the message to'); |
36
|
1 |
|
$this->addArgument('text', InputArgument::REQUIRED, 'The text of the message to send'); |
37
|
1 |
|
$this->addOption('username', 'u', InputOption::VALUE_REQUIRED, 'The username that will send this text (does not have to exist in your Team)'); |
38
|
1 |
|
$this->addOption('icon-emoji', 'ie', InputOption::VALUE_REQUIRED, 'Emoji to use as the icon for this message. Overrides `--icon_url`.'); |
39
|
1 |
|
$this->addOption('icon-url', 'iu', InputOption::VALUE_REQUIRED, 'URL to an image to use as the icon for this message'); |
40
|
1 |
|
$this->addOption('parse', 'p', InputOption::VALUE_REQUIRED, 'Change how messages are treated. See information in `--help`'); |
41
|
1 |
|
$this->addOption('link-names', 'l', InputOption::VALUE_REQUIRED, 'Set this flag to `true` to automatically link channel-names and usernames'); |
42
|
1 |
|
$this->addOption('unfurl-links', 'ul', InputOption::VALUE_REQUIRED, 'Pass true to enable unfurling of primarily text-based content'); |
43
|
1 |
|
$this->addOption('unfurl-media', 'um', InputOption::VALUE_REQUIRED, 'Pass false to disable unfurling of media content'); |
44
|
1 |
|
$this->setHelp(<<<EOT |
45
|
|
|
The <info>chat:post-message</info> command posts a message to a given channel. |
46
|
|
|
|
47
|
|
|
Messages are formatted as described in the formatting spec (link below). You can specify values for `parse` and `link_names` |
48
|
|
|
to change formatting behavior. |
49
|
|
|
|
50
|
|
|
The optional attachments argument should contain a JSON-encoded array of attachments. For more information, see the |
51
|
|
|
`attachments` spec (link below). |
52
|
|
|
|
53
|
|
|
By default links to media are unfurled, but links to text content are not. |
54
|
|
|
|
55
|
|
|
For more information about the related API method, check out the official documentation: |
56
|
|
|
<comment>https://api.slack.com/methods/chat.postMessage</comment> |
57
|
|
|
EOT |
58
|
1 |
|
); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return ChatPostMessagePayload |
63
|
|
|
*/ |
64
|
1 |
|
protected function createPayload() |
65
|
|
|
{ |
66
|
1 |
|
$payload = new ChatPostMessagePayload(); |
67
|
1 |
|
$channel = $this->input->getArgument('channel'); |
68
|
|
|
|
69
|
|
|
// help support un-escaped channel names such as 'general' (the hash-sign requires the channel name to be quoted) |
70
|
|
|
// also making sure to ignore it if a channel ID was given |
71
|
|
|
// @todo Reconsider this approach; different channel formats could be allowed that might conflict with this |
72
|
1 |
|
if (substr($channel, 0, 1) !== '#' && !(substr($channel, 0, 1) === 'G' && is_numeric(substr($channel, 1)))) { |
73
|
|
|
$channel = '#' . $channel; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$payload->setChannel($channel); |
77
|
1 |
|
$payload->setText($this->input->getArgument('text')); |
78
|
|
|
|
79
|
1 |
|
if ($this->input->getOption('username')) { |
80
|
|
|
$payload->setUsername($this->input->getOption('username')); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
if ($this->input->getOption('icon-url')) { |
84
|
|
|
$payload->setIconUrl($this->input->getOption('icon-url')); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
if ($this->input->getOption('icon-emoji')) { |
88
|
|
|
$payload->setIconEmoji($this->input->getOption('icon-emoji')); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
if ($this->input->getOption('parse')) { |
92
|
|
|
$payload->setParse($this->input->getOption('parse')); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
if ($this->input->getOption('link-names')) { |
96
|
|
|
$payload->setLinkNames($this->input->getOption('link-names')); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
if ($this->input->getOption('unfurl-links')) { |
100
|
|
|
$payload->setUnfurlLinks($this->input->getOption('unfurl-links')); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
if ($this->input->getOption('unfurl-media')) { |
104
|
|
|
$payload->setUnfurlMedia($this->input->getOption('unfurl-media')); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return $payload; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @param ChatPostMessagePayloadResponse $payloadResponse |
114
|
|
|
*/ |
115
|
1 |
|
protected function handleResponse($payloadResponse) |
116
|
|
|
{ |
117
|
1 |
|
if ($payloadResponse->isOk()) { |
118
|
1 |
|
$this->writeOk('Successfully sent message to Slack!'); |
119
|
1 |
|
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) { |
120
|
1 |
|
$this->output->writeln(sprintf('Channel ID: <comment>%s</comment>', $payloadResponse->getChannelId())); |
121
|
1 |
|
$this->output->writeln(sprintf('Timestamp: <comment>%s</comment>', $payloadResponse->getSlackTimestamp())); |
122
|
1 |
|
} |
123
|
1 |
|
} else { |
124
|
1 |
|
$this->writeError(sprintf('Failed to send message to Slack: %s', lcfirst($payloadResponse->getErrorExplanation()))); |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
|
|
} |
128
|
|
|
|