Slack OAuth2 API

This is an example script which shows sending a message to a slack channel.

You can start with this script and customize it to your needs.

For this script to work you’ll need to follow the below steps to create a custom app bot in Slack. This gives you two key string tokens that you will need to enter in lines 1 and 2 below:

  • The Bot User OAuth Token for your custom app which is used to authenticate your slack message.

    • For example ‘xoxb-4799146583-2404089022096-LvGW1SpbHLXTuax2eW54qit6’

  • The Channel ID of the slack channel that you want the message written to

    • For example 'G014VCT54UD'

The values for your company will be different since you’ll have to create and authorize the application for your slack instance. This is the script:

local Token = "Bot User OAuth Token" -- REPLACE ME local ChannelId = "Slack Channel ID" -- REPLACE ME function main(Message) if Message == '' then Message = "Test Example" end local Result = net.http.post { url = "https://slack.com/api/chat.postMessage", parameters = {channel=ChannelId, text=Message}, headers = {authorization="Bearer "..Token}, live=true} -- Change to true to test live in editor trace(Result); end

It takes about 5 minutes to create a basic custom slack app which is:

  1. Authorized within your organization’s Slack instance.

  2. Can have the permission to write to a Slack channel you want the notifications to go to.

At the end of it you should with two values to put into the Token and ChannelId variables in the above script.

Go to https://api.slack.com/

You’ll need to sign into this website using your Slack Account credentials. It might be necessary to navigate back to https://api.slack.com/

Then click on Create an App - this is all over the website.

This should bring up this dialog. It’s best to use the From scratch option:

Then select a name for your app and your company workspace - in this example I select iNTERFACEWARE but you will need to select your own company.

In the next screen you’ll need to add permissions to allow your app to send a message to Slack.

Click on Permissions. Then scroll down to “Scopes”

Click on Add an OAuth Scope and you’ll need to add the chat:write scope:

You should end up with something like:

Then scroll up to click on Install to Workspace:

You will then get a Bot User OAuth Token, like this:

You can copy that token in the slack script we above. You’ll need to do two more things to enable this custom app to write to a specific Slack Channel:

To apps in Slack:

Use the Add apps button to add the app to your company works space.

Then right click on the app to get this screen:

And click on Add this app to a channel:

Click Add.

Then to find the Channel ID of the above channel, right click on it in the channel GUI, and click on Open channel details. At the bottom of the dialog which opens up you should see the channel ID - should be something like:

G014VCT54UD

That’s the ChannelId you need for the script at the top of this page. You should be ready to go! Here’s the script again:

local Token = "Bot User OAuth Token" -- REPLACE ME local ChannelId = "Slack Channel ID" -- REPLACE ME function main(Message) if Message == '' then Message = "Test Example" end local Result = net.http.post { url = "https://slack.com/api/chat.postMessage", parameters = {channel=ChannelId, text=Message}, headers = {authorization="Bearer "..Token}, live=true} -- Change to true to test live in editor trace(Result); end