UNPKG

lexica-dialog-facebook-messenger

Version:
39 lines 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const req = require("request-promise-native"); const lodash_1 = require("lodash"); const Constants_1 = require("./Constants"); const facebookSenderInfoMiddleware = async (context, next) => { const { uni, uniConfigs, messenger, request, senderInfo, senderInfoRepository } = context; if (messenger.name === Constants_1.MESSENGER_NAME && !lodash_1.isNil(request) && lodash_1.isNil(senderInfo)) { const { senderId } = request; const apiBaseUrl = uniConfigs.get(Constants_1.CONFIG_FACEBOOK_API_BASE_URL) .value; const accessToken = uniConfigs.get(Constants_1.CONFIG_FACEBOOK_ACCESS_TOKEN) .value; const response = await req({ json: true, qs: { access_token: uniConfigs.get(Constants_1.CONFIG_FACEBOOK_ACCESS_TOKEN) .value, fields: "first_name,last_name,middle_name" }, url: `${apiBaseUrl}/${senderId}` }); context.senderInfo = await senderInfoRepository.create({ creationDate: new Date(), firstName: response.first_name, lastName: response.last_name, lastUpdatedDate: new Date(), messenger: Constants_1.MESSENGER_NAME, middleName: response.middle_name, senderId, uni }); } await next(); }; exports.default = facebookSenderInfoMiddleware; //# sourceMappingURL=FacebookSenderInfoMiddleware.js.map