1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import { v4 as uuidv4 } from 'uuid';
|
7 | import { Activity, ActivityTypes, Attachment, ConversationAccount, ConversationReference, Transcript, TurnContext } from 'botbuilder-core';
|
8 | import * as dayjs from 'dayjs';
|
9 | import * as timezone from 'dayjs/plugin/timezone';
|
10 | dayjs.extend(timezone);
|
11 | import { HandoffEventNames } from './handoffEventNames';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export class EventFactory {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
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 |
|
58 |
|
59 |
|
60 |
|
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 |
|
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 |
|
89 |
|
90 | handoffEvent.localTimezone = dayjs.tz.guess();
|
91 | handoffEvent.conversation = conversation;
|
92 | handoffEvent.attachments = [];
|
93 | handoffEvent.entities = [];
|
94 |
|
95 | return handoffEvent as Activity;
|
96 | }
|
97 | }
|