Step 9: Access Slash Command Parameters

Step 9: Access Slash Command Parameters

Next we will take a look at the information that Slack sends us when we receive a request from a Slash command

1. Update the app.post(/test..) function in index.js

  • Inside the our app.post(/test) function, change the console.log(req) to console.log(req.body)
  app.post('/test', async (req, res) => {
    try {
      console.log(req.body) // <-- update this line
      const data = {
        'response_type': 'in_channel',
        'text': 'Testing testing 123!'
      }

      res.json(data)
    } catch (e) {
      console.log(e)
    }
  })
  
  • Restart your node server

  • Navigate to your Slack workspace and make another slash command to but this time add a name for example, /test Fred Flintstone

2. Navigate to your node server tab in your command line application and observe the output

  • Let’s see the result of console.log(req.body)

  • Navigate to the node server tab in your command line application and you should see something similar to the following

{
  token: 'CePdTWPzpIXUufKUL3UJhaVx',
  team_id: 'TN43P8U3X',
  team_domain: 'reemcoworkspace',
  channel_id: 'CN17QEB2M',
  channel_name: 'slackbot-testing',
  user_id: 'UN43P8UCD',
  user_name: 'kareem.grant',
  command: '/test',
  text: 'Fred Flintstone',
  response_url: 'https://hooks.slack.com/commands/TN43P8U3X/755841282464/Xu0N0r65xVmo3dbMgD83xUq7',
  trigger_id: '755841282544.752125300133.615d84c666fefe471d23a7e53bf50f07'
}
  • Note that the parameter “Fred Flintstone” is available to us through req.body.text in the object we get back from Slack

3. Update your response back to Slack in include the parameter

We are going to update our response back the Slack to the parameter that was sent along with the slash command (`text: ‘Fred Flintstone’)

  • Update the app.post(/test..) function
  app.post('/test', async (req, res) => {
    try {
      console.log(req.body)
      const data = {
        'response_type': 'in_channel',
        'text': `Testing testing 123! ${req.body.text}` // <-- update this line
      }

      res.json(data)
    } catch (e) {
      console.log(e)
    }
  })
  

4. Make a slash command to /test Fred Flintstone in your workspace

  • Let’s see if we can successfully send back data we received from Slack in our response

  • In your workspace, make the following slash command: /test Fred Flintstone

  • You should now see: Testing testing 123! Fred Flintstone

inline