UNPKG

botbuilder

Version:

Bot Builder is a framework for building rich bots on virtually any platform.

410 lines • 15.5 kB
"use strict"; /** * @module botbuilder */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ChannelServiceRoutes = void 0; const botbuilder_core_1 = require("botbuilder-core"); const statusCodeError_1 = require("./statusCodeError"); const activityValidator_1 = require("./activityValidator"); const routeConstants_1 = require("./routeConstants"); /** * Routes the API calls with the ChannelServiceHandler methods. */ class ChannelServiceRoutes { /** * @param channelServiceHandler The channel service handler. */ constructor(channelServiceHandler) { this.channelServiceHandler = channelServiceHandler; } /** * Registers all Channel Service paths on the provided WebServer. * * @param server WebServer * @param basePath Optional basePath which is appended before the service's REST API is configured on the WebServer. */ register(server, basePath = '') { server.post(basePath + routeConstants_1.RouteConstants.Activities, this.processSendToConversation.bind(this)); server.post(basePath + routeConstants_1.RouteConstants.Activity, this.processReplyToActivity.bind(this)); server.put(basePath + routeConstants_1.RouteConstants.Activity, this.processUpdateActivity.bind(this)); server.get(basePath + routeConstants_1.RouteConstants.ActivityMembers, this.processGetActivityMembers.bind(this)); server.post(basePath + routeConstants_1.RouteConstants.Conversations, this.processCreateConversation.bind(this)); server.get(basePath + routeConstants_1.RouteConstants.Conversations, this.processGetConversations.bind(this)); server.get(basePath + routeConstants_1.RouteConstants.ConversationMembers, this.processGetConversationMembers.bind(this)); server.get(basePath + routeConstants_1.RouteConstants.ConversationMember, this.processGetConversationMember.bind(this)); server.get(basePath + routeConstants_1.RouteConstants.ConversationPagedMembers, this.processGetConversationPagedMembers.bind(this)); server.post(basePath + routeConstants_1.RouteConstants.ConversationHistory, this.processSendConversationHistory.bind(this)); server.post(basePath + routeConstants_1.RouteConstants.Attachments, this.processUploadAttachment.bind(this)); // Express 4.x uses the delete() method to register handlers for the DELETE method. // Restify 8.x uses the del() method. if (typeof server.delete === 'function') { server.delete(basePath + routeConstants_1.RouteConstants.ConversationMember, this.processDeleteConversationMember.bind(this)); server.delete(basePath + routeConstants_1.RouteConstants.Activity, this.processDeleteActivity.bind(this)); } else if (typeof server.del === 'function') { server.del(basePath + routeConstants_1.RouteConstants.ConversationMember, this.processDeleteConversationMember.bind(this)); server.del(basePath + routeConstants_1.RouteConstants.Activity, this.processDeleteActivity.bind(this)); } } /** * @private */ processSendToConversation(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; ChannelServiceRoutes.readActivity(req) .then((activity) => { this.channelServiceHandler .handleSendToConversation(authHeader, req.params.conversationId, activity) .then((resourceResponse) => { res.status(200); if (resourceResponse) { res.send(resourceResponse); } res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processReplyToActivity(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; ChannelServiceRoutes.readActivity(req) .then((activity) => { this.channelServiceHandler .handleReplyToActivity(authHeader, req.params.conversationId, req.params.activityId, activity) .then((resourceResponse) => { res.status(200); if (resourceResponse) { res.send(resourceResponse); } res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processUpdateActivity(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; ChannelServiceRoutes.readActivity(req) .then((activity) => { this.channelServiceHandler .handleUpdateActivity(authHeader, req.params.conversationId, req.params.activityId, activity) .then((resourceResponse) => { res.status(200); if (resourceResponse) { res.send(resourceResponse); } res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processDeleteActivity(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; this.channelServiceHandler .handleDeleteActivity(authHeader, req.params.conversationId, req.params.activityId) .then(() => { res.status(200); res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processGetActivityMembers(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; this.channelServiceHandler .handleGetActivityMembers(authHeader, req.params.conversationId, req.params.activityId) .then((channelAccounts) => { if (channelAccounts) { res.send(channelAccounts); } res.status(200); res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processCreateConversation(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; ChannelServiceRoutes.readBody(req).then((conversationParameters) => { this.channelServiceHandler .handleCreateConversation(authHeader, conversationParameters) .then((conversationResourceResponse) => { if (conversationResourceResponse) { res.send(conversationResourceResponse); } res.status(201); res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); }); } /** * @private */ processGetConversations(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; this.channelServiceHandler .handleGetConversations(authHeader, req.params.conversationId, req.query.continuationToken) .then((conversationsResult) => { if (conversationsResult) { res.send(conversationsResult); } res.status(200); res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processGetConversationMembers(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; this.channelServiceHandler .handleGetConversationMembers(authHeader, req.params.conversationId) .then((channelAccounts) => { res.status(200); if (channelAccounts) { res.send(channelAccounts); } res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processGetConversationMember(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; this.channelServiceHandler .handleGetConversationMember(authHeader, req.params.memberId, req.params.conversationId) .then((channelAccount) => { res.status(200); if (channelAccount) { res.send(channelAccount); } res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processGetConversationPagedMembers(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; let pageSize = parseInt(req.query.pageSize); if (isNaN(pageSize)) { pageSize = undefined; } this.channelServiceHandler .handleGetConversationPagedMembers(authHeader, req.params.conversationId, pageSize, req.query.continuationToken) .then((pagedMembersResult) => { res.status(200); if (pagedMembersResult) { res.send(pagedMembersResult); } res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processDeleteConversationMember(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; this.channelServiceHandler .handleDeleteConversationMember(authHeader, req.params.conversationId, req.params.memberId) .then(() => { res.status(200); res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processSendConversationHistory(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; ChannelServiceRoutes.readBody(req) .then((transcript) => { this.channelServiceHandler .handleSendConversationHistory(authHeader, req.params.conversationId, transcript) .then((resourceResponse) => { if (resourceResponse) { res.send(resourceResponse); } res.status(200); res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ processUploadAttachment(req, res, next) { const authHeader = req.headers.authorization || req.headers.Authorization || ''; ChannelServiceRoutes.readBody(req) .then((attachmentData) => { this.channelServiceHandler .handleUploadAttachment(authHeader, req.params.conversationId, attachmentData) .then((resourceResponse) => { if (resourceResponse) { res.send(resourceResponse); } res.status(200); res.end(); return next(); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); }) .catch((err) => { ChannelServiceRoutes.handleError(err, res); }); } /** * @private */ static readActivity(req) { return new Promise((resolve, reject) => { if (req.body) { try { const activity = (0, activityValidator_1.validateAndFixActivity)(req.body); resolve(activity); } catch (err) { reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message)); } } else { let requestData = ''; req.on('data', (chunk) => { requestData += chunk; }); req.on('end', () => { try { const body = JSON.parse(requestData); const activity = (0, activityValidator_1.validateAndFixActivity)(body); resolve(activity); } catch (err) { reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message)); } }); } }); } /** * @private */ static readBody(req) { return new Promise((resolve, reject) => { if (req.body) { try { resolve(req.body); } catch (err) { reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message)); } } else { let requestData = ''; // eslint-disable-next-line prettier/prettier req.on('data', (chunk) => { requestData += chunk; }); req.on('end', () => { try { const body = JSON.parse(requestData); resolve(body); } catch (err) { reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message)); } }); } }); } /** * @private */ static handleError(err, res) { res.status(typeof (err === null || err === void 0 ? void 0 : err.statusCode) === 'number' ? err.statusCode : 500); if (err instanceof Error) { res.send(err.message); } else { res.send('An error occurred while processing the request.'); console.error(err); } res.end(); } } exports.ChannelServiceRoutes = ChannelServiceRoutes; //# sourceMappingURL=channelServiceRoutes.js.map