Connecting our bot to Slack

Back in the Slack console, under the Event Subscriptions page, we will add the endpoint of our chatbot API to the Request URL field. In the following, you can see we are receiving a 500 error:

Adding a request URL resulting in an error

This happens because, when we subscribe to events, Slack will send a URL verification object to the request URL. Our code doesn't cater for this yet, so we need to add that as a feature. The object that Slack sends for verification looks like this:

{
"token":"GveYU40cwJ3NPGNM4MnVYHEe",
"challenge":"Bo0A0QRp5Z7lUILCWfIA284rCQ32cyAkfADRyVNQRopVb2HIutgv",
"type":"url_verification"
}

What our function code needs to do is respond with the plaintext value of the challenge key. The key changes every time you retry the validation, so you shouldn't need to store this anywhere. The idea of this challenge is to provide the other side of our handshake so that Slack knows that our endpoint is legitimate. You can find more details on this in the link in the Further reading section.

Once you've responded to the URL verification handshake, you can continue to subscribe to events. The one we are interested in is the app_mention event. This will trigger an event to be sent to our API method with all of the details of the mention.

A mention in Slack is when you address someone directly using @, followed by their username.

What we want our bot to do is respond to a question such as Hey @configbot, what are the open ports fori-06dd90c34b8f87d76?. Every time we use the @ mention, our endpoint will receive an event with contextual information about the mention (who it was from, what the text was, when the time was, and so on).

Once we've got the details coming in, we can pull out the requested instance ID to look up security groups and find the open ports. Then, we need to send those details by making a request to the Slack Web API.

To make it easier to do this, Slack has a nice Node.js SDK that we can use. Import it at the top of your function using the following command:

const Slack = require('slack');

You'll also need to install the package in your working directory using the Node Package Manager:

npm install @slack/web-api

To use SDK to post a new chat message, use the following function:

const sendSlackMessage = (channel, message) => {
let params = {
token: process.env.BOT_TOKEN,
channel: channel,
text: messgage
}

return Slack.chat.postMessage(params);
}

In the params object in the preceding function, you will notice that the token key has a value of process.env.BOT_TOKEN. This means that the function will look for the value of this in the BOT_TOKEN environment variable. We need to provide our bot user's OAuth token as a parameter for the postMessage function to authenticate properly.

This value should be kept secret, so where should we store it? I've chosen to store mine in the Amazon Systems Manager Parameter store under a parameter I named slack_bot_token. Now, we can add a reference to this in our serverless.yml file so that we don't have to store secrets directly in source control. Add the following to your function configuration in serverless.yml. At deploy time, the Serverless Framework will pull this parameter and add it as an environment variable for our Lambda function to use:

environment:
BOT_TOKEN: ${ssm:slack_bot_token}

Remember to run another deployment after you make this change. 

Let's test this out in Slack. I've created a new channel called #application-support and invited our old friend @configbot. When I ask the question, I get a response right away, and I can see the open ports on my instance. Nice!

 

Testing configbot in a channel in Slack

Now that we have the brain as a base, we can extend our configbot so that it's smarter and can respond to more things.

That concludes this section, where we learned about what a rules-based bot can do and how they can be used to assist operations activities. We also learned how to create an integration with Slack. We will be able to use this knowledge to build new functionality in our chat channels.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190