/** * {@inheritdoc} */ protected function createPayload() { $payload = new ChatPostMessagePayload(); $payload->setChannel('#acme_channel'); $payload->setUsername('acme_user'); $payload->setText('Hello World!'); $payload->setIconEmoji(':truck:'); $payload->setLinkNames(true); $payload->setParse('full'); $payload->setUnfurlLinks(true); $payload->setUnfurlMedia(false); $payload->setIconUrl('http://foo.bar/emoji-1.png'); $fakeAttachmentField = new AttachmentField(); $fakeAttachmentField->setShort(false); $fakeAttachmentField->setTitle('the title'); $fakeAttachmentField->setValue('the value'); $fakeAttachment = new Attachment(); $fakeAttachment->setTitle('the title'); $fakeAttachment->setTitleLink('http://thetitlelink.com'); $fakeAttachment->setColor('the color'); $fakeAttachment->setFallback('the fallback'); $fakeAttachment->setImageUrl('the image url'); $fakeAttachment->setPreText('this is...'); $fakeAttachment->setText('my attachment'); $fakeAttachment->setAuthorIcon(':skull:'); $fakeAttachment->setAuthorName('the author'); $fakeAttachment->setAuthorLink('http://theauthor.com'); $fakeAttachment->addField($fakeAttachmentField); $payload->addAttachment($fakeAttachment); return $payload; }
/** * @return ChatPostMessagePayload */ protected function createPayload() { $payload = new ChatPostMessagePayload(); $channel = $this->input->getArgument('channel'); // help support un-escaped channel names such as 'general' (the hash-sign requires the channel name to be quoted) // also making sure to ignore it if a channel ID was given // @todo Reconsider this approach; different channel formats could be allowed that might conflict with this if (substr($channel, 0, 1) !== '#' && !(substr($channel, 0, 1) === 'G' && is_numeric(substr($channel, 1)))) { $channel = '#' . $channel; } $payload->setChannel($channel); $payload->setText($this->input->getArgument('text')); if ($this->input->getOption('username')) { $payload->setUsername($this->input->getOption('username')); } if ($this->input->getOption('icon-url')) { $payload->setIconUrl($this->input->getOption('icon-url')); } if ($this->input->getOption('icon-emoji')) { $payload->setIconEmoji($this->input->getOption('icon-emoji')); } if ($this->input->getOption('parse')) { $payload->setParse($this->input->getOption('parse')); } if ($this->input->getOption('link-names')) { $payload->setLinkNames($this->input->getOption('link-names')); } if ($this->input->getOption('unfurl-links')) { $payload->setUnfurlLinks($this->input->getOption('unfurl-links')); } if ($this->input->getOption('unfurl-media')) { $payload->setUnfurlMedia($this->input->getOption('unfurl-media')); } return $payload; }
/** * @param string[] $userIds * @param string|null $adminMessage * @param null $adminUserId * * @return Result */ public function dispatchTo($userIds, $adminMessage = null, $adminUserId = null) { $rudolph = new Rudolph(); $associatedUsers = $rudolph->associateUsers($userIds); $hash = md5(serialize($associatedUsers)); $remainingAssociations = $associatedUsers; $error = null; try { foreach ($associatedUsers as $giver => $receiver) { $text = sprintf("Hi! You have been chosen to be part of a Secret Santa!\n\nSomeone has been chosen to get you a gift; and *you* have been chosen to gift <@%s>!", $receiver); if (!empty($adminMessage)) { $text .= "\n\nHere is a message from the Secret Santa admin:\n\n```" . strip_tags($adminMessage) . '```'; } if ($adminUserId) { $text .= sprintf("\n\nMessage sent via <@%s>.", $adminUserId); } $message = new ChatPostMessagePayload(); $message->setChannel(sprintf('@%s', $giver)); $message->setText($text); $message->setUsername('Secret Santa Bot'); $message->setIconUrl('https://slack-secret-santa.herokuapp.com/images/logo.png'); $this->sendPayload($message); unset($remainingAssociations[$giver]); } } catch (\Exception $e) { $error = $e->getMessage(); } return new Result($hash, $remainingAssociations, $error); }
/** * Send messages for remaining associations. * * This method is limited to 20 seconds to be able to display nice error message instead of being timed out by Heroku. * * @param SecretSanta $secretSanta * * @throws \RuntimeException */ public function dispatchRemainingMessages(SecretSanta $secretSanta) { $startTime = time(); try { foreach ($secretSanta->getRemainingAssociations() as $giver => $receiver) { if (time() - $startTime > 19) { throw new \RuntimeException('It takes too much time to contact Slack! Please press the Retry button.'); } $text = sprintf("Hi! You have been chosen to be part of a Secret Santa!\n\nSomeone has been chosen to get you a gift; and *you* have been chosen to gift <@%s>!", $receiver); if (!empty($secretSanta->getAdminMessage())) { $text .= "\n\nHere is a message from the Secret Santa admin:\n\n```" . $secretSanta->getAdminMessage() . '```'; } if ($secretSanta->getAdminUserId()) { $text .= sprintf("\n\nMessage sent via <@%s>.", $secretSanta->getAdminUserId()); } $message = new ChatPostMessagePayload(); $message->setChannel(sprintf('@%s', $giver)); $message->setText($text); $message->setUsername('Secret Santa Bot'); $message->setIconUrl('https://slack-secret-santa.herokuapp.com/images/logo.png'); $this->sendPayload($message); $secretSanta->markAssociationAsProceeded($giver); } } catch (\Exception $e) { $secretSanta->addError($e->getMessage()); } }
/** * @param array $settings * @param boolean $lunchesToSlack */ public function __construct(array $settings = array(), $lunchesToSlack = true) { $this->httpClient = new GuzzleHttp\Client(); $this->slackPayload = new CL\Slack\Payload\ChatPostMessagePayload(); $this->userSettings = array_merge(['restaurantsFolder' => __DIR__ . '/restaurants', 'lunchDb' => __DIR__ . '/lunches.db', 'slackNick' => 'lunch', 'slackIcon' => 'http://i.imgur.com/PWxRcm1.png', 'slackChannel' => '#general', 'slackKey' => '1234-5678-9012-345'], $settings); $this->restaurantsFolder = $this->userSettings['restaurantsFolder']; $this->dbClient = new SQLite3($this->userSettings['lunchDb']); $this->slackClient = new CL\Slack\Transport\ApiClient($this->userSettings['slackKey']); $this->slackPayload->setChannel($this->userSettings['slackChannel']); $this->slackPayload->setUsername($this->userSettings['slackNick']); $this->slackPayload->setIconUrl($this->userSettings['slackIcon']); if ($lunchesToSlack) { $this->fetchAndParseLunches(); $this->lunchesToSlack(); } }
public function send(Message $message) { $payload = new ChatPostMessagePayload(); $payload->setChannel($this->channel); $payload->setUsername('Your X'); $payload->setIconUrl('https://avatars.slack-edge.com/2016-01-07/17979318133_8214b25cb250e363ff1a_48.png'); $payload->setLinkNames(true); $payload->setText($message->getText()); $this->apiClient->send($payload); }
protected function notifySlack($message, $project, $env, $user, $resolverArray, $color = "#FFCC00", $update = false) { if ($update) { $payload = new ChatDeletePayload(); $payload->setSlackTimestamp($this->ts); $payload->setChannelId($this->channel); $response = $this->slackApiClient->send($payload); if ($response->isOk()) { if ($response instanceof ChatPostMessagePayloadResponse) { /** @var ChatPostMessagePayloadResponse $response */ $this->ts = $response->getSlackTimestamp(); } } else { // something went wrong, but what? // simple error (Slack's error message) echo $response->getError(); // explained error (Slack's explanation of the error, according to the documentation) echo $response->getErrorExplanation(); exit(1); } } $payload = new ChatPostMessagePayload(); $payload->setChannel("#" . getenv("slack_channel")); $payload->setIconUrl("https://www.dropbox.com/s/ivrj3wcze7cwh54/masterjenkins.png?dl=1"); $payload->setUsername("Master Jenkins"); $attachment = new Attachment(); $attachment->setColor($color); $attachment->setFallback("[" . $project . "#" . getenv("BUILD_NUMBER") . " - branch *" . getenv("GIT_BRANCH") . "* to *" . $env . "* by _" . $user . "_] " . $message); $attachment->setText("[" . $project . "#" . getenv("BUILD_NUMBER") . " - branch *" . getenv("GIT_BRANCH") . "* to *" . $env . "* by _" . $user . "_] " . $message . "\n<" . getenv("BUILD_URL") . "console|Jenkins Console> - <" . getenv("BUILD_URL") . "changes|Changes>" . (isset($resolverArray["shared_package_target"]) && file_exists($resolverArray["shared_package_target"]) ? " - <" . $resolverArray["shared_package_url"] . "|Download>" : "")); $payload->addAttachment($attachment); $response = $this->slackApiClient->send($payload); if ($response->isOk()) { if ($response instanceof ChatPostMessagePayloadResponse) { /** @var ChatPostMessagePayloadResponse $response */ $this->ts = $response->getSlackTimestamp(); $this->channel = $response->getChannelId(); } } else { // something went wrong, but what? // simple error (Slack's error message) echo $response->getError(); // explained error (Slack's explanation of the error, according to the documentation) echo $response->getErrorExplanation(); exit(1); } }