Writing a Salesforce Bot for Slack

This is another installment in my ongoing Slack and Salesforce integration series.

In part 1, I demonstrated how, using a Webhook, Salesforce can post messages to Slack channels when specific events happen (for example, when the status of an opportunity changes).

In part 2, I showed how, using Slash Commands, Slack users can access Salesforce data from within the Slack UI.

In this article, I’ll share an example of Slack and Salesforce integration using bots. Using a bot, you can monitor Slack channels activity and respond to Salesforce requests expressed in natural language.

Watch this video to see the Salesforce bot in action:

To create a bot, you create an application that opens a Websocket connection to Slack. Your application will then receive all the messages from channels your bot has been invited to, as well as the direct messages Slack users sent to your bot.

Your app can then use pattern matching algorithms and natural language processing tools to decide if and how to respond to a message.

To create this simple Salesforce bot, I used Botkit, a node.js-based toolkit that facilitates the creation of bots for Slack (and now Facebook Messenger). Botkit takes care of the low level Websocket plumbing, and provides the basic infrastructure to set up handlers that respond to specific messages.

For example, here is the code to respond to an Account Search request:

controller.hears(['search account (.*)', 'search (.*) in accounts'], 'direct_message,direct_mention,mention', (bot, message) => {
    let name = message.match[1];
    salesforce.findAccount(name)
        .then(accounts => bot.reply(message, {
            text: "I found these accounts matching  '" + name + "':",
            attachments: formatter.formatAccounts(accounts)
        }))
});

Botkit can also be extended using plugins (middleware). For example, you could add a plugin to preprocess messages with a natural language processing service.

Source Code and Installation Instructions

  1. Create a Connected App in Salesforce
  2. Clone this repository
  3. Install the dependencies:
    npm install
    
  4. Create a bot user in Slack
  5. On the command line, define the environment variables used in your Node.js app. On a Mac:
    export SLACK_BOT_TOKEN=your_slack_bot_token
    export SF_CLIENT_ID=your_salesforce_connected_app_client_id
    export SF_CLIENT_SECRET=your_salesforce_connected_app_secret
    export SF_USER_NAME=salesforce_integration_user_name
    export SF_PASSWORD=salesforce_integration_user_password
    
  6. Start Node:
    node server
    
  7. In Slack, select your bot under Direct Messages, type Help to see what you can ask, and start chatting with your bot!

Authentication

This sample bot uses an “integration user” to connect to Salesforce. In other words, all Slack users connect to Salesforce using the same Salesforce user. In a production environment, you’d probably want to map Slack user ids to Salesforce user ids so that requests coming from a specific Slack user can be served using the associated Salesforce user. I’ll demonstrate this approach in a future post.

Make sure you understand the security implications before deploying the application in a production environment.

css.php