-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsanaAlexaSkill.js
282 lines (232 loc) · 8.31 KB
/
AsanaAlexaSkill.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
var asana = require('asana');
var Promise = require("bluebird");
require('dotenv').load();
var AlexaSkill = require('./AlexaSkill');
var AsanaSkillError = require('./AsanaSkillError');
var ALEXA_APP_ID = process.env.ALEXA_APP_ID;
var ASANA_ACCESS_TOKEN = process.env.ASANA_ACCESS_TOKEN;
var ASANA_DEFAULT_WORKSPACE_ID = process.env.ASANA_DEFAULT_WORKSPACE_ID;
var AsanaSkill = function() {
AlexaSkill.call(this, ALEXA_APP_ID);
};
// Extend AlexaSkill
AsanaSkill.prototype = Object.create(AlexaSkill.prototype);
AsanaSkill.prototype.constructor = AlexaSkill;
/**
* Overriden to show that a subclass can override this function to initialize session state.
*/
AsanaSkill.prototype.eventHandlers.onSessionStarted = function(sessionStartedRequest, session) {
console.log("onSessionStarted requestId: " + sessionStartedRequest.requestId
+ ", sessionId: " + session.sessionId);
// Any session init logic would go here.
};
/**
* If the user launches without specifying an intent, route to the correct function.
*/
AsanaSkill.prototype.eventHandlers.onLaunch = function(launchRequest, session, response) {
console.log("AsanaSkill onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
handleCreateTaskIntent(session, response);
};
/**
* Overriden to show that a subclass can override this function to teardown session state.
*/
AsanaSkill.prototype.eventHandlers.onSessionEnded = function(sessionEndedRequest, session) {
console.log("onSessionEnded requestId: " + sessionEndedRequest.requestId
+ ", sessionId: " + session.sessionId);
//Any session cleanup logic would go here.
};
AsanaSkill.prototype.intentHandlers = {
"CreateTaskIntent": function(intent, session, response) {
CreateTaskIntent(intent, session, response);
},
"AMAZON.HelpIntent": function(intent, session, response) {
var speechText = "You can ask to a task to a list";
var speechOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
var repromptOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
// For the repromptText, play the speechOutput again
response.ask(speechOutput, repromptOutput);
},
"AMAZON.StopIntent": function(intent, session, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
},
"AMAZON.CancelIntent": function(intent, session, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
}
};
var CreateTaskIntent = function(intent, session, response) {
var req = {
asanaClient: getAsanaClient(session),
taskNameSlot: intent.slots.TaskName,
projectNameSlot: intent.slots.ProjectName,
user: null,
targetWorkspaceId: null,
targetProject: null,
createdTask: null
};
// TODO: Handle this by prompting the user for a task name
if (req.taskNameSlot.value === undefined) {
var err = new AsanaSkillError('Cannot create a task without a name');
var speechOutput = speechOutputForError(err);
console.log(speechOutput.speech);
response.tell(speechOutput);
return;
}
var createTaskPromise = setUser(req)
.then(setTargetWorkspaceId)
.then(setTargetProject)
.then(createTask)
.then(addProjectToTask)
.then(function(req) {
var createdInName = req.createdTask.workspace.name;
if (req.targetProject !== null) {
createdInName = req.targetProject.name;
}
var speechText = "I've added that task to " + createdInName + ".";
var speechOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
var cardTitle = "Created task in Asana";
var cardContent = req.createdTask.name + " added to " + createdInName;
console.log(speechText);
response.tellWithCard(speechOutput, cardTitle, cardContent);
})
.catch(function(err) {
var speechOutput = speechOutputForError(err);
console.log(speechOutput.speech);
if (err instanceof asana.errors.NoAuthorization) {
response.tellWithLinkAccountCard(speechOutput);
} else {
response.tell(speechOutput);
}
});
};
var speechOutputForError = function(err) {
var errorMessage = "a problem has occurred";
if (err instanceof AsanaSkillError) {
errorMessage = err.message;
} else if (err instanceof asana.errors.InvalidRequest) {
errorMessage = "the request to Asana was invalid";
} else if (err instanceof asana.errors.NoAuthorization) {
errorMessage = "your Asana credentials are missing or expired. " +
"Please use the Alexa app to link your Asana account";
} else if (err instanceof asana.errors.Forbidden) {
errorMessage = "the request to Asana wasn't allowed";
} else if (err instanceof asana.errors.NotFound) {
errorMessage = "the Asana resource wasn't found";
} else if (err instanceof asana.errors.RateLimitEnforced) {
errorMessage = "the request was rate limited by Asana, try again later";
} else if (err instanceof asana.errors.ServerError) {
errorMessage = "the Asana server experienced an error";
}
var speechText = "I wasn't able to create the task, " + errorMessage + ".";
var speechOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
return speechOutput;
};
var setUser = function(req) {
return req.asanaClient.users.me().then(function(user){
req.user = user;
return Promise.resolve(req);
});
};
var setTargetWorkspaceId = function(req) {
return getDefaultWorkspaceForUser().then(function(workspaceId){
req.targetWorkspaceId = workspaceId;
return Promise.resolve(req);
});
};
var setTargetProject = function(req) {
if (req.projectNameSlot.value === undefined) {
req.targetProject = null;
return Promise.resolve(req);
}
return findProjectInWorkspace(req.asanaClient, req.targetWorkspaceId, req.projectNameSlot.value).then(function(project){
req.targetProject = project;
return Promise.resolve(req);
});
};
var createTask = function(req) {
var taskOptions = {};
if (req.targetWorkspaceId === null) {
return Promise.reject(new AsanaSkillError('Cannot create a task without a workspace ID'));
}
if (req.taskNameSlot === null || req.taskNameSlot.value === undefined) {
return Promise.reject(new AsanaSkillError('Cannot create a task without a task name'));
} else {
taskOptions.name = req.taskNameSlot.value;
}
// Assign to a user if we don't have a project
if (req.targetProject === null) {
if (req.user === null) {
return Promise.reject(new AsanaSkillError('Cannot create a task without a user or project ID'));
}
taskOptions.assignee = req.user.id;
}
return req.asanaClient.tasks.createInWorkspace(req.targetWorkspaceId,
taskOptions).then(function(task) {
req.createdTask = task;
return Promise.resolve(req);
});
};
var addProjectToTask = function(req) {
if (req.targetWorkspaceId === null) {
return Promise.reject(new AsanaSkillError('Cannot add a project to a task without a task'));
}
if (req.targetProject === null) {
return Promise.resolve(req);
}
return req.asanaClient.tasks.addProject(req.createdTask.id, {
project: req.targetProject.id
}).then(function() {
return Promise.resolve(req);
});
};
var getDefaultWorkspaceForUser = function(user) {
if (ASANA_DEFAULT_WORKSPACE_ID !== null) {
return Promise.resolve(ASANA_DEFAULT_WORKSPACE_ID);
} else {
return Promise.resolve(user.workspaces[0].id);
}
};
/**
* Attempt a best-guess for an Asana project in the specified workspace.
*/
var findProjectInWorkspace = function(asanaClient, workspaceId, queryString) {
return asanaClient.workspaces.typeahead(workspaceId, {
type: 'project',
query: queryString,
count: 1
}).then(function(result) {
if (result.data.length > 0) {
return Promise.resolve(result.data[0]);
} else {
return Promise.resolve(null);
}
});
};
var getAsanaClient = function(session) {
var client = asana.Client.create();
if (!session || !session.user || !session.user.accessToken) {
return client.useAccessToken(ASANA_ACCESS_TOKEN);
} else {
return client.useOauth({
credentials: session.user.accessToken
});
}
};
// Create the handler that responds to the Alexa Request.
exports.handler = function(event, context) {
var skill = new AsanaSkill();
skill.execute(event, context);
};