UNPKG

3.29 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License.
5 */
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.EventFactory = void 0;
8const uuid_1 = require("uuid");
9const botbuilder_core_1 = require("botbuilder-core");
10const dayjs = require("dayjs");
11const timezone = require("dayjs/plugin/timezone");
12dayjs.extend(timezone);
13const handoffEventNames_1 = require("./handoffEventNames");
14/**
15 * Contains utility methods for creating various event types.
16 */
17class EventFactory {
18 /**
19 * Create handoff initiation event.
20 * @param context The context object for the turn.
21 * @param handoffContext Agent hub-specific context.
22 * @param transcript Transcript of the conversation.
23 */
24 static createHandoffInitiation(context, handoffContext, transcript) {
25 if (!context) {
26 throw new TypeError('EventFactory.createHandoffInitiation(): Missing context.');
27 }
28 const handoffEvent = this.createHandoffEvent(handoffEventNames_1.HandoffEventNames.InitiateHandoff, handoffContext, context.activity.conversation);
29 handoffEvent.from = context.activity.from;
30 handoffEvent.relatesTo = botbuilder_core_1.TurnContext.getConversationReference(context.activity);
31 handoffEvent.replyToId = context.activity.id;
32 handoffEvent.serviceUrl = context.activity.serviceUrl;
33 handoffEvent.channelId = context.activity.channelId;
34 if (transcript) {
35 const attachment = {
36 content: transcript,
37 contentType: 'application/json',
38 name: 'Transcript',
39 };
40 handoffEvent.attachments.push(attachment);
41 }
42 return handoffEvent;
43 }
44 /**
45 * Create handoff status event.
46 * @param conversation Conversation being handed over.
47 * @param state State, possible values are: "accepted", "failed", "completed".
48 * @param message Additional message for failed handoff.
49 */
50 static createHandoffStatus(conversation, state, message) {
51 if (!conversation) {
52 throw new TypeError('EventFactory.createHandoffStatus(): missing conversation.');
53 }
54 if (!state) {
55 throw new TypeError('EventFactory.createHandoffStatus(): missing state.');
56 }
57 return this.createHandoffEvent(handoffEventNames_1.HandoffEventNames.HandoffStatus, { state, message }, conversation);
58 }
59 /**
60 * @private
61 */
62 static createHandoffEvent(name, value, conversation) {
63 const handoffEvent = { type: botbuilder_core_1.ActivityTypes.Event };
64 handoffEvent.name = name;
65 handoffEvent.value = value;
66 handoffEvent.id = uuid_1.v4();
67 handoffEvent.timestamp = new Date(Date.now());
68 // The timestamp does not contain the local offset which is a known limitation of Date objects in JavaScript.
69 // Therefore, the localTimezone is included in the handoffEvent.
70 handoffEvent.localTimezone = dayjs.tz.guess();
71 handoffEvent.conversation = conversation;
72 handoffEvent.attachments = [];
73 handoffEvent.entities = [];
74 return handoffEvent;
75 }
76}
77exports.EventFactory = EventFactory;
78//# sourceMappingURL=eventFactory.js.map
\No newline at end of file