This tutorial will show you how to acquire opt-ins for Facebook Messenger and send messages using Dotdigital's Omnichannel API and PHP. Click the "Start" button below to start the tutorial.Start Tutorial
This 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.
The tutorial's navigation controls are located above this text. Use to move to the next step of the tutorial. Use to move to the previous step of the tutorial (if there is one). Use to see an overview of all the steps in the tutorial.
To begin with you will need the following prerequisites installed:
In the tutorials Website folder you will find a simple web site created using Symfony (PHP web framework) that renders a single page which
In the tutorials folder you will find a simple web site created using Symfony that will receive POSTed data over SSL, and process it to ensure it was from Dotdigital Omnichannel and not tampered with.
OneAPI\PHP\Facebookdirectory in the repo in your IDE as a project
Next we will configure the web site for your Facebook page and Dotdigital Omnichannel API credentials
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.
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 firstname.lastname@example.org; 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.