Facebook generic templates allows your business to send structured messages with a title, subtitle, image and up to three buttons.
In order to send a generic template, you can use any of the endpoints for sending messages in Conversations API with the message type facebookGenericTemplate. The example below illustrates how to send it using the endpoint POST /v1/send.
curl -X "POST" "https://conversations.messagebird.com/v1/send" \-H 'Authorization: AccessKey {your-access-key}' \-H 'Content-Type: application/json; charset=utf-8' \-d $'{"content": {"facebookGenericTemplate": {"attachment": {"type": "template","payload": {"image_aspect_ratio": "horizontal","template_type": "generic","elements": [{"title": "Welcome to MessageBird!","subtitle": "Would you like to know more about Plans & Pricing?","image_url": "https://www.messagebird.com/img/og/messagebird.gif","buttons": [{"type": "postback","payload": "custom-value","title": "Talk to us"},{"type": "web_url","url": "https://www.messagebird.com","title": "Visit website"},{"type": "phone_number","title": "Call us","payload": "+31000000000"}]}]}}}},"to": "4190117307726093","type": "facebookGenericTemplate","from": "99d0e570cd834661a1771ab08044be8b"}'
The message sent by the previous code snippet looks like the following.
The message looks like the following in Conversations API.
{"id": "b8bb66a07fcc46c2b56f8422587200b5","conversationId": "6279c2012f7d4df393cfa1f872518518","platform": "facebook","to": "4190117307726093","from": "101397625015964","channelId": "99d0e570cd834661a1771ab08044be8b","type": "facebookGenericTemplate","content": {"facebookGenericTemplate": {"attachment": {"type": "template","payload": {"template_type": "generic","elements": [{"buttons": [{"type": "postback","title": "Talk to us","payload": "custom-value"},{"type": "web_url","url": "https://www.messagebird.com","title": "Visit website"},{"type": "phone_number","title": "Call us","payload": "+31000000000"}],"title": "Welcome to MessageBird!","subtitle": "Would you like to know more about Plans \u0026 Pricing?","image_url": "https://www.messagebird.com/img/og/messagebird.gif"}]}}}},"direction": "sent","status": "sent","createdDatetime": "2022-03-21T15:46:39.403957027Z","updatedDatetime": "2022-03-21T15:46:39.403957027Z"}
When sending a generic template message with a postback button, like in the code snippet illustrated previously, you must set a custom payload in the payload field of the button. The value set in this field is sent through an inbound message with type FacebookPostBack.
The value in the payload field is limited to 1000 characters.
Assuming a postback button with a payload field set to custom-value like the following:
{"type": "postback","title": "Talk to us","payload": "custom-value"}
When the user clicks on the button, then the following inbound message is created in the conversation.
{"id": "3b9d18e21fd3468fa35004f7733e33bc","conversationId": "6279c2012f7d4df393cfa1f872518518","platform": "facebook","to": "101397625015964","from": "4190117307726093","channelId": "99d0e570cd834661a1771ab08044be8b","type": "facebookPostback","content": {"facebookPostback": {"title": "Talk to us","payload": "custom-value"}},"direction": "received","status": "received","createdDatetime": "2022-03-21T16:05:46.432Z","updatedDatetime": "2022-03-21T16:05:46.432Z"}
Generic templates also support the ability of triggering a default action when the template (not a button) is clicked. In order to enable this feature, you must set the field default_action in the FacebookElement object. The example below illustrates how to send a message with a default action.
curl -X "POST" "https://conversations.messagebird.com/v1/send" \-H 'Authorization: AccessKey {your-access-key}' \-H 'Content-Type: application/json; charset=utf-8' \-d $'{"content": {"facebookGenericTemplate": {"attachment": {"type": "template","payload": {"image_aspect_ratio": "horizontal","template_type": "generic","elements": [{"default_action": {"type": "web_url","url": "https://www.messagebird.com"},"title": "Welcome to MessageBird!","subtitle": "Would you like to know more about Plans & Pricing?","image_url": "https://www.messagebird.com/img/og/messagebird.gif","buttons": [{"type": "postback","payload": "custom-value","title": "Talk to us"},{"type": "web_url","url": "https://www.messagebird.com","title": "Visit website"},{"type": "phone_number","title": "Call us","payload": "+31000000000"}]}]}}}},"to": "4190117307726093","type": "facebookGenericTemplate","from": "99d0e570cd834661a1771ab08044be8b"}'
The only difference between this code snippet and the previous one is that, in this one, the default_action is defined with the type set to web_url and the url field set to https://www.messagebird.com. Therefore, when the user clicks on the template, they will be redirected to the MessageBird website. In the Facebook Messenger app, the message looks like exactly the same as previously demonstrated.
Conversations API also supports sending a carousel of generic templates. In order to do that, you must set multiple FacebookElement in the field elements. You can use any of the endpoints for sending messages in Conversations API with the message type facebookGenericTemplate. The example below illustrates how to send it using the endpoint POST /v1/send.
curl -X "POST" "https://conversations.messagebird.com/v1/send" \-H 'Authorization: AccessKey {your-access-key}' \-H 'Content-Type: application/json; charset=utf-8' \-d $'{"content": {"facebookGenericTemplate": {"attachment": {"type": "template","payload": {"image_aspect_ratio": "horizontal","template_type": "generic","elements": [{"buttons": [{"type": "web_url","url": "https://messagebird.com/en/pricing","title": "See pricing"}],"title": "Solutions Plans","image_url": "https://www.messagebird.com/img/og/solutions.png"},{"buttons": [{"type": "web_url","url": "https://messagebird.com/en/pricing/api","title": "See API pricing"}],"title": "API Pricing","image_url": "https://www.messagebird.com/img/og/api.jpg"},{"buttons": [{"type": "web_url","url": "https://messagebird.com/en/pricing/support","title": "See Support pricing"}],"title": "Support Plans","image_url": "https://www.messagebird.com/img/og/support.png"}]}}}},"to": "4190117307726093","type": "facebookGenericTemplate","from": "99d0e570cd834661a1771ab08044be8b"}'
As you can see, the elements field contains three FacebookElement objects, and every one of those elements is displayed at once in the carousel. The following images illustrate how the message sent in the previous code snippet is displayed in the Facebook Messenger app.