Making A Simple Slack Application (Part 1)

Making A Simple Slack Application
Slack socks, San Francisco, California, November 21st, 2014.

Slack is powerful and great tool for communication at workplace. Slack has also exposed their powerful APIs which lets their users make awesome stuff like Giphy, /todo App etc.

There are also bots and apps which automate some of your tasks to help you impress your boss. A bot making startup just grabbed 4 million in investment, so it is a serious business.

Here, I will be making app using Node.js and hosting it on Heroku platform. For going ahead in the tutorial you need to have basic knowledge of Node.js, NPM and JavaScript.

You will also need to make an account on Slack (Free until some limits which won’t matter until you are just using for making an app) and an account on Heroku.com (Free, but consider buying if you are building app for commercial use) to host the app.

Making An Integration

Slack refers to an app as an integration when its use is limited to one team only. First off we will start off with making an integration for a team which we will convert to an app in later on. One major difference between Slack App and Slack Integration is that integration does not need to predefine permissions.

Steps to make an integration:

  • Step 1: Go to Slack API Homepage

  • Step 2: Click “Start building a custom integrations”

  • Step 3: Click “New /command”

    Making Slack Integration
    Making Slack Integration

  • Step 4: Click “Add Configuration”

  • Step 5: Give a slash command

    Give a slash command with which we will call our app. For example, I am making an app which will send a message(like a declaration) to users or channels he/she mentions: /ideclare
    Keep the page open. As we need to fill out “External URL” field and we will need the “Token” field for our application
  • Step 6: Write the Node Script

    Now here is the main part, the brain of the integration. We will write a Node.js script which will handle the incoming messages from Slack and reply accordingly.

    We are going to make two simple functions:

    • Help command (Slack App must have a help command!):
      /ideclare help
    • Declaration command:
      /ideclare @someuser #general -m"I DECLARE BANKRUPTCY!!!"

    Lets get basics up for the script first! Setup NPM and packages we need to install from command line:

        npm init
        npm install http --save-dev
        npm install express --save-dev
        npm install request --save-dev
        

    So, now that we have necessary packages installed, we will start building the script. First of, we need to see what will slack send when we enter a slash command. For example, when you enter:

         /ideclare something
        

    It will POST a message(JSON message) to our URL(the External URL):

    token=gIkuvaNzQIHg97ATvDxqgjtO
    team_id=T0001
    team_domain=example
    channel_id=C2147483705
    channel_name=test
    user_id=U2147483697
    user_name=Harshal
    command=/ideclare
    text=something
    response_url=https://hooks.slack.com/commands/1234/5678
        

    Here’s the code snippet which will be deployed on Heroku. The TOKEN variable is to be set from Slash Command’s settings page:

    Token is important for security
    Token is important for security

    Token is important for the security, as from that only you will know that request is coming from Slack. So, do not share or show your token anywhere else or your application may get compromised.

         var http = require("http"),
             express = require("express"),
             request = require("request"),
             TOKEN = "YOUR_TOKEN_FROM_APPLICATION_SETTINGS",
             //Slack accepts time stamp in Unix Epoch Time
             timeStamp = Math.floor((new Date).getTime()/1000),
             app = express();
    
        app.post('/ideclare',function(req,res){
            //Check if request coming from slack
            if(req.body.token === TOKEN){
                var response_url = req.body.response_url,
                    timeStamp = Math.floor((new Date).getTime()/1000),
                    arr = req.body.text.split(" ");
        
                //Check if the first word is "help"
                if(arr[0] === "help"){
                     res.setHeader('Content-Type', 'application/text');
                     res.send("iDeclare Help!");
                     request.post(response_url ,{
                         json:{
                             //There are two types of "ephemeral" and "in_channel"
                             "response_type": "ephemeral",
                             "attachments": [{
                                 "fallback": "/ideclare (@username | #channel) -m'Your Declaration' /ideclare help",
                                 "title": "iDeclare Commands:",
                                 "mrkdwn_in":["text"],
                                 "text": ":small_blue_diamond:`/ideclare (@username | #channel) -m'Your Declaration'`  "+
                                    "_(For example: `/ideclare @john #general -m'Meeting at 5pm'`)_ \n" +                 
                                ":small_blue_diamond:`/ideclare help` Lists available commands \n"+
                                    "For more features, visit: ",
                                 "footer": "iDeclare",
                                 "footer_icon": "https://pbs.twimg.com/profile_images/2589963899/rlp8wuwga0407z2cgquj_bigger.png",
                                 "ts": timeStamp 
                             }]
    
                         }
                     },function(error,response,body){
                         //Handling Errors
                         if(error){
                             throw err;
                         }
                     })
                 }
             }
         })
        

    For more reference: Slack Slash Commands
    For more on how to format messages: Message Formatting

  • Step 7: Deploy the app on Heroku

  • Step 8: Now in “External URL” give the url of your Heroku app: http://your-app-0000.herokuapp.com/

Try it out entering /ideclare help, after saving the integration.

In the next part, I will expand on this Slack Application more on how to send message to Slack Channels and Slack Users in your team.

Be the first to comment

Leave a Reply

Your email address will not be published.


*