forked from palindromed/Bot-HandOff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.ts
167 lines (150 loc) · 6.18 KB
/
commands.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import * as builder from 'botbuilder';
import { Conversation, ConversationState, Handoff } from './handoff';
export function commandsMiddleware(handoff: Handoff) {
return {
botbuilder: (session: builder.Session, next: Function) => {
if (session.message.type === 'message') {
command(session, next, handoff);
}
}
}
}
function command(
session: builder.Session,
next: Function,
handoff: Handoff
) {
if (handoff.isAgent(session)) {
agentCommand(session, next, handoff);
} else {
customerCommand(session, next, handoff);
}
}
function agentCommand(
session: builder.Session,
next: Function,
handoff: Handoff
) {
const message = session.message;
const conversation = handoff.getConversation({ agentConversationId: message.address.conversation.id });
const inputWords = message.text.split(' ');
if (inputWords.length == 0)
return;
if (message.text === 'disconnect') {
disconnectCustomer(conversation, handoff, session);
return;
}
// Commands to execute whether connected to a customer or not
switch (inputWords[0]) {
case 'options':
sendAgentCommandOptions(session);
return;
case 'list':
session.send(currentConversations(handoff));
return;
case 'history':
handoff.getCustomerTranscript(
inputWords.length > 1
? { customerName: inputWords.slice(1).join(' ') }
: { agentConversationId: message.address.conversation.id },
session);
return;
case 'waiting':
if (conversation) {
//disconnect from current conversation if already watching/talking
disconnectCustomer(conversation, handoff, session);
}
const waitingConversation = handoff.connectCustomerToAgent(
{ bestChoice: true },
ConversationState.Agent,
message.address
);
if (waitingConversation) {
session.send("You are connected to " + waitingConversation.customer.user.name);
} else {
session.send("No customers waiting.");
}
return;
case 'connect':
case 'watch':
let newConversation;
if (inputWords[0] === 'connect') {
newConversation = handoff.connectCustomerToAgent(
inputWords.length > 1
? { customerName: inputWords.slice(1).join(' ') }
: { customerConversationId: conversation.customer.conversation.id },
ConversationState.Agent,
message.address
);
} else {
// watch currently only supports specifying a customer to watch
newConversation = handoff.connectCustomerToAgent(
{ customerName: inputWords.slice(1).join(' ') },
ConversationState.Watch,
message.address
);
}
if (newConversation) {
session.send("You are connected to " + newConversation.customer.user.name);
return;
} else {
session.send("something went wrong.");
}
return;
default:
if (conversation && conversation.state === ConversationState.Agent) {
return next();
}
sendAgentCommandOptions(session);
return;
}
}
function customerCommand(session: builder.Session, next: Function, handoff: Handoff) {
const message = session.message;
if (message.text === 'help') {
// lookup the conversation (create it if one doesn't already exist)
const conversation = handoff.getConversation({ customerConversationId: message.address.conversation.id }, message.address);
if (conversation.state == ConversationState.Bot) {
handoff.addToTranscript({ customerConversationId: conversation.customer.conversation.id }, message.text);
handoff.queueCustomerForAgent({ customerConversationId: conversation.customer.conversation.id })
session.send("Connecting you to the next available agent.");
return;
}
}
return next();
}
function sendAgentCommandOptions(session: builder.Session) {
const commands = ' ### Agent Options\n - Type *waiting* to connect to customer who has been waiting longest.\n - Type *connect { user name }* to connect to a specific conversation\n - Type *watch { user name }* to monitor a customer conversation\n - Type *history { user name }* to see a transcript of a given user\n - Type *list* to see a list of all current conversations.\n - Type *disconnect* while talking to a user to end a conversation.\n - Type *options* at any time to see these options again.';
session.send(commands);
return;
}
function currentConversations(handoff) {
const conversations = handoff.currentConversations();
if (conversations.length === 0) {
return "No customers are in conversation.";
}
let text = '### Current Conversations \n';
conversations.forEach(conversation => {
const starterText = ' - *' + conversation.customer.user.name + '*';
switch (ConversationState[conversation.state]) {
case 'Bot':
text += starterText + ' is talking to the bot\n';
break;
case 'Agent':
text += starterText + ' is talking to an agent\n';
break;
case 'Waiting':
text += starterText + ' is waiting to talk to an agent\n';
break;
case 'Watch':
text += starterText + ' is being monitored by an agent\n';
break;
}
});
return text;
}
function disconnectCustomer(conversation: Conversation, handoff: any, session: builder.Session) {
if (handoff.connectCustomerToBot({ customerConversationId: conversation.customer.conversation.id })) {
session.send("Customer " + conversation.customer.user.name + " is now connected to the bot.");
}
}