UNPKG

3.37 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 });
7const botbuilder_core_1 = require("botbuilder-core");
8const moment = require("moment-timezone");
9const handoffEventNames_1 = require("./handoffEventNames");
10/**
11 * Contains utility methods for creating various event types.
12 */
13class EventFactory {
14 /**
15 * Create handoff initiation event.
16 * @param context The context object for the turn.
17 * @param handoffContext Agent hub-specific context.
18 * @param transcript Transcript of the conversation.
19 */
20 static createHandoffInitiation(context, handoffContext, transcript) {
21 if (!context) {
22 throw new TypeError('EventFactory.createHandoffInitiation(): Missing context.');
23 }
24 const handoffEvent = this.createHandoffEvent(handoffEventNames_1.HandoffEventNames.InitiateHandoff, handoffContext, context.activity.conversation);
25 handoffEvent.from = context.activity.from;
26 handoffEvent.relatesTo = botbuilder_core_1.TurnContext.getConversationReference(context.activity);
27 handoffEvent.replyToId = context.activity.id;
28 handoffEvent.serviceUrl = context.activity.serviceUrl;
29 handoffEvent.channelId = context.activity.channelId;
30 if (transcript) {
31 const attachment = {
32 content: transcript,
33 contentType: 'application/json',
34 name: 'Transcript'
35 };
36 handoffEvent.attachments.push(attachment);
37 }
38 return handoffEvent;
39 }
40 /**
41 * Create handoff status event.
42 * @param conversation Conversation being handed over.
43 * @param state State, possible values are: "accepted", "failed", "completed".
44 * @param message Additional message for failed handoff.
45 */
46 static createHandoffStatus(conversation, state, message) {
47 if (!conversation) {
48 throw new TypeError('EventFactory.createHandoffStatus(): missing conversation.');
49 }
50 if (!state) {
51 throw new TypeError('EventFactory.createHandoffStatus(): missing state.');
52 }
53 const value = { state, message };
54 const handoffEvent = this.createHandoffEvent(handoffEventNames_1.HandoffEventNames.HandoffStatus, value, conversation);
55 return handoffEvent;
56 }
57 static createHandoffEvent(name, value, conversation) {
58 const handoffEvent = {};
59 handoffEvent.name = name;
60 handoffEvent.value = value;
61 handoffEvent.id = uuid();
62 handoffEvent.timestamp = new Date(Date.now());
63 // The timestamp does not contain the local offset which is a known limitation of Date objects in JavaScript.
64 // Therefore, the localTimezone is included in the handoffEvent.
65 handoffEvent.localTimezone = moment.tz.guess();
66 handoffEvent.conversation = conversation;
67 handoffEvent.attachments = [];
68 handoffEvent.entities = [];
69 return handoffEvent;
70 }
71}
72exports.EventFactory = EventFactory;
73function uuid() {
74 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
75 let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
76 return v.toString(16);
77 });
78}
79//# sourceMappingURL=eventFactory.js.map
\No newline at end of file