UNPKG

lexica-dialog-facebook-messenger

Version:
187 lines 8.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const immutable_1 = require("immutable"); const req = require("request-promise-native"); const lodash_1 = require("lodash"); const Intent_1 = require("lexica-dialog-model/dist/Intent"); const Api_1 = require("lexica-dialog-core/dist/Api"); const Types_1 = require("./Types"); const Constants_1 = require("./Constants"); class FacebookMessenger { constructor(locale) { this.locale = locale; this.name = Constants_1.MESSENGER_NAME; } request(request) { const messaging = request.entry[0].messaging[0]; let botRequest; if (!lodash_1.isNil(messaging) && !lodash_1.isNil(messaging.message)) { const message = messaging.message; if (messaging.message.hasOwnProperty("text") || messaging.message.hasOwnProperty("quick_reply")) { const textMessaging = messaging; const message = textMessaging.message; if (!lodash_1.isNil(message.quick_reply)) { const command = JSON.parse(message.quick_reply.payload); botRequest = { commands: immutable_1.List([ { features: immutable_1.Map(command.features), name: command.name } ]), locale: this.locale, message: textMessaging.message.text, senderId: messaging.sender.id, type: Api_1.RequestType.TEXT }; } else { botRequest = { locale: this.locale, message: textMessaging.message.text, senderId: messaging.sender.id, type: Api_1.RequestType.TEXT }; } } else if (messaging.message.hasOwnProperty("attachments")) { const fileMessaging = messaging; const facebookType = fileMessaging.message.attachments[0].type; if (facebookType === Types_1.FileType.AUDIO || facebookType === Types_1.FileType.IMAGE || facebookType === Types_1.FileType.VIDEO || facebookType === Types_1.FileType.FILE) { let type; switch (facebookType) { case Types_1.FileType.AUDIO: type = Api_1.RequestType.AUDIO; break; case Types_1.FileType.FILE: type = Api_1.RequestType.FILE; break; case Types_1.FileType.IMAGE: type = Api_1.RequestType.IMAGE; break; case Types_1.FileType.VIDEO: type = Api_1.RequestType.VIDEO; break; default: type = Api_1.RequestType.FILE; break; } botRequest = { fileUrl: fileMessaging.message.attachments[0].payload.url, locale: this.locale, senderId: messaging.sender.id, type }; } } } else if (messaging && messaging.postback) { const postbackMessaging = messaging; const command = JSON.parse(postbackMessaging.postback.payload); botRequest = { commands: immutable_1.List([ { features: immutable_1.Map(command.features), name: command.name } ]), locale: this.locale, message: postbackMessaging.postback.title, senderId: messaging.sender.id, type: Api_1.RequestType.TEXT }; } if (lodash_1.isNil(botRequest)) { throw new Error("Does not support the request: " + request); } return botRequest; } response(responses, senderId, request) { const baseMessage = { messaging_type: Types_1.MessagingType.RESPONSE, recipient: { id: senderId } }; const facebookResponses = responses.toArray().map(response => { let result; if (response.type === Intent_1.ResponseType.TEXT) { result = Object.assign({}, baseMessage, { message: { text: response.message } }); } else if (response.type === Intent_1.ResponseType.OPTIONS && response.options.length <= 3) { result = Object.assign({}, baseMessage, { message: { attachment: { payload: { buttons: response.options.map(option => ({ payload: JSON.stringify({ features: option.features, name: option.command }), title: option.message, type: Types_1.ButtonType.POSTBACK })), template_type: Types_1.ResponseMessageTemplateType.BUTTON, text: response.message }, type: Types_1.ResponseMessageType.TEMPLATE } } }); } else if (response.type === Intent_1.ResponseType.OPTIONS) { result = Object.assign({}, baseMessage, { message: { quick_replies: response.options.map(option => ({ content_type: Types_1.QuickReplyMessageContentType.TEXT, payload: JSON.stringify({ features: option.features, name: option.command }), title: option.message })), text: response.message } }); } else if (response.type === Intent_1.ResponseType.ITEMS) { result = Object.assign({}, baseMessage, { message: { attachment: { payload: { elements: response.items.map(item => ({ buttons: [ { title: item.message, type: Types_1.ButtonType.WEB_URL, url: item.url } ], title: " " })), template_type: Types_1.ResponseMessageTemplateType.GENERIC }, type: Types_1.ResponseMessageType.TEMPLATE } } }); } return result; }); return immutable_1.List(facebookResponses); } async send(responses, configs) { for (const response of responses.toArray()) { await req({ json: response, method: "POST", qs: { access_token: configs.get(Constants_1.CONFIG_FACEBOOK_ACCESS_TOKEN).value }, uri: `${configs.get(Constants_1.CONFIG_FACEBOOK_API_BASE_URL) .value}/me/messages` }); } } } exports.default = FacebookMessenger; //# sourceMappingURL=FacebookMessenger.js.map