Post a message to a Slack channel
Create a pipe to post messages or notifications to Slack.
This template shows how to set up Slack notifications. The channel and message are passed as parameters to the pipe, so you can use it to post to any Slack channel.
You can set up a serverless function as a simple tool for posting messages on Slack. Common uses for this include:
- Adding a Slack notification feature to your app
- Automatically posting notifications or messages from other web apps in a particular Slack channel
- Posting a notification when an action is triggered in another web app
Flexio.pipe() is called, a local pipe object is created and returned. This pipe object contains information about the pipe, including an array of tasks that will be executed sequentially when the pipe is run.
In this template, a pipe is created and a single
request task is added to the pipe that issues a POST request to Slack’s message API endpoint.
When the pipe is run, the single
request task is executed, telling Slack to post a message to the ‘#general’ channel.
To add your own account, you’ll need to follow three steps: 1) sign up for Slack and generate a Legacy Token, 2) create a connection in Flex.io with your token, 3) update the request call to use your token.
First, sign in to your Slack account (or sign up if you don’t already have one). Next, retrieve or generate a Legacy Token using the using the Legacy Token Generator. Once you have one, you can send a test message by running a flex.io request job with the
url parameter set to
https://slack.com/api/chat.postMessage. The request method needs to be POST, and your token, channel, and message should be specified in the POST form data (parameters ‘token’, ‘channel’, and ‘text’, respectively).
Next, after you’ve sent a test message, you’ll want to hide your Auth Token so that you can send messages, but not expose them in code that you check into your source coderepository. To do this, create a connection in Flex.io by logging into Flex.io, selecting
Connections in the left nav bar, then selecting
New in the upper right of the connection page. Once you’ve created a new connection, give your connection a name and an alias, then enter the Slack API endpoint
https://slack.com/api/chat.postMessage into the url parameter on the connection page. Also select POST as your HTTP method. Next, add some Form Data parameters: ‘token’, ‘as_user’, and optionally ‘channel’. Set token to your Slack token, and as_user to true; ‘as_user’ instructs Slack to post as the user who owns the token; ‘channel’ specifies the Slack channel to post to (for example, #general). You can specify the channel here, or in the request call in your pipe. To finish, press the Create Connection button at the bottom of the page to create the connection.
Finally, once you’ve created a connection, you can use it in the request object by specifying it as the first parameter in the request object using the connection alias and then supplying other parameters:
When the pipe is run, an HTTP request is made using the parameters specified in the connection and any additional parameters in the request function supplementing those defined in the connection.
In the template above, the code snippet is an all-in-one example which sends a single message to Slack. Building on this, you can create a serverless function that will post any message you specify to any channel.
To create a more general serverless function to post Slack notifications, you can parameterize the code like this:
Later on, you can run pipe in the following manner: