In conventional web site administration, web developers build an admin site to perform daily tasks such as query how many members register daily and block a user. The admin site development includes frontend and backend codes.
The slack developer can do the same thing without any frontend code. Starting from Feb 2019, Slack provides a very powerful and beautiful UI component (Block UI):
In this blog, we will develop a Slack bot to tell you how many users register today, or block a user. Take a look to below video:
What is Slack?
Slack is a collaboration hub where you and your team can work together. Like Whatsapp group, our team 50% of workers are working remotely and all communicate in Slack workspace.
Why Slack?
Slack provides SDK for developers to develop applications. Below sections will show you how to develop a Slack Bot (Chatbot application). It works like a colleague to perform backend tasks. The bot can recognize below messages:
- How many users registered today?
- Block user NNN
Flow Diagrams
The programming logic to handle “How many users register today”:
The programming logic to handle “Block user NNN”:
Prerequisite
To develop the App, we will use below languages, frameworks, and libraries:
- NodeJS v8.10 or above
- ExpressJS v4 or above
- MongoDB v3 or above
- Slack SDK for Nodejs
- RiveScriptJS v2
Let’s start to develop the source codes step-by-step.
Create an Express app
Initialise the project
$ npm init -y
$ npm install express --save
Modify the package.json as below:
Create the application file app.js:
Load some dummy data
Install MongoDb driver for NodeJS:
$ npm install mongodb --save
Create an initdb.js file to insert some demo data:
Run the initdb to create the demo records:
$ npm run initdb
Define the conversations
We’ll use RiveScript for NLP. With rivescript, we can easily to develop a chatbot. Rivescript takes care of natural language processing. We just need to define the conversations as below brain/app.rive file:
Module for handling RiveScript
Install the RiveScript package:
$ npm install rivescript --save
Create a rivescripthandlers.js:
Develop the webhook for Slack
Install the Slack SDK for NodeJS:
$ npm install --save body-parser @slack/web-api
Run and Debug the App at local
I use Ngrok to make a HTTPS tunnel for Slack to call the local server directly. Run below commands:
$ npm start
Then open another command-line session, and run the Ngrok:
$ ngrok http 3000
Create and register the Slack App
We’re going to register our App in Slack (you’ll need a Slack account). Go to Slack API website https://api.slack.com/apps. Click Create New App:
Enter a new App name and select the workspace you’re working on. Click Create App button. Then the basic information page appears. Click Bots, as below:
Then click Add a Bot user, enter the Display name and default user name, as below:
Next step is to enable event subscription, click Event Subscriptions at the left side as below:
Switch on the Enable Events. Copy & paste the https address which is generated by Ngrok and append /slack/events to the URL, see below:
Slack calls to your App to verify the App immediately. It should be verified successfully as below:
Scroll down to Subscribe to Bot Events section and click Add Bot User Event, select the app_mention:
Click Save Changes:
Next is to enable Interactive Components, click the Interactive Components on the left side and enable it. Copy & paste the URL again but append /slack/actions to the path as below:
The last step is to install the App in the Slack workspace. Click Install App at the left side, then install Install App to Workspace:
Click the Authorize:
After that, we’ll need the token:
Paste the token into /config.json upper portion:
{
"Slack": {
"BotUserOAuthToken": "<Your Token>"
}
}
Now the bot is ready to go!
Chat with the App
Open the Slack workspace, we’ll see the App appears. Thus, we can chat with it:
At the first time, you’ll need to invite the bot to the channel:
Say hello again to the App, it replies:
Conclusion
Slack announced Block UI in Feb 2019. It not only allows us to build robust frontend components (without any codes) such as button, combobox, dropdown, date picker… but also provides Block Kit builder online to build the UI visually. We just need to concentrate the backend logic in two webhooks to handle:
- Messages from Slack
- UI component actions (e.g. button clicked)
The source codes are hosting in this Github repo
Enjoy!
ReplyDeletei am very happy to read this blog. that is nice
for more information
email signature management office 365