-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildtr.js
185 lines (172 loc) · 5.62 KB
/
buildtr.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require('dotenv-safe').config();
const chalk = require('chalk');
const accountSid = process.env.ACCOUNT_SID;
const authToken = process.env.AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
// Creates a TR Workspace
const createWorkspace = async name => {
try {
const response = await client.taskrouter.workspaces.create({
friendlyName: name
});
return response;
} catch (e) {
throw new Error(
`Unable to create Workspace with the Friendly Name ${name}. Try using a different name.`
);
}
};
// Commented out getActivitySid function, but keeping code here in case
// we want to use this at a later date.
// const getActivitySid = async (workspace, activity) => {
// try {
// const response = await client.taskrouter
// .workspaces(workspace)
// .activities.list();
// const result = response.find(
// activities => activities.friendlyName === activity
// );
// return result.sid;
// } catch (e) {
// throw new Error(`Could not retrieve activity ${activity}`);
// }
// };
// Creates a TR TaskQueue
const createTaskQueue = async (workspace, name, skills) => {
try {
const response = await client.taskrouter
.workspaces(workspace)
.taskQueues.create({
targetWorkers: `skills HAS "${skills}"`,
friendlyName: name
});
return response;
} catch (e) {
throw new Error(`TaskQueue creation was unsuccessful`);
}
};
// Creates a TR Worker
const createWorker = async (workspace, name, skills, language) => {
try{
const response = client.taskrouter.workspaces(workspace)
.workers
.create({attributes: JSON.stringify({
skills: skills,
languages: language,
contact_uri: '+12345678910'
}), friendlyName: name});
return response;
}catch (e) {
throw new Error(`The Worker ${name} was not created.`);
}
}
// Creates a TR Workflow
const createWorkflow = async (workspace, support, sales, marketing, manager) => {
try {
client.taskrouter.workspaces(workspace).workflows.create({
taskReservationTimeout: 30,
friendlyName: 'Single Workflow',
configuration: JSON.stringify({
task_routing: {
filters: [
{
filter_friendly_name: "Support",
expression: `department=='support'`,
targets: [
{
queue: support,
expression: `task.selected_language IN worker.languages`
}
]
},
{
filter_friendly_name: "Sales",
expression: `department=='sales'`,
targets: [
{
queue: sales,
expression: `task.selected_language IN worker.languages`
}
]
},
{
filter_friendly_name: "Marketing",
expression: `department=='marketing'`,
targets: [
{
queue: marketing,
expression: `task.selected_language IN worker.languages`
}
]
},
{
filter_friendly_name: "Manager",
expression: `department=='manager'`,
targets: [
{
queue: manager
}
]
}
]
}
})
});
} catch (e) {
throw new Error(`Workflow creation was unsuccessful'`);
}
};
// Creates a TR custome Task Channel
const createTaskChannel = async (workspace, fname, uname) => {
try{
const response = client.taskrouter.workspaces(workspace)
.taskChannels
.create({
friendlyName: fname,
uniqueName: uname
});
return response;
}catch (e) {
throw new Error(`The Task Channel named ${name} was not created.`);
}
};
// Builds the full TR environment
const buildTaskRouter = async newWorkspace => {
const workspace = await createWorkspace(newWorkspace);
const supportQueue = await createTaskQueue(
workspace.sid,
'Support',
'support'
);
const salesQueue = await createTaskQueue(
workspace.sid,
'Sales',
'sales'
);
const marketingQueue = await createTaskQueue(
workspace.sid,
'Marketing',
'marketing'
);
const managerQueue = await createTaskQueue(
workspace.sid,
'Manager',
'manager'
);
const francisco = await createWorker(workspace.sid, 'Francisco', ['support', 'sales', 'marketing'], ['en', 'es', 'fr']);
const lisa = await createWorker(workspace.sid,'Lisa', 'manager', 'en');
const frank = await createWorker(workspace.sid, 'Frank', ['sales', 'marketing'], ['en', 'es']);
const workflow = await createWorkflow(
workspace.sid,
supportQueue.sid,
salesQueue.sid,
marketingQueue.sid,
managerQueue.sid
);
const emailChannel = await createTaskChannel(workspace.sid, 'EMail Channel', 'email-channel');
const twitterChannel = await createTaskChannel(workspace.sid, 'Twitter Channel', 'twitter-channel' );
return chalk `${chalk.bold('Workspace')} "{red ${workspace.friendlyName}}" has been created with the following ${chalk.bold('TaskQueues')}: {green ${supportQueue.friendlyName}}, {green ${salesQueue.friendlyName}}, {green ${marketingQueue.friendlyName}} and {green ${managerQueue.friendlyName}}! The following ${chalk.bold('Workers')} have been added to your Workspace: {blue ${francisco.friendlyName}}, {blue ${frank.friendlyName}}, and {blue ${lisa.friendlyName}}. Two custom ${chalk.bold('Task Channels')} were created: {magenta ${emailChannel.friendlyName}} and {magenta ${twitterChannel.friendlyName}}.`;
};
module.exports = {
buildTaskRouter
};