Slack Integration with Botkit and NPM
Create a bot API key
Each bot needs its own API key for tracking.
Create a bot to get an API key.
Install Dashbot via NPM
npm install --save dashbot
Include Dashbot
Use the API key created above.
const dashbot = require('dashbot')(process.env.DASHBOT_API_KEY).slack;
After you create your Botkit controller, simply add send and receive middleware
const controller = Botkit.slackbot();controller.configureSlackApp(...);// Add the dashbot middlewarecontroller.middleware.receive.use(dashbot.receive);controller.middleware.send.use(dashbot.send);
Slack Buttons
Starting with this slack button example, here is how you add Slack Button reporting to your bot.
Since Botkit does not send button interactions through its middleware, we need to log the button presses and interactive replies when they are made.
When you receive and send an interactive message, log it (you also must set the channel on the reply):
controller.on('interactive_message_callback', (bot, message) => {dashbot.logIncoming(bot.identity, bot.team_info, message);...replyMessage.channel = message.channel;dashbot.logOutgoing(bot.identity, bot.team_info, replyMessage);bot.replyInteractive(message, replyMessage)
If you set the interactive_replies: true
option in botkit, the incoming message is received by the middleware, but you still must send dashbot the outgoing message with the channel set:
controller.on('direct_message', (bot, message) => {...replyMessage.channel = message.channel;dashbot.logOutgoing(bot.identity, bot.team_info, replyMessage);bot.replyInteractive(message, replyMessage)
Example
View sample code for a complete Slack bot example.