-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudioSetup.js
executable file
·176 lines (159 loc) · 5.18 KB
/
studioSetup.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
#!/usr/bin/env node
const { waitForDebugger } = require('inspector');
require('dotenv').config();
const client = require("twilio")(
process.env.ACCOUNT_SID,
process.env.ACCOUNT_SECRET
);
function getWorkspaceSid() {
console.log("Getting the Flex TaskRouter Workspace SID.")
return new Promise((resolve, reject) => {
client.taskrouter.workspaces
.list({limit: 20})
.then(workspaces => {
workspaces.forEach(w => {
if (w.friendlyName === "Flex Task Assignment") {
console.log(`Found Workspace ${w.sid}.`)
resolve(w.sid);
return;
}
})
reject('Could not find Flex Task Assignment workspace. Did you change the friendly name?')
})
.catch(err => reject(err));
});
}
function getDidWorkflowSid(workspaceSid) {
return new Promise((resolve, reject) => {
client.taskrouter.workspaces(workspaceSid)
.workflows
.list({limit: 20})
.then(workflows => {
workflows.forEach(wf => {
if (wf.friendlyName === "Direct Inward Dial") {
console.log(`Found Direct Inward Dial Workflow ${wf.sid}.`)
resolve(wf.sid);
return;
}
});
reject('Could not find Direct Inward Dial workflow. You can create one by running workflow-setup');
return;
})
.catch(err => reject(err));
})
}
function getVoiceTaskChannelSid(workspaceSid) {
return new Promise((resolve, reject) => {
client.taskrouter.workspaces(workspaceSid)
.taskChannels
.list({limit: 20})
.then(taskChannels => {
taskChannels.forEach(tc => {
if (tc.friendlyName === "Voice") {
console.log(`Found Voice TaskChannel Sid ${tc.sid}.`)
resolve(tc.sid);
return;
}
});
reject('Could not find Voice TaskChannel. Did you change the TaskChannel friendly name?')
})
.catch(err => reject(err));
})
}
function createDidStudioFlow(didWorkflowSid, voiceTaskChannelSid) {
console.log(
"Creating the Studio Flow."
);
const studioFlowDef = {
"description": "Direct Inbound",
"states": [
{
"name": "Trigger",
"type": "trigger",
"transitions": [
{
"event": "incomingMessage"
},
{
"next": "Welcome",
"event": "incomingCall"
},
{
"event": "incomingRequest"
}
],
"properties": {
"offset": {
"x": 40,
"y": -120
}
}
},
{
"name": "ConnectToAgentByDID",
"type": "send-to-flex",
"transitions": [
{
"event": "callComplete"
},
{
"event": "failedToEnqueue"
},
{
"event": "callFailure"
}
],
"properties": {
"offset": {
"x": 70,
"y": 330
},
"workflow": didWorkflowSid,
"channel": voiceTaskChannelSid,
"attributes": "{\n\"name\": \"{{trigger.call.From}}\" ,\n\"phone_number\": \"{{trigger.call.To}}\",\n\"type\": \"inbound\", \n\"direction\": \"inbound\"}",
}
},
{
"name": "Welcome",
"type": "say-play",
"transitions": [
{
"next": "ConnectToAgentByDID",
"event": "audioComplete"
}
],
"properties": {
"voice": "Polly.Joanna",
"offset": {
"x": 60,
"y": 70
},
"loop": 1,
"say": " ",
"language": "en-US"
}
}
],
"initial_state": "Trigger",
"flags": {
"allow_concurrent_calls": true
}
}
client.studio.flows
.create({
commitMessage: 'First draft',
friendlyName: 'Direct Inward Dial',
status: 'published',
definition: studioFlowDef
})
.then(flow => console.log(`Done! Successfully created ${flow.sid}`))
.catch(err => console.log(err.details.errors));
}
async function run() {
console.log('\n=== Studio Setup ===\n')
const workspaceSid = await getWorkspaceSid();
const didWorkflowSid = await getDidWorkflowSid(workspaceSid);
const voiceTaskChannelSid = await getVoiceTaskChannelSid(workspaceSid);
createDidStudioFlow(didWorkflowSid, voiceTaskChannelSid);
}
run();