Skip to content

Instantly share code, notes, and snippets.

@Frenchcooc
Last active September 4, 2019 10:13
Show Gist options
  • Save Frenchcooc/eadaedaa4e40a4c75a064071a8efa221 to your computer and use it in GitHub Desktop.
Save Frenchcooc/eadaedaa4e40a4c75a064071a8efa221 to your computer and use it in GitHub Desktop.

Let's see how to post a message with a Slack bot, using Bearer.sh.

Steps

  1. Create a custom API on Bearer.sh dashboard (learn how here).
  2. Use the configuration below for that custom API
  3. Create a Slack bot on Slack (link)
  4. Register the Bot API Key on Bearer (it starts with xoxb-...)
  5. Post to a channel using the snippet below

API Configuration

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"
}

Snippet to post a message

// 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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment