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
  • post helps to know about fb messenger bot…

  • 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.

  • Venkatraman Natarajan

    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?

  • 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.

  • 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

  • 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.

  • 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.

  • jagan salesforce

    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

  • Eduardo Asenjo Rodriguez

    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

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

  • Nihat Yayla

    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/

  • Vincent Li

    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

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

  • Pingback: A Bot Toolkit for Creating and Deploying Bots Inside Salesforce - Developer Relations()

  • user eonlinetraining

    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/

  • Suraj Gupta

    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

  • Prop Yantra

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

    bhanu prathap
    http://www.propyantra.com

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

  • ZAF SEO

    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

css.php