Skip to content

Slack Notifier utilitizes Slack's Incoming Webhooks feature to post messages directly to a Slack channel of your choosing through your individual Slack app Webhook URL.

License

Notifications You must be signed in to change notification settings

timkippdev/slack-notifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slack Notifier

Slack Notifier utilitizes Slack's Incoming Webhooks feature to post messages directly to a Slack channel of your choosing through your individual Slack app Webhook URL.

Installation

These instructions assume you already have Composer installed on your machine or the server you are working on.

Install through Composer:

composer require timkippdev/slack-notifier

Usage

Creating Slack App and Enabling Incoming Webhooks

See the following instructions provided by Slack to create your app, if needed, and obtain your Webhook URL:

https://api.slack.com/incoming-webhooks#getting-started

Setup SlackNotifier Instance

Once you have your Webhook URL, you can create a new instance of the SlackNotifier class:

// your Webhook URL
$webhookAPI = 'https://hooks.slack.com/services/Txxxxxx/Byyyyyyy/Zzzzzzzzz';

// new SlackNotifier instance
$slackNotifier = new \TimKippDev\SlackNotifier\SlackNotifier($webhookAPI);

You can also choose to invoke the static sendMessageToChannel method instead of creating your own instance.

Sending Messages

Now that you have your SlackNotifier instance created, you can use it to send a message with optional attachments to the channel configured from your Webhook URL.

Simple Message Using Instance Method

$slackNotifier->sendMessage('First message using Slack Notifier!');

Simple Message Using Static Method

We provide a static method to invoke to easily send messages to different channels so you are not tied down to a single instance.

\TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!');

Message with Attachments

For the complete Slack message attachment documentation, see the following link: https://api.slack.com/docs/message-attachments

$slackAttachment = new \TimKippDev\SlackNotifier\SlackAttachment();
$slackAttachment
    ->setAuthorName('Author name goes here')
    ->setAuthorLinkUrl('https://example.com/author')
    ->setAuthorIconUrl('https://img.icons8.com/ultraviolet/48/000000/soy.png')
    ->setColor('#222222')
    ->setFallbackText('Fallback goes here')
    ->setFooterIconUrl('https://img.icons8.com/color/48/000000/soy.png')
    ->setFooterText('Footer text goes here')
    ->setImageUrl('https://img.icons8.com/color/400/000000/soy.png')
    ->setPretext('Pretext goes here')
    ->setText('Attached using Slack Notifier!')
    ->setThumbnailUrl('https://img.icons8.com/ultraviolet/96/000000/soy.png')
    ->setTitle('Title goes here')
    ->setTitleLinkUrl('https://example.com/title');

// with instance method
$slackNotifier->sendMessage('First message with attachments using Slack Notifier!', [
    $slackAttachment
]);

// with static method
\TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!', [
    $slackAttachment
]);

Attachment Actions

$slackActionConfirmation = new \TimKippDev\SlackNotifier\SlackActionConfirmation();
$slackActionConfirmation->setDismissButtonText('Dismiss')
    ->setOkButtonText('Confirm')
    ->setText('Confirmation Text')
    ->setTitle('Confirmation Title');

$slackAction = new \TimKippDev\SlackNotifier\SlackAction();
$slackAction->setStyle('primary')
    ->setType('button')
    ->setText('Click Me with Confirmation')
    ->setUrl('https://example.com/action')
    ->setName('action-name')
    ->setConfirmation($slackActionConfirmation);

$slackAttachment = new \TimKippDev\SlackNotifier\SlackAttachment();
$slackAttachment
    ->setText('Attached using Slack Notifier!')
    ->setActions([$slackAction]);

// with instance method
$slackNotifier->sendMessage('First message attachment containing actions using Slack Notifier!', [
    $slackAttachment
]);

// with static method
\TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!', [
    $slackAttachment
]);

Attachment Fields

$slackField = new \TimKippDev\SlackNotifier\SlackField();
$slackField->setTitle('Field Title')
    ->setValue('Field Value');

$slackAttachment = new \TimKippDev\SlackNotifier\SlackAttachment();
$slackAttachment
    ->setText('Attached using Slack Notifier!')
    ->setFields([$slackField]);

// with instance method
$slackNotifier->sendMessage('First message attachment containing fields using Slack Notifier!', [
    $slackAttachment
]);

// with static method
\TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!', [
    $slackAttachment
]);

About

Slack Notifier utilitizes Slack's Incoming Webhooks feature to post messages directly to a Slack channel of your choosing through your individual Slack app Webhook URL.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages