{"size": 5001, "ext": "js", "max_stars_repo_path": "api/services/FacebookService.js", "max_stars_repo_name": "cooleraid/bmatch", "max_stars_repo_stars_event_min_datetime": "2020-11-02T01:06:10.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-07T08:57:47.000Z", "max_issues_repo_path": "api/services/FacebookService.js", "max_issues_repo_name": "cooleraid/bmatch", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/services/FacebookService.js", "max_forks_repo_name": "cooleraid/bmatch", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.1527777778, "max_line_length": 76, "alphanum_fraction": 0.5284943011}

/**
 * FacebookService.js
 */
const { promisify } = require('util')
module.exports = {

  async facebook(userData, responseData) {
    this.senderAction(userData, "mark_seen");
    this.senderAction(userData, "typing_on");
    const sleep = promisify(setTimeout)
    await sleep(2000)
    if (responseData['messageType'] == 'message') {
      return this.message(userData, responseData);
    } else if (responseData['messageType'] == 'audio') {
      return this.audio(userData, responseData);
    } else if (responseData['messageType'] == 'image') {
      return this.image(userData, responseData);
    } else if (responseData['messageType'] == 'video') {
      return this.video(userData, responseData);
    } else if (responseData['messageType'] == 'quick_reply') {
      return this.quickReply(userData, responseData);
    } else if (responseData['messageType'] == 'location') {
      return this.location(userData, responseData);
    } else if (responseData['messageType'] == 'contact') {
      return this.contact(userData, responseData);
    } else if (responseData['messageType'] == 'generic_template') {
      return this.genericTemplate(userData, responseData);
    } else if (responseData['messageType'] == 'button_template') {
      return this.buttonTemplate(userData, responseData);
    }
  },

  async senderAction(userData, senderAction) {
    const data = {
      recipient: {
        id: userData['chatId']
      },
      sender_action: senderAction
    };
    return await RequestService.notify(userData, data);
  },

  async message(userData, responseData) {
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        text: responseData['text']
      }
    };
    return data;
  },

  async audio(userData, responseData) {
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        attachment: {
          type: 'audio',
          payload: {
            url: responseData['text']
          }
        }
      }
    };
    return data;
  },

  async image(userData, responseData) {
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        attachment: {
          type: 'image',
          payload: {
            url: responseData['text']
          }
        }
      }
    };
    return data;
  },

  async video(userData, responseData) {
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        attachment: {
          type: 'video',
          payload: {
            url: responseData['text']
          }
        }
      }
    };
    return data;
  },


  async quickReply(userData, responseData) {
    const dataArr = [];
    for (const resData of responseData['data']) {
      dataArr.push({
        content_type: 'text', title: resData['name'], payload: resData['id']
      });
    }
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        text: responseData['text'],
        quick_replies: dataArr
      }
    };
    return data;
  },

  async location(userData, responseData) {
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        text: responseData['text'],
        quick_replies: [{ content_type: 'location' }]
      }
    };
    return data;

  },

  async contact(userData, responseData) {
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        text: responseData['text'],
        quick_replies: [{ "content_type": "user_phone_number" }]
      }
    };
    return data;
  },

  async genericTemplate(userData, responseData) {
    const dataArr = [];
    for (resData of responseData['data']) {
      if (resData['id'] == 'web_url') {
        dataArr.push({
          title: resData['name'],
          image_url: resData['image'],
          subtitle: resData['description']
        });
      } else {
        dataArr.push({
          "title": resData['name'],
          "image_url": resData['image'],
          "subtitle": resData['description']
        });
      }
    }

    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        attachment: {
          type: 'template',
          payload: {
            template_type: 'generic',
            elements: dataArr
          },
        },
      },
    };
    return data;
  },

  async buttonTemplate(userData, responseData) {
    const dataArr = [];
    for (resData of responseData['data']) {
      dataArr.push({
        type: 'postback',
        title: resData['name'],
        payload: resData['id']
      });
    }
    const data = {
      recipient: {
        id: userData['chatId']
      },
      message: {
        attachment: {
          type: 'template',
          payload: {
            template_type: 'button',
            text: responseData['text'],
            buttons: dataArr
          },
        },
      },
    };
    return data;
  },
};
