(code snippets included)
In this article, I will give you a complete tutorial of how to return multiple elements of Facebook messenger dynamically through fulfillment and webhooks.
Facebook messenger essentially supports 5 different elements.
Multiple Card elements
It’s a very easy job while using Dialogflow UI to integrate these items. But when it comes to returning it through custom webhooks for dynamic content from your web server, it can be very painful for every chatbot developer.
Follow the steps below.
Step 1: Connect your intent to fulfillment.
Dialogflow supports inline editor which is powered by google cloud functions and Webhooks. You can enable anyone of those.
In this tutorial, I will go through only webhooks. Go ahead and enable it quickly.
Step 2: Enter the credentials here along with the URL of the web service.
I am using a python flask web app, to connect my chatbot with Dialogflow and hosted in Heroku web server, so the URL here is: htttps://heroku.com/
Now the webhook is enabled for particular intent it displays what returns from the URL. We will return the required response from our program in this part.
2.1 GENERIC CARD LAYOUT.
This piece of code returns cards. We can also return multiple cards which are displayed as a card carousel in messenger.
If you want multiple card elements displayed as a carousel then separate another card element with a comma and return as a fulfillment message from the callback URL. Here is the code snippet.
2.2 Quick Replies
These can be very handy when you want the user to enter the desired options to explore. The quick reply comes with a title which is a string and multiple quick replies options. Here is the code snippet.
2.3 Dynamic Text
You can just return plain text through your code, into messenger chatbot. Here we return a response as fulfillmentText and not fulfillment message. This is important otherwise you may encounter error not syntax but logical error and hard to find.
Facebook messenger also has a video card element that only supports video from Facebook and not other video hosting sites like youtube, Vimeo, and other websites.
Dialogflow fulfillment inbuild code editor has support for only node js, and the choice of web server should be google firebase. If you are uncomfortable with this you can always choose webhooks to be enabled and connect to your web services.
Thank you for coming down to the end of this article. I hope you find this article useful in building a successful chatbot. If you have any question regarding this tutorial you can always write to me @ firstname.lastname@example.org