This tutorial will show you how to acquire opt-ins to Facebook and Facebook Messenger and send messages using Dotdigital's Omnichannel API and Ruby. Click the "Start" button below to start the tutorial.
Start TutorialThis short tutorial will show you how to obtain a Facebook Messenger Id for a user using the Send to Messenger web plugin from Facebook, and then how to send the user a message using the Dotdigital Omnichannel API.
To begin with you will need the following prerequisites installed:
In the tutorials folder you will find a simple web site created using Ruby on Rails that renders a single page which contains the Send to Messenger web plugin populated with the correct secure metadata to allow Dotdigital Omnichannel to capture and associate the Facebook Messenger Id with a Dotdigital Omnichannel profile. Once the Send to Messenger has been pressed some test buttons will be shown, these will demonstrate sending simple text and rich Facebook messages via Comapi's Omnichannel API.
To setup this project do the following:
gem install rails
Once installed you can check all is working by requesting the version of Ruby on Rails by running:
rails --version
Next we will configure the web site for your Facebook page and Dotdigital Omnichannel API Space
The Facebook Send to Messenger control will only render if the the Facebook Messenger channel has been setup with your Facebook page in Dotdigital. Follow the these instructions to setup your Facebook channel in Dotdigital if you haven't already done so.
You need to know you Facebook page numeric id for the Send to Messenger web plugin. If you don't know this, then the simplest way of finding it is via findmyfbid.com, simply open this site and paste in your Facebook pages URL to obtain the Facebook page id.
Once you have your Facebook Page Id simply enter it into the source file as indicated replaced the YOUR FACEBOOK PAGE ID place holder.
In the .\templates\index.html.erb file (highlighted) the end to Messenger control is embedded by importing the Javascript script from Facebook and then creating a DIV element with the class fb-send-to-messenger which the script will render the Send to Messenger control into. In addition to the class the DIV element needs some custom attributes in order to render:
The code merges some meta-data into the data-ref property of the Send to Messenger div. This meta-data is used to associate the Facebook Messenger Id with a Dotdigital Omnichannel profile and is essential. The next steps will explain how to obtain this meta-data.
Dotdigital Omnichannel provides a web service to create secure encrypted meta-data to be passed to the Facebook Send to Messenger web plugin in the data-ref property.
This web service is called each time the web page is rendered and should be passed the profileId you want to associate the Facebook Messenger Id to as a minimum, but you can pass any other data you want adding to the profile.
In this case we are passing in the logged in users user id.
In order for the Facebook meta-data web service call to work your must configure your Dotdigital Omnichannel API User credentials. Please enter your API user credentials in the source file as indicated to the right, or create an API user using the instructions below:
To create an API User please see this tutorial
To run the website now it's configured simply click the debug button in your IDE.
If all has worked you can now browse the website at http://localhost:3000/
Note: The port number may be different, check your IDE's output to see what port Symfony is listening on
Click on the Send to Messenger button and if required login to Facebook. In the background Facebook will send the opt-in information to Dotdigital, where we will decrypt and validate the metadata. If the metadata is valid Dotdigital Omnichannel will either append the fbMessengerId to an existing profile or create a profile using the details provided.
Sending messages to Facebook is very simple now that you have an opted in user with their Facebook Id (fbMessngerId) stored against their Dotdigital Omnichannel profile. To send a simple text based message you simply make a REST call to the Omnichannel API referencing the recipients profileId in the to section, set a body message and target the fbMessenger channel. Some basic code to do this is highlighted.
Set the profileId to the user you opted in on the website in the example it was test@acme.com; Dotdigital Omnichannel will then automatically access the fbMessengerId field from the profile to send the message via Facebook. You can target a user on Facebook explicitly if you have their Facebook Messenger Id already by adding it the the to object in the request as fbMessengerId.
Dotdigital Omnichannel will automatically create a Facebook Messenger message from your body text, making text based messages very easy indeed, set your message text now.
To run the website, and ensure you have opted in by clicking the Send to Messenger button.
Click on the Send Simple Test Message button, you should receive your Facebook message very shortly, check your Facebook account online or via Facebook Messenger.
Facebook messenger can support various types of rich content including:
You can send any of the Facebook message body types if you use the customBody property and define a fbMessenger object (highlighted) within it that complies with the Facebook Graph API's message object as defined in the Facebook docs.
Find out more about sending custom bodies with the Facebook channel here.
Click on the Send Rich Test Message button, you should receive your rich Facebook message very shortly, check your Facebook account online or via Facebook Messenger.
Thanks for taking the time to look through the tutorial, to find out more visit our full documentation.