Introduction
In this blog, I am going to explain how to set up chatbot with salesforce and Slack. Nowadays Chatbots are gaining more and more popularity thorough conducts a conversation via auditory or textual methods.we will build chat bot for Slack using Botkit — a popular and open source bot development kit written in node.js jsforce for Salesforce integration with node.js.
Create a bot in Slack
Login you Slack account and go to bot configurations page, enter “any name you want ” in the username field and click Add Bot Integration.
you will be brought to an edit configuration page. Under Integration Settings section, there is an API Token which we will use it for later in the code for authentication.
Setup configuration
we are built on node js you can do it by npm init and npm install packages. but here is the package.JSON file which you can use.
{ "name": "slacksalesforce", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "botkit": "^0.6.3", "express": "^4.16.1", "http": "0.0.0", "jsforce": "^1.8.0" } }
Run the following command
npm install
Create a new file index.js in root directory and add the following code
var Botkit = require('botkit'); var jsforce = require('jsforce'); var conn = new jsforce.Connection({ loginUrl: 'https://login.salesforce.com' }); var username = '<Your Salesforce ID>'; var password = 'Password +Token'; conn.login(username, password, function (err, userInfo) { if (err) { return console.error(err); } }); var controller = Botkit.slackbot(); var bot = controller.spawn({ token: "xoxb-<Slack Token>" }) bot.startRTM(function (err, bot, payload) { if (err) { throw new Error('Could not connect to Slack'); } }); controller.hears(['help', 'Salesforce '], 'interactive_message_callback,direct_message,direct_mention,mention', function (bot, message) { bot.startConversation(message, function (err, convo) { if (!err) { convo.say('Ok . Let me Help you ! '); convo.ask('Which Object data , say Contact or Opportunity or Account?', function (response, convo) { if (response.text == 'Contact') { convo.say('Ok .You are looking for ' + response.text + ' data '); convo.ask('Can i have contact Name ?', function (response, convo) { conn.query("SELECT Id,Title, Name,email ,Phone FROM Contact where name=\'"+response.text+"\' Limit 1", function (err, result) { console.log(result); var name = result.records[0].Name ; var email = result.records[0].EMail ; var phone= result.records[0].Phone; var id = result.records[0].Id ; console.log(name); bot.reply(message, { attachments: [ { "fallback": "Required plain-text summary of the attachment.", "color": "#36a64f", "pretext": "Contact Details are here", "author_name": name, "title": "Contact Details", "title_link": "https://fscttt-dev-ed.my.salesforce.com/"+id, "text": "Details are here ", "fields": [ { "title": "Name", "value": name, "short": false }, { "title": "Phone", "value": phone, "short": false }, { "title": "Id", "value": id, "short": false } ], "footer": "Slack API", "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png", "ts": 123456789 } ] }); convo.say("Ok! Goodbye."); convo.next(); }); }); } if (response.text == 'Account') { convo.say('Ok .You are looking for ' + response.text + ' data '); convo.ask('Can i have Account Name ?', function (response, convo) { }); } if (response.text == 'Opportunity') { convo.say('Ok .You are looking for ' + response.text + ' data '); convo.ask('Can i have Opportunity Name ?', function (response, convo) { }); } convo.next(); }); // store the results in a field called nickname convo.on('end', function (convo) { if (convo.status == 'completed') { bot.reply(message, 'OK! I Hope you find the infomration'); } else { bot.reply(message, 'OK, nevermind!'); } }); } }); }); controller.hears('^stop', 'direct_message', function (bot, message) { bot.reply(message, 'Goodbye'); bot.rtm.close(); });
Now go to your slack channel and type help to start interacting with the bot as shown below. As of now, the code is working to fetch contact details to slack but you can extend to other levels.
Understand the Botkit
Before we make it be a real chatbot, let’s dive into the code and see how Botkit works. Open the index.js file and you will see a lot of controller.hears() functions. In fact, hears() is a function provided by Botkit to listen messaging events based on matching keywords in message text and those are help and Salesforce
Here’s a simple example of hears() function;
controller.hears(['help', 'Salesforce '], 'interactive_message_callback,direct_message,direct_mention,mention', function (bot, message) { bot.startConversation(message, function (err, convo) { } })
I’ve added indirect_mention,direct_message,interactive_message_callback
this parameter so this function can be fired when I say “@salesforce “ in the channel
Here’s the full list of parameter options from Botkit github:
Event | Description |
message_received | This event is fired for any message of any kind that is received and can be used as a catch-all |
ambient | Ambient messages are messages that the bot can hear in a channel, but that does not mention the bot in any way |
direct_mention | Direct mentions are messages that begin with the bot’s name, as in “@bot hello” |
mention | Mentions are messages that contain the bot’s name, but not at the beginning, as in “hello @bot” |
direct_message | Direct messages are sent via private 1:1 direct message channels |
The third parameter is a callback receiving bot and message
the bot can be used to reply the received message using the functionreply
(message, 'response text')
.
Apart from that, bot holds functions and data of slackbot and joined channels.
You can find the code here https://github.com/rajamohanvakati/Slack-Salesforce