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.

25 Responses to Writing a Salesforce Bot for Slack

  1. Mike L April 20, 2016 at 2:21 pm #

    Thanks for this post Christophe! So with this method you can create new accounts — is the process much different for editing accounts? I want to be able to change the status of a certain deal in Salesforce via Slack instead of having to go in and edit the field. Is there a big difference from what you’ve described here?

  2. propchill May 16, 2016 at 7:56 am #

    Expenses of property and real estate in Bengaluru have been enduring for as far back as 2 years, regardless of the way that the land market in India has been experiencing a droop. Bengaluru has possessed the capacity to hang on against this downtrend because of various variables. The fundamental reason is that it is an end client driven market instead of a financial specialist pushed market. The land designers in Bengaluru have focused on the mid fragment and moderate lodging which holds the expenses under check and the land stock stays quick moving. As the business sector is end client driven the rental expenses are high which make the land optional business sector in Bengaluru extremely alluring.
    Affordable Apartments in bengaluru

  3. Nihat Yayla June 27, 2016 at 3:50 pm #

    Geleceğin yatırımı olarak değerlendirilen konut ve arsalar emlakdream güvencesi ile istediğiniz tarzda ve istediğiniz konumda sizlerle .

    Kartal Konut Projeleri ile İstanbul Anadolu yakasının en çok değer kazanabilecek konutlarını sizlere sunmaktayız. Sizler için yapılan araştırmalar neticesinde web sitemizde yer alan iskanlı konutları incelemenizi tavsiye ediyoruz. Bilgilendirmeler için 7 gün 24 saat çağrı merkezimizde sizlerin çağrılarını beklemekteyiz.

    Sevdikleriniz için yaşamın önemini iyi biliyoruz bu yüzden en kaliteli mekanlarda en gözde mekanları sizler için derledik.

    http://www.emlakdream.com/etiket/kartal-konut-projeleri/

  4. Emlak Dream September 1, 2016 at 1:08 pm #

    Planlı gelişimi ve dev projelerle yatırımcıların yeni gözdesi Esenyurt’un en prestijli noktasında yükselen Terrace Mix’te; 1+0 daireler 163 bin lira ile 279 bin lira, 1+1 daireler 241 bin lira ile 385 bin lira, 2+1 daireler 395 bin lira ile 535 bin lira, 3+1 daireler ise 505 bin lira ile 580 bin lira arasında değişen fiyatlarla alıcıların karşısına çıkıyor. Esenyurt Konut Projeleri ile çok uygun fiyatlara konut sahibi olabilirsiniz.
    Merkezi konumu ile ideal bir mekanda konut sahibi olabileceksiniz. Sevdikleriniz ile merkezi konumdan her yere ulaşabilirsiniz.

    http://www.emlakdream.com/haber/Esenyurt-konut-projeleri/72561

  5. Emlak Dream October 5, 2016 at 1:36 pm #

    Tarihi Adaya komşu Zeytinburnu kazlıçeşme de bulunan 111 Dönümlük arsa üzerine Emlak Konut Ayrıcalıkları ile iyi bir yatırıma ayak basmanızın tam zamanı. Büyükyalı istanbul projesi Konumu itibari ile en çok tercih edilen yapıtlar arasında bulunan bu proje metro, metrobüs, otobüs , tranvay gibi ulaşımın olmazsa olmazlarına çok yakın mesafede aynı zamanda birçok kurum ve kuruluşa dakikalar içerisinde ulaşabileceğiniz bir yerde olması dolayısıyla zamanınızı trafiğe değil kendinize ayırabileceksiniz.

    Fırsat ve ayrıcalıklardan yararlanmak istiyorsanız sayfamızı takipte kalın en doğru ve güvenilir haberleri bizzat proje sahipleri ile olan röportajlar ile sizlere bilgi vermekteyiz.

    http://www.emlakdream.com/proje/buyukyali-istanbul-projesi/36

  6. Ebenezer November 27, 2016 at 5:26 pm #

    is there any way we can create a bot user using apex and not node? slash commands can be created using apex rest, but not sure if we can have web sockets open in Salesforce and hear the requests from bot user.

  7. CIIT Noida April 19, 2018 at 3:40 am #

    Thanks for sharing this amazing post with us.

    Hadoop Training Institute in Noida

    Best Hadoop Training in Noida

  8. eyal kama May 2, 2018 at 5:03 am #

    Do I need to build a custom integration in order to post to a specific record in Salesforce? I use the Slack for Salesforce from the Salesforce store and it it giving an option to slack to all records only. (/chatter [message])

    also if I want to auto link a record in sf base on a field value is that also something I need to create a custom integration or there is an of the shelf package who does this?

  9. Oladejo Joshua Jay December 3, 2018 at 12:04 pm #

    How can i link a record to it ??

    Hot Entertainment and News Here

    Latest Music Download Here

    Latest Video Download Here

  10. Oladejo Joshua Jay December 3, 2018 at 12:04 pm #

    Great tips

    Hot Entertainment and News Here

    Latest Music Download Here

    Latest Video Download Here

  11. Serpil ÇİÇEKÇİ June 20, 2019 at 8:49 am #

    Marmaris tekne turu olarak hizmetlerimiz, marmaris efes turu | Dalyan tekne turu | kleopatra plajı | marmaris pamukkale turu gibi ve daha fazla avantajlardan yararlanmak için bizimle iletişime geçmeniz yeterli olacaktır.

  12. gtacheat code April 1, 2020 at 8:50 am #

    Howdy, it was a genuinely awesome adventure when I eventually checked out your web blog. I just wished to commend you on the level of quality of the work you have done here and to send you the best of luck as you advance into the future. It was enjoyable to surf your web site and I will obviously be calling back once more to find out exactly how you’re doing. Visit my website – sims 4 vampire cheats and gta 5 cheats ps4 .

  13. samaira May 18, 2020 at 10:58 am #

    Awesome content you shared about the Spotify Premium Mod Apk where you explain the very major factor of every slack term. Thanks

  14. yuvaraj singh September 7, 2020 at 7:08 am #

    Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
    Advanced Scrum Master Training

  15. Invincible01 October 13, 2020 at 7:46 am #

    Amazing Article, Really useful information to all So, I hope you will share more information to be check and share here.

    landing page
    landing page design
    landing page tutorial
    react landing page tutorial
    bootstrap landing page tutorial
    mailchimp landing page tutorial
    wix landing page tutorial
    wordpress landing page tutorial
    create landing page tutorial
    django landing page tutorial

  16. yuvaraj singh November 23, 2020 at 4:35 am #

    Scrum master interview Questions
    To crack scrum master interview : Scrum Master Interview Questions

  17. yuvaraj singh December 22, 2020 at 3:01 am #

    Informative content , Great work
    Leanpitch provides online training in Enterprise agile coaching during this lockdown period everyone can use it wisely.
    Enterprise Agile Coaching

  18. Vishu January 4, 2021 at 2:52 am #

    Pradhan Mantri Vaya Vandana Yojana | PMVVY

  19. mira khan January 18, 2021 at 1:29 pm #

    this is what it is
    bittorrent pro crack
    avast pro antivirus crack
    avg pc tuneup crack

  20. skull mp3 February 19, 2021 at 7:29 am #

    I always appreciated your work, your creation is definitely unique. Great job

    rasmussen student portal

Trackbacks/Pingbacks

  1. Slack and Salesforce Integration: Authentication | Christophe Coenraets - June 17, 2016

    […] part 2 (Slash Commands) and part 3 (Bots), I used an “integration user” to access Salesforce on behalf of Slack users. In […]

  2. A Bot Toolkit for Creating and Deploying Bots Inside Salesforce - Developer Relations - March 13, 2017

    […] Writing a Salesforce Bot for Slack […]

css.php