Let's see how to post a message with a Slack bot, using Bearer.sh.
- Create a custom API on Bearer.sh dashboard (learn how here).
- Use the configuration below for that custom API
- Create a Slack bot on Slack (link)
- Register the Bot API Key on Bearer (it starts with
xoxb-...
) - Post to a channel using the snippet below
We need to use the Slack API, with an API Key Authentication method.
API Name: Slack API Key
Request configuration:
{
"headers": {
"Accept": "application/json",
"Authorization": "Bearer ${auth.apiKey}",
"User-Agent": "Bearer.sh"
},
"baseURL": "https://slack.com/api/"
}
Authentication configuration:
{
"authType": "APIKEY"
}
// Require the Bearer client for NodeJS
const Bearer = require('@bearer/node').default
// Initialize the client with your Bearer API key
const bearer = Bearer('YOUR_BEARER_API_KEY')
// List channel
function listChannel() {
bearer
.integration('slack_api_key')
.get('/channels.list')
.then(({ data }) => {
console.log('Success:', data)
})
}
function postToChannel(channelId) {
bearer
.integration('slack_api_key')
.post('/chat.postMessage', {
body: { channel: channelId, text: 'Hello, world!' }
})
.then(({ data }) => {
console.log('Success:', data)
})
}
// First list channel
listChannel()
// Then post a message to a channel
// postToChannel('CD83RRS23')