Writing a Salesforce Bot for Facebook Messenger

Last week Facebook launched its bot-enabled Messenger Platform. I decided to check it out and build a simple Salesforce bot for Messenger. This is not an official project, just a quick experiment to explore the possibilities.

Watch the video to see the bot in action:

or using the phone version of Messenger:

Bots allow you to access information in an unstructured manner, using natural language, and from wherever you happen to be without having to switch context and load a dedicated app.

Part of exploring the new possibilities enabled by bots is going to be to identify the right use cases; what should be botified and what shouldn’t. This particular bot is a proof of concept that lets employees access their customer information and perform quick actions in a conversational manner. The Facebook Messenger platform is more focused on Customer Engagement use cases, but the approach used in this example to build a simple Salesforce-powered bot remains the same regardless of the specific use case.

Source Code

The source code for the Node.js application is available in this repository. The easiest way to deploy your instance of the Node.js App is to click Deploy to Heroku button below, and fill in the deployment wizard. You need to provide the access token of your Facebook Webhook, the id of your Salesforce connected app, the username and password of an integration user in Salesforce. If you are testing this in a Developer Edition, you can enter your own credentials.

  1. Make sure you are logged in to the Heroku Dashboard
  2. Click the Button below to deploy the application on Heroku.

    Deploy

Your own instance of the application is automatically deployed.

Check out the Messenger Platform documentation to create a Facebook-side of the application.

Authentication

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

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

43 Responses to Writing a Salesforce Bot for Facebook Messenger

  1. Arulnath May 10, 2016 at 5:30 am #

    post helps to know about fb messenger bot…

  2. dhiyar May 10, 2016 at 7:18 am #

    Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here.Well, got a good knowledge.

  3. Venkatraman Natarajan May 12, 2016 at 5:10 am #

    I believe the userid received from the webhook for a specific user is not the facebook Id, rather the page scoped id, which is different from the facebook id.

    Currently messenger user profile APIs does not provide option to retrieve the facebook id of the user.

    In this case, how does the user authenticate against the salesforce?

    I believe the users must be authenticated outside the messenger through a weburl from the messenger app, and map the outcome of the authentication to the subsequent transactions. Or do you have another way to achieve this?

  4. Jeeshi May 12, 2016 at 5:59 am #

    Thanks for the interesting articles and Business ethics for new information’s , and i like that kind of information .So the peoples like that post,because all of given information was very excellent.

  5. propchill May 17, 2016 at 7:41 am #

    Many how had put resources into Real estate in Gurgaon around 7 to 10 years back, turned moguls. However, the previous 2 or 3 years have seen a colossal downtrend which is prone to proceed for the coming one year. The business sector has seen a revision of more than 20% and with tremendous stock excesses, delay in activities the future stays hopeless. The principle purposes behind this more than typical downtrend has been –residential land market has been financial specialist driven as opposed to end client driven in Gurgaon; the sudden increment in area costs prompting higher per square feet rates; dispatch of various top of the line, extravagance and aspiring undertakings with excessive sticker prices; absence of framework improvement and backing from the organization. Be that as it may, there is a silver covering too, with the costs having dropped by more than 20% in the private land, the following 6 months is the correct time for land interests in Gurgaon.
    Affordable Apartments in Gurgaon

  6. dr driving game June 9, 2016 at 2:07 pm #

    One such development in the video gaming arena would be the current, 3D video game conversion innovation. The 3D video game conversion innovation, a brand name brand-new innovation and initially of its kind has actually been released by LG Electronic devices for their Mobile phone series – LG Optimus 3D and LG Excitement 4G.

  7. bus simulator June 9, 2016 at 2:09 pm #

    The 3D material and 3D Online game Design has much potential opening with this most current development. The software application is anticipated to promote both video game designers along with Mobile phone users.

  8. jagan salesforce June 22, 2016 at 3:48 am #

    hey Christophe can you please help me out to solve this issue ? :)

    I have followed the steps which you have said.It is working fine for me.I want to add enhancements into it like Searching for Escalated Cases.I have added corresponding logic too.

    When i deploy the updated build in Heroku am getting the below error stating SyntaxError: Unexpected token case.

    Can you please help me out ? Please find below the log for your reference !!!!

    2016-06-22T07:39:54.315043+00:00 heroku[api]: Deploy 64f4816 by jagannathan.salesforce@gmail.com
    2016-06-22T07:39:54.315092+00:00 heroku[api]: Release v24 created by jagannathan.salesforce@gmail.com
    2016-06-22T07:39:54.491553+00:00 heroku[slug-compiler]: Slug compilation started
    2016-06-22T07:39:54.488061+00:00 heroku[web.1]: State changed from crashed to starting
    2016-06-22T07:39:55.785328+00:00 heroku[web.1]: Starting process with command `npm start`
    2016-06-22T07:39:58.289908+00:00 app[web.1]:
    2016-06-22T07:39:58.289927+00:00 app[web.1]: > messengerforce@1.0.0 start /app
    2016-06-22T07:39:58.289932+00:00 app[web.1]: > node server.js
    2016-06-22T07:39:58.289933+00:00 app[web.1]:
    2016-06-22T07:39:58.811083+00:00 app[web.1]: /app/modules/formatter-messenger.js:102
    2016-06-22T07:39:58.811103+00:00 app[web.1]: Cases.forEach(case =>
    2016-06-22T07:39:58.811105+00:00 app[web.1]: ^^^^
    2016-06-22T07:39:58.811105+00:00 app[web.1]:
    2016-06-22T07:39:58.811106+00:00 app[web.1]: SyntaxError: Unexpected token case
    2016-06-22T07:39:58.811107+00:00 app[web.1]: at exports.runInThisContext (vm.js:53:16)
    2016-06-22T07:39:58.811108+00:00 app[web.1]: at Module._compile (module.js:387:25)
    2016-06-22T07:39:58.811112+00:00 app[web.1]: at Object.Module._extensions..js (module.js:422:10)
    2016-06-22T07:39:58.811113+00:00 app[web.1]: at Module.load (module.js:357:32)
    2016-06-22T07:39:58.811113+00:00 app[web.1]: at Function.Module._load (module.js:314:12)
    2016-06-22T07:39:58.811114+00:00 app[web.1]: at Module.require (module.js:367:17)
    2016-06-22T07:39:58.811115+00:00 app[web.1]: at Object. (/app/modules/webhook.js:5:17)
    2016-06-22T07:39:58.811116+00:00 app[web.1]: at Module._compile (module.js:413:34)
    2016-06-22T07:39:58.811117+00:00 app[web.1]: at Object.Module._extensions..js (module.js:422:10)
    2016-06-22T07:39:58.811117+00:00 app[web.1]: at Module.load (module.js:357:32)
    2016-06-22T07:39:58.811118+00:00 app[web.1]: at Function.Module._load (module.js:314:12)
    2016-06-22T07:39:58.829087+00:00 app[web.1]: npm ERR! Linux 3.13.0-85-generic
    2016-06-22T07:39:58.831049+00:00 app[web.1]: npm ERR!
    2016-06-22T07:39:58.831243+00:00 app[web.1]: npm ERR! Failed at the messengerforce@1.0.0 start script ‘node server.js’.
    2016-06-22T07:39:58.833015+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
    2016-06-22T07:39:58.838477+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
    2016-06-22T07:39:58.838682+00:00 app[web.1]: npm ERR! /app/npm-debug.log
    2016-06-22T07:39:59.638953+00:00 heroku[web.1]: Process exited with status 1
    2016-06-22T07:39:59.656110+00:00 heroku[web.1]: State changed from starting to crashed
    2016-06-22T07:39:59.657076+00:00 heroku[web.1]: State changed from crashed to starting
    2016-06-22T07:40:00.938560+00:00 heroku[web.1]: Starting process with command `npm start`
    2016-06-22T07:40:03.311351+00:00 app[web.1]:
    2016-06-22T07:40:03.311373+00:00 app[web.1]: > messengerforce@1.0.0 start /app
    2016-06-22T07:40:03.816295+00:00 app[web.1]: /app/modules/formatter-messenger.js:102
    2016-06-22T07:40:03.816318+00:00 app[web.1]: Cases.forEach(case =>
    2016-06-22T07:40:03.816320+00:00 app[web.1]: ^^^^
    2016-06-22T07:40:03.816328+00:00 app[web.1]: SyntaxError: Unexpected token case
    2016-06-22T07:40:03.816328+00:00 app[web.1]: at exports.runInThisContext (vm.js:53:16)
    2016-06-22T07:40:03.816329+00:00 app[web.1]: at Module._compile (module.js:387:25)
    2016-06-22T07:40:03.816333+00:00 app[web.1]: at Module.load (module.js:357:32)
    2016-06-22T07:40:03.816334+00:00 app[web.1]: at Function.Module._load (module.js:314:12)
    2016-06-22T07:40:03.816339+00:00 app[web.1]: at require (internal/module.js:20:19)
    2016-06-22T07:40:03.834038+00:00 app[web.1]: npm ERR! Failed at the messengerforce@1.0.0 start script ‘node server.js’.
    2016-06-22T07:40:03.834206+00:00 app[web.1]: npm ERR! Make sure you have the latest version of node.js and npm installed.
    2016-06-22T07:40:03.835267+00:00 app[web.1]: npm ERR! npm bugs messengerforce
    2016-06-22T07:40:03.835426+00:00 app[web.1]: npm ERR! Or if that isn’t available, you can get their info via:
    2016-06-22T07:40:03.835771+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
    2016-06-22T07:40:03.840989+00:00 app[web.1]:
    2016-06-22T07:40:03.841336+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
    2016-06-22T07:40:03.841485+00:00 app[web.1]: npm ERR! /app/npm-debug.log
    2016-06-22T07:40:04.754095+00:00 heroku[web.1]: Process exited with status 1
    2016-06-22T07:40:04.766410+00:00 heroku[web.1]: State changed from starting to crashed

  9. Eduardo Asenjo Rodriguez June 27, 2016 at 6:39 am #

    Hi! Great job!. But I have an authentication problem from heroku to salesforce:

    Authentication error
    { [Error: invalid_grant – authentication failure] statusCode: 400 }

    Any suggestion?

    • Vijaya murugan August 31, 2016 at 12:18 pm #

      Did you solve this issue? if you have resolved, Could you please post the solution. I am facing the same issue

    • Jeetesh December 29, 2017 at 12:36 pm #

      Any clue, I am also getting the same error.

  10. Nihat Yayla July 12, 2016 at 2:50 pm #

    Türkiyenin en kaliteli güzel sanatlara hazırlık kurumunda eğitim almaya ne dersiniz ?
    %60 ‘a varan indirimler ile uygun fiyatlara kişisel gelişiminizi tamamlayabilir ve hayal ettiğiniz fakültelere ruyaavcısı ayrıcalıkları ile ulaşabilirsiniz.
    Tarafımızca eğitim almış sektörün önde gelen ressam, öğretim görevlisi, sanatkarlardan biri neden siz olmayasınız ki …
    2 Şubemiz ile haftanın yedi günü hizmetinizdeyiz. Çalışma ortamını ve atölyelerimizi incelemek için sizleri kurumumuza davet ediyoruz sizde kaliteli eğitim hak ediyorsunuz bu yüzden durmayın….
    Bakırköy Resim Kursu ile el becerinize tecrübe ve ince dokunuşlar katarak eşsiz tatlar ortaya çıkaracağımıza eminiz…
    bakırköy resim kursu, bakirkoy resim kursu, resim kursu bakirkoy, resim kursu bakırköy, resim kursu, güzel sanatlara hazırlık, bakırköy güzel sanatlara hazırlık,
    http://ruyaavcisi.com/

  11. Vincent Li September 5, 2016 at 7:29 am #

    Hi,
    Thanks for this quite nice explanation !
    I installed everything as explained, but I am stucked at the first level of interaction : I type ‘help’ , then the bot replys by explaining me what i can ask. Then i ask something but i never get an answer.
    Has anyone faced this problem ?
    Thanks

    • vaibhav kaurwar January 14, 2017 at 6:40 am #

      Instead of integration user you TRY System Administrator’s credentials and check again…hope this helps

      • Bijay S January 18, 2018 at 6:40 am #

        Hi Vaibhav,

        I am using System admin cred but still its not happening , only help key work works.

        Could you please help me how you resolved the issue.

        Thanks

  12. user eonlinetraining March 15, 2017 at 8:51 am #

    Good website! I really love how it is simple on my eyes and the data are well written

    http://eonlinetraining.co/course/data-science-online-training/

  13. Suraj Gupta April 9, 2017 at 2:16 am #

    Hello Sir

    I have created app, It works fine for me when I type help in messanger. But it is not giving me any response when I ask somthing like search account acme.

    Can you help me with the same

    • Bijay S January 18, 2018 at 6:39 am #

      Hi Suraj ,

      Were you able to resolve the issue ?

      Thanks

    • Jeneesha Kasundi February 15, 2018 at 5:23 am #

      I have solved the issue. You can find the code in the below link.

      https://github.com/jenyks/messengerbot

  14. Prop Yantra May 11, 2017 at 5:29 am #

    this post is very informative on FB msg bots, can we do something similar for MS dynamics

    bhanu prathap
    http://www.propyantra.com

    • Prop Yantra October 10, 2018 at 11:29 am #

      indeed a great post!

      Guntur Property

  15. McGutscheine August 6, 2017 at 4:21 am #

    Thank you for sharing this. Learnt a lot as it was highly informative.

  16. ZAF SEO August 15, 2017 at 8:15 am #

    These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post, writing is simply great, thank you for the post. Alexandria car service

  17. usps tracking October 27, 2017 at 12:48 pm #

    Your article is detailed, thanks to it I solved the problem I am entangled. I will regularly follow your writers and visit this site daily.
    abcya

  18. Rahul December 9, 2017 at 4:58 am #

    Nice… you are doing great..thnx for sharing arcticle & videos..keep up the good work…
    http://indiakaaoffer.com/

  19. Nicole John January 15, 2018 at 12:21 pm #

    These are genuinely enormous ideas in concerning blogging.
    You have touched some pleasant things here. Any way keep up writing.

  20. Jeneesha Kasundi February 15, 2018 at 5:20 am #

    Hello sir,
    Thank you for your great explanation. I am new to salesforce and I have tried this example. But the code worked only for “help” message. So i solved the issue with that code and now it is working.

    Those who have the same issue can find the code in the below link.

    https://github.com/jenyks/messengerbot

    • Panda February 21, 2018 at 8:02 am #

      Hi,

      I have still some issues even when I change the initial code with your version.

      Some ideas what the issue can be? I receive the following error when I go to : myapphere.herokuapp.com/webhook => Error, wrong validation token

      Thanks in advance!

  21. tiara tech June 11, 2018 at 6:59 am #

    Thank you sharing valuable information

    visitDigital-Marketing for more new ideas, we are thankful for this article.

  22. Dinesh Kumar June 11, 2018 at 8:28 am #

    This amazing information for Salesforce Bot for Facebook Messenger.visit,Best software company for more excellent ideas. We really thankful this article.

  23. Web Development Company September 15, 2018 at 3:58 am #

    Thanks for sharing this information –

    Thanks on behalf of Team TechnoBrains (www.technobrains.net)

  24. Sezen Karaca September 17, 2019 at 7:48 am #

    Paykwik kart sayesinde web sitesi üzerinden yapacağınız alışverişlerinizde şahsi bilgilerinizi vermeye gerek kalmayacaktır. Paykwik hakkında daha fazla bilgi ve detay için http://www.paykwikalim.com adresine tıklayın.

  25. gtacheat code April 4, 2020 at 1:56 pm #

    Nice blog and absolutely outstanding. You can do something much better but I still say this perfect. Keep trying for the best. Today I find another website that includes game updated Cheat Code. And if you want Latest eu4 console commands 2020 click on the given link.

  26. yuvaraj singh July 29, 2020 at 3:58 am #

    Leanpitch provides online training in DevOps during this lockdown period everyone can use it wisely.
    DevOps Online Training

  27. yuvaraj singh September 7, 2020 at 7:06 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.
    CSM online

  28. yuvaraj singh September 7, 2020 at 7:07 am #

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

  29. yuvaraj singh September 9, 2020 at 3:22 am #

    Informative article , Great work
    Leanpitch provides online training in Advanced Scrum Master during this lockdown period everyone can use it wisely.
    Advanced Scrum Master Training Online

  30. Invincible01 October 22, 2020 at 10:06 am #

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

    What is Fuzzy Logic
    fuzzy logic tutorial
    fuzzy logic is
    what is the fuzzy logic
    “fuzzy logic control”
    fuzzy logic controller
    “fuzzy logic application”
    “applications of fuzzy logic”
    “application of fuzzy logic”
    what fuzzy logic

  31. yuvaraj singh December 22, 2020 at 2:59 am #

    Thanks for sharing this informative content
    Leanpitch provides online training in Enterprise agile coaching during this lockdown period everyone can use it wisely.
    Enterprise Agile Coaching

  32. Aarush January 30, 2021 at 1:17 am #

    Informative article , Great work Thanks For Including all useful stuff
    Visit : https://www.buildmyrebusiness.com/

  33. Saket Kumar February 12, 2021 at 6:24 am #

    Informative post. Thanks for sharing this

Trackbacks/Pingbacks

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

    […] Writing a Salesforce Bot for Facebook Messenger […]

css.php