GithubHelp home page GithubHelp logo

timkippdev / slack-notifier Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 31 KB

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: MIT License

PHP 100.00%

slack-notifier's Introduction

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
]);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.