Using Facebook with Dotdigital Omnichannel API

This tutorial will show you how to acquire opt-ins to Facebook and Facebook Messenger and send messages using Dotdigital's Omnichannel API and Python. Click the "Start" button below to start the tutorial.

Start Tutorial

Using Facebook with Dotdigital Omnichannel using Python

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.

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.

Setup and configure the example web site

In the tutorials Website folder you will find a simple web site created using Flask 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 Dotdigital's Omnichannel's Omnichannel API.

To setup this project do the following:

  • Clone the Github repo for the tutorial
  • Open a console / shell and change to the Github clone folder
  • Download the required dependencies from by running:
    pip install Flask

Next we will configure the web site for your Facebook page and Dotdigital Omnichannel API Space

Setup your Facebook channel in Dotdigital

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.

Finding your Facebook Page Id

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

Including Send to Messenger in your page

In the .\templates\index.html 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:

  • messenger_app_id - This must always equal 336037380081042
  • page_id - The numeric Facebook page id for the page you want to message the customers
  • data-ref - The secure metadata generated by Dotdigital in order to ensure the opt in is associated with the correct Omnichannel profile

The data-ref meta-data

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.

Retrieving the secure 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.

Configure your Dotdigital Omnichannel details

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:

Creating an API User

To create an API User please see this tutorial

Running the website

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

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 send to

Set the profileId to the user you opted in on the website in the example it was; 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.

Set the message to be sent

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.

Run the code

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.

Sending rich content

Facebook messenger can support various types of rich content including:

  • Quick reply buttons
  • Attachments
  • Templates
  • Etc...

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.

All done

Thanks for taking the time to look through the tutorial, to find out more visit our full documentation.