balsimpson
10/7/2017 - 5:26 PM

Message formats to send to FB Messenger

message formats for fb messenger

context.global.makeMsg = {
 
textMsg : function (fbId, text) {
    var data = {
        recipient : {
        id: fbId
    },
    message : {
        text : text
        }
    };
    return data;
},
 
cardMsg : function (fbId, elements) {
    var data = {
        recipient : {
        id: fbId
        },
        message : {
        attachment:{
            type:"template",
            payload:{
            template_type:"generic",
            elements: elements
            }
        }
    }
 };
    return data;
},


anyMsg : function function_name(fbId, dataObject) {
    
},

 
videoMsg : function (fbId, media_url) {
        var data = {
            recipient: {
            id: fbId
        },
        message: {
            attachment: {
            type: "video",
            payload: {
        	url: media_url
            }
        }
    }
 };
 return data;
},
 
 imageMsg : function (fbId, media_url) {
 var data = {
 recipient: {
 id: fbId
 },
 message: {
 attachment: {
 type: "image",
 payload: {
 	 url: media_url
 }
 }
 }
 };
 return data;
 },
 
 mediaMsg : function (fbId, media_url) {
 
    if (media_url) {
        if (media_url.substr(media_url.length-3,3) == 'mp4') {
 
            data = {
                recipient: {
                id: fbId
            },
            message: {
                attachment: {
                type: "video",
                payload: {
 	                url: media_url
                    }
                }
            }
        };
 } else {
            data = {
                recipient: {
                id: fbId
            },
            message: {
                attachment: {
                type: "image",
                payload: {
            	    url: media_url
                    }
                }
            }
        };
    }
 }
 
 return data;
 },
 
 quick_replies : function (textArr, locArr) {
 
 var quick_reply = [];
 
 if (locArr) {
 var loc = {
 content_type:"location"
 };
 
 quick_reply.push(loc);
 }
 
 if (textArr) {
 for (var i in textArr) {
 
 var title = textArr[i].title;
 var postback = textArr[i].postback;
 
 var data = {
 content_type:"text",
 title: " " + title + " " ,
 payload: postback
 };
 
 quick_reply.push(data);
 }
 }
 return quick_reply; 
 },
 
 button: function (type, title, url, postback) {
 //type - postback, web_url, account_link
 
 var post = postback;
 var ratio = "tall";
 
 var button = {};
 
 if (type == "web_url") {
 
 button = {
 type: "web_url",
 url: url,
 title: title,
 };
 }
 
 
 if (type == "postback") {
 button = {
 type: "postback",
 title: title,
 payload: postback
 };
 }
 
 if (type == "account") {
 button = {
 type: "account_link",
 url: url
 };
 }
 
 return button; 
 },
 
 element : function (message) {
 var title = message.title;
 var subtitle = message.subtitle;
 var image_url = message.image_url;
 var url = message.url || message.image_url;
 var ratio = message.ratio || "compact";
 
 var element = {
 title: title,
 image_url: image_url,
 subtitle: subtitle,
 default_action: {
 type: "web_url",
 url: image_url,
 // webview_height_ratio: ratio,
 // fallback_url : image_url
 }
 };
 
 return element;
 }
};

msg.payload = context.global.makeMsg;