How one can Send a Message to Slack From a Bash Script

How one can Send a Message to Slack From a Bash Script

With the lend a hand of just a few webhooks, that that you simply might perchance turn Slack into a notification middle for your roar line. There’s loads that that you simply might perchance assemble with this, from easy message feeds to customized Slack integrations for your app.

This roughly integration works namely effectively for notification-primarily based fully events, bask in when something updates, or fresh content material goes are living someplace, comparable to how RSS feeds operate. You’re going to be ready to also assemble customized integrations for your have apps that work over the identical API, even including interactive dialogs and menus to have person enter.

Expend a Slack Bot With Webhooks

Slack has an API that enables you to automate a complete lot of interactions along with your workspace. You’re going to be ready to discover right of entry to this API from anyplace that enables you to assemble HTTP requests bask in POST and GET, which accommodates bash scripts utilizing the curl utility, as effectively as most scripting languages. The curl utility is installed by default on nearly all Unix distros, and it’s readily available in the market for get on Dwelling windows.

Piece of this API entails webhooks, a technique for 2 functions to send every other messages. You’ll be given a webhook URL which that that you simply might perchance assemble a POST search info from to, with some info in the create of a JSON object. Shall we embrace, that that you simply might perchance send a short message with:

{"text":"Hello, World!"}

Your bash script will send this little bit of records off to Slack, which is able to be taught it and send “Hello, World!” to the channel you specify can absorb to you dwelling up the webhook.

You’ll must exercise your have Slack App, so the message will reach from a bot (which that that you simply might perchance customize). Right here is largely a correct thing; webhooks are primarily weak for notifications, and since a message despatched through a webhook acquired’t be despatched out of your have legend, you are going to discover push notifications for messages despatched this form.

You’re going to be ready to also send computerized messages out of your have Slack legend in a identical manner, moreover you’ll must exercise the chat.postMessage API with the as_user argument dwelling to factual. This would require an OAuth 2.0 token with the chat:write:person permission scope, which that that you simply might perchance must generate your self sooner than utilizing the API. We recommend sticking with webhooks, as they’re a lot more straightforward to rearrange.

Location Up A Slack App to Settle for Webhooks

Navigate to Slack’s API portal and produce a brand fresh Slack app. Give this app a username, have out the workspace this might possibly perchance perchance also merely belong to, and hit “Make App.”

You’ll be introduced to a homepage the build that that you simply might perchance add parts to your bot. There’s a complete lot of wintry stuff here, bask in customized /commands and interactive messages, but all that that you simply might perchance perchance presumably like pretty now might possibly perchance perchance be “Incoming Webhooks.” Click on this button, and switch it on.

You’ll must register a brand fresh webhook for your application to send messages to. Click on “Add Fresh Webhook to Workspace” at the bottom of this veil.

You’re going to be requested to authorize the applying, and have out the channel for the messages. These can even be public channels or declare messages with other customers; you each will scrutinize the bot to your DMs and get notifications.

You’re going to be ready to register a complete lot of webhooks for assorted channels, but every hook can absorb a assorted URL.

Replica your webhook’s URL, and begin up your terminal. Paste in:

curl -X POST -H 'Notify-form: application/json' --info '{"text":"Hello, World!"}' YOUR_WEBHOOK_URL

Replacing YOUR_WEBHOOK_URL with the one Slack gave you. Hit enter and likewise that that you simply might perchance absorb to tranquil get a “Hello, World!” notification and scrutinize a brand fresh message in the channel you chose.

You’re going to be ready to exercise this curl roar from interior a bash script, and likewise that that you simply might perchance configure the JSON object to send irrespective of records you’d bask in, including from bash variables.

Utilizing Formatted Messages and Attachments

The message API can assemble more than correct send easy text. Whenever you happen to’d bask in your messages to be more stylized, you’ll must add some extra parameters to the JSON object that curl sends out.

You’re going to be ready to exercise markdown and other easy formatting in the identical manner as that that you simply might perchance perchance presumably from Slack’s message field. One thing to prove though is that for linking to channels and @ing customers that that you simply might perchance must specify the channel ID and person ID rather than the plaintext names, or else this might possibly perchance perchance also merely no longer work.

For everything else, you’ll wish to exercise Slack’s block kit builder, a WYSIWYG editor for Slack messages that enables you to assemble interactive and richly embedded content material, and even handles developing the JSON for you. Parts bask in buttons and menus can even be configured to send POST requests to a URL you specify, letting your customers talk wait on to the applying sending the message.

The block kit builder isn’t fully primary though, for the reason that weak programs aren’t being deprecated. Whenever you happen to’d correct bask in to exercise parts bask in Attachments, that that you simply might perchance exercise the weak Message Builder to assemble and preview the JSON.

Read Extra