UNPKG

wa-chat-server-microsoft

Version:

wa-chat-server adapter for the Microsoft Bot Framework

79 lines (78 loc) 3.38 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WAChatServerMicrosoftAdapter = void 0; const express_1 = __importDefault(require("express")); const botbuilder_1 = require("botbuilder"); const WatsonHandler_1 = require("./class/WatsonHandler"); class WAChatServerMicrosoftAdapter { constructor(config, waChatServer) { this.config = config; this.waChatServer = waChatServer; this.log = waChatServer.getLogger(); } maskValue(value) { return `${value.substr(0, 3)}...${value.substr(-3)}`; } initAdapter() { if (!WAChatServerMicrosoftAdapter.botAdapter) { const msg = `WAChatServerMicrosoft - initializing adapter`; const params = { appId: this.config.appId, appPassword: this.config.appPassword, }; const maskedParams = { appId: this.config.appId, appPassword: this.maskValue(this.config.appPassword), }; try { WAChatServerMicrosoftAdapter.botAdapter = new botbuilder_1.BotFrameworkAdapter(params); this.log.info(msg, maskedParams); } catch (error) { this.log.error(msg, maskedParams); throw error; } } } getRouter() { const router = express_1.default.Router(); router.use((req, res) => __awaiter(this, void 0, void 0, function* () { this.initAdapter(); const { path, method, headers, body } = req; if (method === 'POST' && path === '/api/messages') { this.log.info(`WAChatServerMicrosoft - message request received`, { path, method, headers, body, }); const sessionId = body.conversation.id; WAChatServerMicrosoftAdapter.botAdapter.processActivity(req, res, (context) => __awaiter(this, void 0, void 0, function* () { yield new WatsonHandler_1.WatsonHandler(sessionId, this.waChatServer).run(context); })); } else { this.log.warn(`WAChatServerMicrosoft - unsupported request received`, { path, method, headers, body, }); } })); return router; } } exports.WAChatServerMicrosoftAdapter = WAChatServerMicrosoftAdapter;