forked from palindromed/Bot-HandOff
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovider.ts
119 lines (97 loc) · 3.22 KB
/
provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as builder from 'botbuilder';
import { Provider, Conversation, By, ConversationState } from './handoff';
export let conversations: Conversation[];
export const init = () => {
conversations = [];
}
// Update
const addToTranscript = (by: By, text: string) => {
const conversation = getConversation(by);
if (!conversation)
return false;
conversation.transcript.push({
timestamp: Date.now(),
from: by.agentConversationId ? 'agent' : 'customer',
text
});
return true;
}
const connectCustomerToAgent = (by: By, agentAddress: builder.IAddress) => {
const conversation = getConversation(by);
if (conversation) {
conversation.state = ConversationState.Agent;
conversation.agent = agentAddress;
}
return conversation;
}
const queueCustomerForAgent = (by: By) => {
const conversation = getConversation(by);
if (!conversation)
return false;
conversation.state = ConversationState.Waiting;
if (conversation.agent)
delete conversation.agent;
return true;
}
const connectCustomerToBot = (by: By) => {
const conversation = getConversation(by);
if (!conversation)
return false;
conversation.state = ConversationState.Bot;
if (conversation.agent)
delete conversation.agent;
return true;
}
// Get
const getConversation = (
by: By,
customerAddress?: builder.IAddress // if looking up by customerConversationId, create new conversation if one doesn't already exist
) => {
// local function to create a conversation if customer does not already have one
const createConversation = (customerAddress: builder.IAddress) => {
const conversation = {
customer: customerAddress,
state: ConversationState.Bot,
transcript: []
};
conversations.push(conversation);
return conversation;
}
if (by.bestChoice) {
const waitingLongest = conversations
.filter(conversation => conversation.state === ConversationState.Waiting)
.sort((x, y) => y.transcript[y.transcript.length - 1].timestamp - x.transcript[x.transcript.length - 1].timestamp);
return waitingLongest.length > 0 && waitingLongest[0];
}
if (by.customerName) {
return conversations.find(conversation =>
conversation.customer.user.name == by.customerName
);
} else if (by.agentConversationId) {
return conversations.find(conversation =>
conversation.agent && conversation.agent.conversation.id === by.agentConversationId
);
} else if (by.customerConversationId) {
let conversation = conversations.find(conversation =>
conversation.customer.conversation.id === by.customerConversationId
);
if (!conversation && customerAddress) {
conversation = createConversation(customerAddress);
}
return conversation;
}
return null;
}
const currentConversations = () =>
conversations;
export const defaultProvider: Provider = {
init,
// Update
addToTranscript,
connectCustomerToAgent,
connectCustomerToBot,
queueCustomerForAgent,
// Get
getConversation,
currentConversations,
}