How to Send Messages to a Slack Channel Using Bash

An excellent option for users looking to send messages to Slack from a command-line interface

4
How to Send Messages to a Slack Channel Using Bash
CPaaSInsights

Published: June 8, 2022

UC Today News

Technology Journalist

What Is a Bash Script?

A bash script is a text file that contains a series of commands. These commands are executed by the bash shell, which is the default shell in most Linux distributions. Users can use any text editor to write the code when creating a bash script.

The first line of a bash script is typically a shebang (#!) followed by the path to the bash interpreter. This line tells the operating system which interpreter to use to execute the script. The second line is usually a comment that describes what the script does. The bash interpreter does not execute comments, but they can help document your code. The rest of the lines in a bash script are typically commands that the bash interpreter will execute.

Users can use bash scripts to automate tasks at the terminal. For example, users could create a script that deletes all files in a directory older than 30 days — or create a script that checks for updates to the user’s operating system and installs them automatically. To run a bash script, users need to have the bash interpreter installed on your system. Most Linux distributions come with the bash interpreter pre-installed.

Sending Slack Channel Messages Using Bash

Users can transform Slack into a command-line notification center with a few webhooks. This may be used for various purposes, ranging from simple message streams to specific Slack integrations for your app.

Using a Slack Bot with Webhooks

Slack provides an API that allows users to automate many workplace interactions. This API is accessible from any application that can make HTTP queries like POST and GET, including Bash scripts utilizing the curl function in most scripting languages.

The curl tool comes standard with all Linux distributions and may be downloaded for Windows. This API includes webhooks, which allow two apps to communicate messages to one other. The user will be given a webhook URL to which the user can POST some data in the form of a JSON object. Users could, for example, send a brief message with: {“text”: “Hello”}. The Bash script will send this data to Slack, read it and deliver “Hello” to the channel one chooses when they set up the webhook.

Users can send messages to a Slack channel using bash by utilizing their Slack app. This is beneficial because webhooks are typically used for alerts, and a message sent via a webhook will not be sent from the user’s account. This means that the user will receive push notifications for messages sent this way.

Users can use the chat to send automated messages from their Slack “account.postMessage” API and set the as-user argument to true. However, users will need an OAuth 2.0 token with the “chat: write user permission scope” before using the API. Webhooks are generally easier to maintain.

Setting Up a Slack App to Accept Webhooks

● Create a new Slack app by going to Slack’s API gateway. Give this app a name, choose the workspace it will belong to, and click “Create App.”

● Users will be sent to a page where you may customize your bot’s features. Click on the “Webhooks incoming” button to turn it on.

● Create a new webhook for the application when sending messages. At the bottom of the page, click “Add New Webhook to Workspace.”

● Users will be prompted to approve the application and select the messaging channel. These may be public channels or direct messages with other users; the bot will appear in your DMs and get notifications.

● Set up multiple webhooks for various channels; each will have its URL. Open your terminal and copy the URL of your webhook. Copy and paste for example: curl -X POST -H ‘Content-type: application/JSON -data ‘{“text”:”Hello”}’ YOUR_WEBHOOK_URL.

● Replace YOUR_WEBHOOK_URL with the one provided by Slack. You should get a “Hello” notification and a new message in the selected channel when you press enter.

● Use the curl command from within a Bash script, and the JSON object can be configured to convey any data you want, including data from bash variables.

Making Use of Formatted Messages and Attachments

Slack supports several message formats beyond plain text, which can be specified using the markdown syntax in bash. If the user wants to customize the appearance of their messages, they must include more data in the JSON object returned by curl. Remember to use the channel ID and user ID instead of the plaintext names when connecting to channels.

Use Slack’s block kit builder for everything else. It’s a WYSIWYG editor for Slack messages that lets users construct interactive and deeply embedded content and even generates JSON. Users can interact with the program providing the message by setting buttons and menus to initiate POST requests to a URL you specify.

Users can also add attachments to their messages, i.e., blocks of formatted information that appear underneath the message itself. These may include images, links, snippets of code, and more. Attachments are added using the “attachments” property of the JSON object you’re POST-ing to your webhook URL. Thus, a message with an attachment might look like this:

{“text”: “Hello”,

“attachments”:

{“image_url”:”http://example.com/image.png“,

“title”: “This is the title.”

}

}

The “attachments” property is an array of attachments. To add an attachment, the user will need to specify a few properties in the JSON object:

image_url – The URL of the image to be displayed

title – The attachment’s title

text – The attachment’s main text body

color – The color of the attachment, either one of Slack’s default colors or a hex code

fields – An array of short pieces of information; each field includes a title and value

Users can add as many attachments as they want to this array and play around with the Block Kit Builder to get a feel for what’s possible.

Conclusion

Bash can be an excellent option for users looking for a way to send messages to their Slack channel from a command-line interface. Using the tips in this post, users should be able to get up and running with basic messaging in no time. Users can try implementing all of the different possibilities that bash offers.

 

 

User Experience

Brands mentioned in this article.

Featured

Share This Post