Slack Integration with 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;

When you first connect, tell dashbot and save the bot and team locally

request('https://slack.com/api/rtm.start?token=' + process.env.SLACK_BOT_TOKEN, (error, response) => {
    const parsedData = JSON.parse(response.body);
    
    // Tell Dashbot when you connect.
    <strong>dashbot.logConnect(parsedData);</strong>
    const bot = parsedData.self;
    const team = parsedData.team;
    ...
})

When you receive a message on the websocket, tell Dashbot — passing bot, team, and message

connection.on('message', (message) => {
    const parsedMessage = JSON.parse(message.utf8Data);
    
    // Tell Dashbot when a message arrives
    <strong>dashbot.logIncoming(bot, team, parsedMessage);</strong>
})

When you send a reply on the websocket, tell Dashbot — passing bot, team, and reply

const reply = {
    type: 'message',
    text: 'You are right when you say: ' + parsedMessage.text,
    channel: parsedMessage.channel
};

// Tell Dashbot about your response
<strong>dashbot.logOutgoing(bot, team, reply);</strong>

connection.sendUTF(JSON.stringify(reply));

Example

See a complete example.