botbuilder
Version:
Bot Builder is a framework for building rich bots on virtually any platform.
82 lines • 3.39 kB
JavaScript
;
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventFactory = void 0;
const uuid_1 = require("uuid");
const botbuilder_core_1 = require("botbuilder-core");
const dayjs = require("dayjs");
const timezone = require("dayjs/plugin/timezone");
dayjs.extend(timezone);
const handoffEventNames_1 = require("./handoffEventNames");
/**
* Contains utility methods for creating various event types.
*/
class EventFactory {
/**
* Create handoff initiation event.
*
* @param context The context object for the turn.
* @param handoffContext Agent hub-specific context.
* @param transcript Transcript of the conversation.
* @returns The handoff event activity.
*/
static createHandoffInitiation(context, handoffContext, transcript) {
if (!context) {
throw new TypeError('EventFactory.createHandoffInitiation(): Missing context.');
}
const handoffEvent = this.createHandoffEvent(handoffEventNames_1.HandoffEventNames.InitiateHandoff, handoffContext, context.activity.conversation);
handoffEvent.from = context.activity.from;
handoffEvent.relatesTo = botbuilder_core_1.TurnContext.getConversationReference(context.activity);
handoffEvent.replyToId = context.activity.id;
handoffEvent.serviceUrl = context.activity.serviceUrl;
handoffEvent.channelId = context.activity.channelId;
if (transcript) {
const attachment = {
content: transcript,
contentType: 'application/json',
name: 'Transcript',
};
handoffEvent.attachments.push(attachment);
}
return handoffEvent;
}
/**
* Create handoff status event.
*
* @param conversation Conversation being handed over.
* @param state State, possible values are: "accepted", "failed", "completed".
* @param message Additional message for failed handoff.
* @returns The handoff event activity.
*/
static createHandoffStatus(conversation, state, message) {
if (!conversation) {
throw new TypeError('EventFactory.createHandoffStatus(): missing conversation.');
}
if (!state) {
throw new TypeError('EventFactory.createHandoffStatus(): missing state.');
}
return this.createHandoffEvent(handoffEventNames_1.HandoffEventNames.HandoffStatus, { state, message }, conversation);
}
/**
* @private
*/
static createHandoffEvent(name, value, conversation) {
const handoffEvent = { type: botbuilder_core_1.ActivityTypes.Event };
handoffEvent.name = name;
handoffEvent.value = value;
handoffEvent.id = uuid_1.v4();
handoffEvent.timestamp = new Date(Date.now());
// The timestamp does not contain the local offset which is a known limitation of Date objects in JavaScript.
// Therefore, the localTimezone is included in the handoffEvent.
handoffEvent.localTimezone = dayjs.tz.guess();
handoffEvent.conversation = conversation;
handoffEvent.attachments = [];
handoffEvent.entities = [];
return handoffEvent;
}
}
exports.EventFactory = EventFactory;
//# sourceMappingURL=eventFactory.js.map