UNPKG

3.48 kBPlain TextView Raw
1/**
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License.
4 */
5
6import { v4 as uuidv4 } from 'uuid';
7import { Activity, ActivityTypes, Attachment, ConversationAccount, ConversationReference, Transcript, TurnContext } from 'botbuilder-core';
8import * as dayjs from 'dayjs';
9import * as timezone from 'dayjs/plugin/timezone';
10dayjs.extend(timezone);
11import { HandoffEventNames } from './handoffEventNames';
12
13/**
14 * Contains utility methods for creating various event types.
15 */
16export class EventFactory {
17 /**
18 * Create handoff initiation event.
19 * @param context The context object for the turn.
20 * @param handoffContext Agent hub-specific context.
21 * @param transcript Transcript of the conversation.
22 */
23 public static createHandoffInitiation<T = unknown>(
24 context: TurnContext,
25 handoffContext: T,
26 transcript?: Transcript
27 ): Activity {
28 if (!context) {
29 throw new TypeError('EventFactory.createHandoffInitiation(): Missing context.');
30 }
31
32 const handoffEvent = this.createHandoffEvent(
33 HandoffEventNames.InitiateHandoff,
34 handoffContext,
35 context.activity.conversation
36 );
37
38 handoffEvent.from = context.activity.from;
39 handoffEvent.relatesTo = TurnContext.getConversationReference(context.activity) as ConversationReference;
40 handoffEvent.replyToId = context.activity.id;
41 handoffEvent.serviceUrl = context.activity.serviceUrl;
42 handoffEvent.channelId = context.activity.channelId;
43
44 if (transcript) {
45 const attachment: Attachment = {
46 content: transcript,
47 contentType: 'application/json',
48 name: 'Transcript',
49 };
50 handoffEvent.attachments.push(attachment);
51 }
52
53 return handoffEvent;
54 }
55
56 /**
57 * Create handoff status event.
58 * @param conversation Conversation being handed over.
59 * @param state State, possible values are: "accepted", "failed", "completed".
60 * @param message Additional message for failed handoff.
61 */
62 public static createHandoffStatus(conversation: ConversationAccount, state: string, message?: string): Activity {
63 if (!conversation) {
64 throw new TypeError('EventFactory.createHandoffStatus(): missing conversation.');
65 }
66
67 if (!state) {
68 throw new TypeError('EventFactory.createHandoffStatus(): missing state.');
69 }
70
71 return this.createHandoffEvent(HandoffEventNames.HandoffStatus, { state, message }, conversation);
72 }
73
74 /**
75 * @private
76 */
77 private static createHandoffEvent<T = unknown>(
78 name: string,
79 value: T,
80 conversation: ConversationAccount
81 ): Activity {
82 const handoffEvent: Partial<Activity> = { type: ActivityTypes.Event };
83
84 handoffEvent.name = name;
85 handoffEvent.value = value;
86 handoffEvent.id = uuidv4();
87 handoffEvent.timestamp = new Date(Date.now());
88 // The timestamp does not contain the local offset which is a known limitation of Date objects in JavaScript.
89 // Therefore, the localTimezone is included in the handoffEvent.
90 handoffEvent.localTimezone = dayjs.tz.guess();
91 handoffEvent.conversation = conversation;
92 handoffEvent.attachments = [];
93 handoffEvent.entities = [];
94
95 return handoffEvent as Activity;
96 }
97}