-
Notifications
You must be signed in to change notification settings - Fork 7
/
update.js
126 lines (105 loc) · 3.58 KB
/
update.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
const { TwilioClientCommand } = require("@twilio/cli-core").baseCommands,
AutopilotCore = require("@dabblelab/autopilot-core"),
ora = require("ora"),
{
convertYargsOptionsToOclifFlags,
normalizeFlags,
} = require("../../../utils"),
{ options, describe } = require("../../../lib/options/webhooks/update");
class UpdateAssistantWebhook extends TwilioClientCommand {
async run() {
await super.run();
let { flags } = await this.parse(UpdateAssistantWebhook);
flags = normalizeFlags(flags);
const eventTypes = {
ondialoguestart: "onDialogueStart",
ondialogueend: "onDialogueEnd",
ondialoguetaskstart: "onDialogueTaskStart",
onactionsfetch: "onActionsFetch",
oncollectattempt: "onCollectAttempt",
};
const {
assistantSid,
webhookSid,
webhookUniqueName,
events,
webhookURL,
method,
} = flags;
if (!flags.hasOwnProperty("assistantSid")) {
console.log(`The '--assistantSid' is required`);
return;
}
const spinner = ora();
try {
let mEvents = [],
wSid = webhookSid;
if (!webhookSid) {
spinner.start(`Getting webhook list...`);
const webhookList = await AutopilotCore.webhooks.list(
this.twilioClient,
assistantSid
),
webhookChoice = webhookList.map((t) => t.uniqueName);
spinner.stop();
if (!webhookList.length) {
console.log(
`\n No webhook found to update \n Use "twilio autopilot:webhooks:create" if you need to create a new webhook.`
);
return;
}
const answer = await this.inquirer.prompt([
{
type: "list",
name: "webhookUniqueName",
message: "Select the webhook you want to update: ",
choices: webhookChoice,
},
]);
wSid = answer.webhookUniqueName;
}
if (events) {
const eventList = [...new Set(events.toLowerCase().trim().split(" "))];
if (eventList.length) {
mEvents = eventList.filter((l) =>
eventTypes.hasOwnProperty(l.toLowerCase())
);
if (!mEvents.length) {
console.log(`The '--events' paramater can contain one or all of the following values (space separtated).
onDialogueStart
onDialogueEnd
onDialogueTaskStart
onActionsFetch
onCollectAttempt`);
return;
}
}
}
spinner.start("Updating assistant webhooks...\n");
let params = {};
if (webhookUniqueName) params["uniqueName"] = webhookUniqueName;
if (mEvents.length) params["events"] = mEvents.join(" ");
if (webhookURL) params["webhookUrl"] = webhookURL;
if (method) params["webhookMethod"] = method;
let webhook = {};
if (Object.keys(params).length)
webhook = await AutopilotCore.webhooks.update(
this.twilioClient,
assistantSid,
wSid,
params
);
spinner.stop();
console.log(`Webhooks "${wSid}" was updated`);
} catch (err) {
spinner.stop();
console.error(`ERROR: ${err}`);
}
}
}
UpdateAssistantWebhook.description = describe;
UpdateAssistantWebhook.flags = Object.assign(
convertYargsOptionsToOclifFlags(options),
{ profile: TwilioClientCommand.flags.profile }
);
module.exports = UpdateAssistantWebhook;