Skip to content

arouze/slack-message-builder

Repository files navigation

Slack Message Builder

A builder for Slack message written in PHP.

Objective

Building Slack message using block kit is quite hard. This package will help you to build message easily and will warn you if you don't respect Slack restrictions (Message too large, too much elements in an action block)

Installation

composer require arouze/slack-message-builder

Usage

    // Here an example with the first template of Slack Documentation
    // @see: https://api.slack.com/tools/block-kit-builder?template=1
    // Will return an array of Slack blocks
    // You can post them on the Slack webhook using json_encode
    $blocks = (new SlackMessageBuilder())
        ->addBlock(
            (new SectionBlock())
                ->setTextObject(
                    (new TextObject())
                        ->setType(TextObject::TEXT_OBJECT_TYPE_MARKDOWN)
                        ->setText("You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*")
                )
        )
        ->addBlock(
            (new SectionBlock())
                ->addFields(
                    (new TextObject())
                        ->setType(TextObject::TEXT_OBJECT_TYPE_MARKDOWN)
                        ->setText("Type:*\nComputer (laptop)")
                )
                ->addFields(
                    (new TextObject())
                        ->setType(TextObject::TEXT_OBJECT_TYPE_MARKDOWN)
                        ->setText("*When:*\nSubmitted Aut 10")
                )
                ->addFields(
                    (new TextObject())
                        ->setType(TextObject::TEXT_OBJECT_TYPE_MARKDOWN)
                        ->setText("*Last Update:*\nMar 10, 2015 (3 years, 5 months)")
                )
                ->addFields(
                    (new TextObject())
                        ->setType(TextObject::TEXT_OBJECT_TYPE_MARKDOWN)
                        ->setText("*Reason:*\nAll vowel keys aren't working.")
                )
                ->addFields(
                    (new TextObject())
                        ->setType(TextObject::TEXT_OBJECT_TYPE_MARKDOWN)
                        ->setText("*Specs:*\n\"Cheetah Pro 15\" - Fast, really fast\"")
                )
        )
        ->addBlock(
            (new ActionBlock())
                ->addElement(
                    (new ButtonElement())
                        ->setText(
                            (new TextObject())
                                ->escapeEmoji()
                                ->setText('Approve')
                        )
                        ->setStyle(ButtonElement::BUTTON_STYLE_PRIMARY)
                        ->setValue('click_me_123')
                )
                ->addElement(
                    (new ButtonElement())
                        ->setText(
                            (new TextObject())
                                ->escapeEmoji()
                                ->setText('Deny')
                        )
                        ->setStyle(ButtonElement::BUTTON_STYLE_DANGER)
                        ->setValue('click_me_123')
                )
        )
        ->render();

    // Or you can use the Sender to easily send message in your Slack channels.
    // @see https://api.slack.com/messaging/webhooks to configure a webhook on your Slack instance.
    (new Sender())->sendToChannel(
        'channel-name',
        'SERVICE_ID',
        'CHANNEL_ID',
        'CHANNEL_TOKEN',
        $blocks
    );

Features

  • Action
  • Context
  • Divider
  • File
  • Header
  • Image
  • Input
  • RichText
  • Section
  • Video
  • Button
  • Checkboxes
  • Date pickers
  • Datetime pickers
  • Email input
  • File input
  • Image
  • Multi-select menus
  • Number input
  • Overflow menu
  • Plain-text input
  • Radio buttons
  • Rich text input
  • Select menus
  • Time pickers
  • URL input
  • Workflow button
  • RichTextSection
  • RichTextList
  • RichTextPreformatted
  • RichTextQuote
  • Confirmation dialog object
  • Conversation filter object
  • Dispatch action configuration object
  • Option object
  • Option group object
  • Text object
  • Trigger object
  • Workflow object
  • Slack file object
  • Channel
  • Emoji
  • Link
  • Text
  • User
  • UserGroup

Want to help ? An issue ?

About

A builder for Slack message written in PHP

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages