Slack Salesforce bot kit

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.

Run the following command

Create a new file index.js in root directory and add the following code

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;

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