Create a Slack Bot using AWS Lex

For instance, our WebGate internal slack bot can query data from various sources like group calendar, employee directory, vacation requests, CRM contacts, etc. Employees can query information just by asking a question, from the slack desktop- or mobile app, and instantly get results. 

In this blog entry we will show how to create a slack bot application using AWS Lex as the backend for conversational dialog services. 

Let's start by creating a simple Lex conversation bot in the AWS console

Open AWS Lex in one of the supported regions to create a Lex bot. You have the option to select one of the samples, or "create your own".

We select "Custom bot" and enter a name for the bot. Then we change the output voice to "text only bot" since this bot just sends back text messages to the slack channel. 

In the Lex Bot Editor we can create our first intent by clicking the "Create Intent" button. 

Let's create a simple "SayHello" intent to test interaction with our bot. 

Enter some sample utterances that will trigger this intent. 

 

This intent will just return predefined responses. In the "Response" section enter some responses using the "Add Message" button. The messages will be returned at random, when our SayHello intent was detected.

Don't forget to click on "Save Intent" at the bottom of the page!
Before we can test our bot, we first have to click the "build" button in the upper right corner. When building has completed, expand the chat window in the right corner and enter a sentence:

AWS Lex supports versioning, this allows you to implement and test new features without affecting the productive version your users are using. Switch to the Settings tab and create at least an alias for "prod". When you release a new version of your bot using the "publish" button, you can assign a version to the alias here, and also jump back to a previous version if something didn't work as expected.

We now have a very basic AWS Lex Bot, which for now just can reply "Hello". Before you continue to the slack integration, publish the current bot to the "prod" alias. 

Integrating the bot into Slack

We will build a private (not distributed) bot app, accessible just for our own workspace by all employees of the company. 
Open the Slack API page and create a new application:

https://api.slack.com/apps

In the application settings console, add "bot user" and assign a username for the bot. Also enable "always show as online". 

Go to "interactive components" and enable interactivity. 

Enter slack.com as the request URL, we will change that one later when we are done with the AWS slack channel setup.

Go back to the "Basic settings" page and copy the client ID, the Client Secret and the Verification Token, we will need this information for the AWS Lex channel setup.

Head back to the AWS Lex Bot management console, open your lex bot and switch to the "Channels" tab and open "Slack" as the channel to create.

Assign a channel name and a description. Select the KMS key (head to the IAM console and create a key if the dropdown is empty), select "prod" as the alias to bind this channel to. Enter the Client ID, Client Secret and Verification Token from the slack application basic settings page, then activate your channel integration. 

You will be provided two callback URLs: a Postback URL and a OAuth URL.


Back in the slack application configuration open "OAuth & Permissions" and add the OAuth URL from the AWS Lex bot, provided in the previous step, by selecting "add new redirect URL", then save the changes. 

On the same page, scroll down to the "Scopes" section and from the "select permission scopes" inputbox/dropdown select:

- chat:write:bot
- team:read

Just type the values above into the selection box, then select the corresponding entry. Don't forget to save the changes.

Open the "Interactive Components" page and update the Request URL to the Postback URL provided by the Lex bot channel, then save URLs.

Open the "Event Subscriptions" page and enable event subscriptions. Set the Request URL to the Postback URL by the Lex bot channel. 

Scroll down to the "Subscribe to bot events" section and subscribe to the "message.im" event. This will forward messages directed towards the bot user to the AWS Lex channel. Don't forget to save the changes. 

 

You can now also configure appearance of the bot in the section "Display Information" of the basic settings menu, to assign an icon, description and colors. When done we are ready to test the slack bot by distributing it to the slack workspace. 

To activate the slack bot, open the page "Manage distribution" and select "Add to slack". 

In your slack team workspace, select "Direct messages" option and start typing the name of your new bot. 
Now you should be able to talk to your bot.

The Lex Bot for now just knows one single intent, the SayHello intent. You can add more complex and interactive interactions by creating more Intents and Slots and forwarding specific Intents to AWS Lambda functions as the backend instead of statically linked responses, but that will be part of another blog entry.

The steps to link the AWS Lex Bot channel to the Slack bot application are outlined here in the AWS documentation.

Kommentar einfügen: