forked from palindromed/Bot-HandOff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.ts
138 lines (117 loc) · 4.64 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
import * as builder from 'botbuilder';
import { Handoff, ConversationState } 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;
// Commands to execute whether connected to a customer or not
if (inputWords[0] === 'options') {
sendAgentCommandOptions(session);
return;
} else if (inputWords[0] === 'list') {
session.send(currentConversations(handoff));
return;
}
// Commands to execute when not connected to a customer
if (!conversation) {
switch (inputWords[0]) {
case 'connect':
const newConversation = handoff.connectCustomerToAgent(
inputWords.length > 1
? { customerName: inputWords.slice(1).join(' ') }
: { bestChoice: true },
message.address
);
if (newConversation) {
session.send("You are connected to " + newConversation.customer.user.name);
} else {
session.send("No customers waiting.");
}
break;
default:
sendAgentCommandOptions(session);
break;
}
return;
}
if (conversation.state !== ConversationState.Agent) {
// error state -- should not happen
session.send("Shouldn't be in this state - agent should have been cleared out.");
console.log("Shouldn't be in this state - agent should have been cleared out");
return;
}
if (message.text === 'disconnect') {
if (handoff.connectCustomerToBot({ customerConversationId: conversation.customer.conversation.id })) {
session.send("Customer " + conversation.customer.user.name + " is now connected to the bot.");
}
return;
}
next();
}
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 *connect* to connect to customer who has been waiting longest.\n - Type *connect { user name }* to connect to a specific conversation\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;
}
});
return text;
}