<commit_before>﻿const dashbot = require('dashbot')(process.env.DASHBOT_API_KEY).facebook;
const request = require('request');
const Q = require('q');

function processMessagesFromApiAi(messages, senderID) {
    for (let i = 0; i < messages.length; i++) {
        let message = messages[i];
        let replyMessage = null;

        switch (message.type) {
            case 0:
                replyMessage = textMessage(message.speech);
                break;
            case 1:
                replyMessage = structuredMessage(message);
                break;
            case 2:
                replyMessage = quickReply(message);
                break;
        }

        reply(replyMessage, senderID);
    }
}

function structuredMessage(message) {
    let buttons = [];
    for (let i = 0; i < message.buttons.length; i++) {
        buttons.push({
            "type": "postback",
            "title": message.buttons[i].text,
            "payload": message.buttons[i].postback
        });
    }


    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [
                    {
                        "title": message.title,
                        "subtitle": message.subtitle,
                        "image_url": message.imageUrl,
                        "buttons": buttons
                    }
                ]
            }
        }
    }
}

//--------------------------------------------------------------------------------
function textMessage(message) {
    return {
        "text": message
    }
}

//pass into this method the `messages` section of the api.ai response
//message: {
//  title: 'Yes',
//  replies" [
//   "No", "Maybe"
//  ],
//  type: 2
// }
function quickReply(apiai_qr) {
    let quick_replies = [];

    for (let i = 0; i < apiai_qr.replies.length; i++) {
        quick_replies.push({
            "content_type": "text",
            "title": apiai_qr.replies[i],
            "payload": apiai_qr.replies[i]
        });
    }

    return {
        "text": apiai_qr.title,
        "quick_replies": quick_replies
    };
}

function imageCard(thumbUrl, downloadUrl, instaUrl, authName) {
    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [
                    {
                        "title": authName + " on Instagram",
                        "item_url": instaUrl,
                        "image_url": thumbUrl,
                        "buttons": [
                            {
                                "type": "web_url",
                                "url": downloadUrl,
                                "title": "Download"
                            },
                            {
                                "type": "element_share"
                            }
                        ]
                    }]
            }
        }
    }
}

function youtubeVideoCard(thumbUrl, downloadUrl, originalUrl) {
    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [
                    {
                        "title": "Youtube Video",
                        "item_url": originalUrl,
                        "image_url": thumbUrl,
                        "buttons": [
                            {
                                "type": "web_url",
                                "url": downloadUrl,
                                "title": "Download"
                            },
                            {
                                "type": "element_share"
                            }
                        ]
                    }]
            }
        }
    }
}

function imageAttachment(thumbUrl) {
    return {
        "attachment": {
            "type": "image",
            "payload": {
                "url": thumbUrl
            }
        }
    }
}

function genericMessage() {
    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [{
                    "title": "First card",
                    "subtitle": "Element #1 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/rift.png",
                    "buttons": [{
                        "type": "web_url",
                        "url": "https://www.messenger.com",
                        "title": "web url"
                    }, {
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for first element in a generic bubble",
                    }],
                }, {
                    "title": "Second card",
                    "subtitle": "Element #2 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png",
                    "buttons": [{
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for second element in a generic bubble",
                    }],
                }]
            }
        }
    }
}
//--------------------------------------------------------------------------------
function reply(message, senderId) {
    let deferred = Q.defer();
    console.log("===sending message to: ", senderId);

    let reqData = {
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {
            access_token: process.env.FB_PAGE_ACCESS_TOKEN
        },
        method: 'POST',
        json: {
            recipient: {
                id: senderId
            },
            message: message
        }
    };

    request(reqData, (err, response, body) => {
        if (err) {
            console.log("===error while sending message to FB: ", err.message);
            deferred.reject(err);
        } else {
            dashbot.logOutgoing(reqData, response.body);

            if (response.statusCode == 200) {
                console.log("===sent message to FB");
                deferred.resolve(body);
            } else {
                console.log("===Error in FB response:", response);
                deferred.reject(body);
            }
        }
    });
    return deferred.promise;
}
//--------------------------------------------------------------------------------
exports.processMessagesFromApiAi = processMessagesFromApiAi;
exports.textMessage = textMessage;
exports.quickReply = quickReply;
exports.genericMessage = genericMessage;
exports.imageCard = imageCard;
exports.imageAttachment = imageAttachment;
exports.reply = reply;
exports.youtubeVideoCard = youtubeVideoCard;  
<commit_msg>added new functions greetingText getStartedButton persistentMenu<commit_after>﻿const dashbot = require('dashbot')(process.env.DASHBOT_API_KEY).facebook;
const request = require('request');
const Q = require('q');

function processMessagesFromApiAi(messages, senderID) {
    for (let i = 0; i < messages.length; i++) {
        let message = messages[i];
        let replyMessage = null;

        switch (message.type) {
            case 0:
                replyMessage = textMessage(message.speech);
                break;
            case 1:
                replyMessage = structuredMessage(message);
                break;
            case 2:
                replyMessage = quickReply(message);
                break;
        }

        reply(replyMessage, senderID);
    }
}

function structuredMessage(message) {
    let buttons = [];
    for (let i = 0; i < message.buttons.length; i++) {
        buttons.push({
            "type": "postback",
            "title": message.buttons[i].text,
            "payload": message.buttons[i].postback
        });
    }


    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [
                    {
                        "title": message.title,
                        "subtitle": message.subtitle,
                        "image_url": message.imageUrl,
                        "buttons": buttons
                    }
                ]
            }
        }
    }
}

//--------------------------------------------------------------------------------
function textMessage(message) {
    return {
        "text": message
    }
}

//pass into this method the `messages` section of the api.ai response
//message: {
//  title: 'Yes',
//  replies" [
//   "No", "Maybe"
//  ],
//  type: 2
// }
function quickReply(apiai_qr) {
    let quick_replies = [];

    for (let i = 0; i < apiai_qr.replies.length; i++) {
        quick_replies.push({
            "content_type": "text",
            "title": apiai_qr.replies[i],
            "payload": apiai_qr.replies[i]
        });
    }

    return {
        "text": apiai_qr.title,
        "quick_replies": quick_replies
    };
}

function setThreadSettings(data) {
    let deferred = Q.defer();
    let reqData = {
        url: "https://graph.facebook.com/v2.6/me/thread_settings",
        qs: {
            access_token: process.env.FB_PAGE_ACCESS_TOKEN
        },
        method: 'POST',
        json: data
    };
    request(reqData, (err, response, body) => {
        if (err) {
            console.log("===error while sending message to FB: ", err.message);
            deferred.reject(err);
        } else {
            if (response.statusCode == 200) {
                console.log("===sent settings to FB");
                deferred.resolve(body);
            } else {
                console.log("===Error in FB response:", response);
                deferred.reject(body);
            }
        }
    });
    return deferred.promise;
}

function greetingText(text) {
    console.log("===sending greeting text");
    let data = {
        setting_type: "greeting",
        greeting: {
            text: text
        }
    }
    return setThreadSettings(data);
}

function getStartedButton(payload) {
    console.log("===sending started button");
    let data = {
        setting_type: "call_to_actions",
        thread_state: "new_thread",
        call_to_actions: [
            { payload: payload }
        ]
    }
    return setThreadSettings(data);
}

/*
[{ type: "postback", title: "Yes", payload: "Yes" },
 { type: "postback", title: "Help", payload: "Help" }]
*/
function persistentMenu(call_to_actions) {
    console.log("===sending persistent menu settings");
    let data = {
        setting_type: "call_to_actions",
        thread_state: "existing_thread",
        call_to_actions: call_to_actions
    };
    return setThreadSettings(data);
}


function imageCard(thumbUrl, downloadUrl, instaUrl, authName) {
    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [
                    {
                        "title": authName + " on Instagram",
                        "item_url": instaUrl,
                        "image_url": thumbUrl,
                        "buttons": [
                            {
                                "type": "web_url",
                                "url": downloadUrl,
                                "title": "Download"
                            },
                            {
                                "type": "element_share"
                            }
                        ]
                    }]
            }
        }
    }
}

function youtubeVideoCard(thumbUrl, downloadUrl, originalUrl) {
    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [
                    {
                        "title": "Youtube Video",
                        "item_url": originalUrl,
                        "image_url": thumbUrl,
                        "buttons": [
                            {
                                "type": "web_url",
                                "url": downloadUrl,
                                "title": "Download"
                            },
                            {
                                "type": "element_share"
                            }
                        ]
                    }]
            }
        }
    }
}

function imageAttachment(thumbUrl) {
    return {
        "attachment": {
            "type": "image",
            "payload": {
                "url": thumbUrl
            }
        }
    }
}

function genericMessage() {
    return {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [{
                    "title": "First card",
                    "subtitle": "Element #1 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/rift.png",
                    "buttons": [{
                        "type": "web_url",
                        "url": "https://www.messenger.com",
                        "title": "web url"
                    }, {
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for first element in a generic bubble",
                    }],
                }, {
                    "title": "Second card",
                    "subtitle": "Element #2 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png",
                    "buttons": [{
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for second element in a generic bubble",
                    }],
                }]
            }
        }
    }
}
//--------------------------------------------------------------------------------
function reply(message, senderId) {
    let deferred = Q.defer();
    console.log("===sending message to: ", senderId);

    let reqData = {
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {
            access_token: process.env.FB_PAGE_ACCESS_TOKEN
        },
        method: 'POST',
        json: {
            recipient: {
                id: senderId
            },
            message: message
        }
    };

    request(reqData, (err, response, body) => {
        if (err) {
            console.log("===error while sending message to FB: ", err.message);
            deferred.reject(err);
        } else {
            dashbot.logOutgoing(reqData, response.body);

            if (response.statusCode == 200) {
                console.log("===sent message to FB");
                deferred.resolve(body);
            } else {
                console.log("===Error in FB response:", response);
                deferred.reject(body);
            }
        }
    });
    return deferred.promise;
}
//--------------------------------------------------------------------------------
exports.processMessagesFromApiAi = processMessagesFromApiAi;
exports.textMessage = textMessage;
exports.quickReply = quickReply;
exports.genericMessage = genericMessage;
exports.imageCard = imageCard;
exports.imageAttachment = imageAttachment;
exports.reply = reply;
exports.youtubeVideoCard = youtubeVideoCard;
exports.greetingText = greetingText;
exports.getStartedButton = getStartedButton;
exports.persistentMenu = persistentMenu;

{'fraction_non_alphanumeric': 0.14839452475374185, 'fraction_numerical': 0.002110784188307535, 'mean_word_length': 1.1403148528405203, 'pattern_counts': {'":': 140, '<': 9, '<?xml version=': 0, '>': 6, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2785290', 'n_tokens_mistral': 4083, 'n_tokens_neox': 3859, 'n_words': 785}