-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
282 lines (243 loc) · 7.86 KB
/
handler.ts
File metadata and controls
282 lines (243 loc) · 7.86 KB
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
import * as Ask from 'ask-sdk';
import * as thingiverse from 'thingiverse-js'
import * as sql from 'mysql2/promise'
import 'source-map-support/register';
import axios from "axios";
// const constants = require('./constants');
const api = {
thingiverseUrl: "https://api.thingiverse.com/",
thingiverseToken: "a34aca11d88f36b8bdd1793cfeafd2b0"
}
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle: handlerInput =>
handlerInput.responseBuilder
.speak(`Welcome to My three d printer`)
// .reprompt("What would you like to play?")
.withShouldEndSession(false)
.getResponse()
}
const OctoprintTestIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'OctoprintTestIntent';
},
handle(handlerInput) {
const speechText = "Octoprint Test"
// console.log("Repeat Variable: ", handlerInput.requestEnvelope.request.intent.slots.repeatName.value);
// console.log("Repeat Variable: ", handlerInput.requestEnvelope.context);
// console.log("Repeat Variable: ", handlerInput);
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Octoprint Test', speechText)
// .reprompt("test reprompt")
.getResponse();
}
}
const PrintThingIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'PrintThingIntent';
},
async handle(handlerInput) {
let data: any;
let speechText: string;
const options = {
method: 'get',
url: `${api.thingiverseUrl}search/${handlerInput.requestEnvelope.request.intent.slots.thingQuery.value}?access_token=${api.thingiverseToken}`
}
let items
try {
data = await axios(options);
items = data.data[0];
console.log( 'items', items)
} catch (e) {
console.log('error in items')
console.log(e)
}
const newoptions = {
method: 'get',
url: `${api.thingiverseUrl}things/${items.id}/files?access_token=${api.thingiverseToken}`
}
let things
let result: any;
try {
things = await axios(newoptions);
result = things.data[0];
console.log( 'result' ,result)
} catch (e) {
console.log('error in files')
console.log(e)
}
// const query = 'show tables;';
try{
const connection = await sql.createConnection({
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
charset: 'utf8mb4'
});
console.log('connected!');
let dbcontent = await connection.execute(`insert into file_urls (url) values (?)`,[result.url+'/download?access_token='+ api.thingiverseToken]);
console.log(dbcontent)
speechText = 'printing';
console.log("Speech Text", speechText)
return handlerInput.responseBuilder
.speak(speechText)
// .reprompt("test reprompt")
.withSimpleCard('Print Thing', speechText)
.getResponse();
} catch (e) {
console.log(e)
}
// speechText = `printing ${result.name}`;
speechText = 'printing';
console.log("Speech Text", speechText)
return handlerInput.responseBuilder
.speak(speechText)
// .reprompt("test reprompt")
.withSimpleCard('Print Thing', speechText)
.getResponse();
}
}
const SearchThingIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'SearchThingIntent';
},
async handle(handlerInput) {
let data: any;
let speechText: string;
let options = {
method: 'get',
url: `${api.thingiverseUrl}search/${handlerInput.requestEnvelope.request.intent.slots.thingQuery.value}?access_token=${api.thingiverseToken}`
}
let items
try {
data = await axios(options);
items = data.data[0];
console.log(items)
} catch (e) {
console.log(e)
}
options = {
method: 'get',
url: `${api.thingiverseUrl}things/${items.id}?access_token=${api.thingiverseToken}`
}
let things
let result: any;
try {
things = await axios(options);
result = data.data;
console.log(result)
} catch (e) {
console.log(e)
}
let thingString = `The files for ${items.name} are `;
// if (Array.isArray(things)) {
for (let thing of result) {
thingString += ", name: " + thing.name + ", file id: " + thing.id;
}
// } else {
// thingString += ", name: " + things.name + " file id: " + things.id;
// }
speechText = `the id for ${items.name} is ${items.id}.`;
// console.log("Name:", handlerInput.requestEnvelope.request.intent.slots.thingQuery.value)
// const speechText = handlerInput.requestEnvelope.request.intent.slots.thingQuery.value;
// console.log('Speech Text', speechText)
return handlerInput.responseBuilder
.speak(speechText)
// .reprompt("test reprompt")
.withSimpleCard('Search Thing', speechText)
.getResponse();
}
}
const NewestThingsIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'NewestThingsIntent';
},
async handle(handlerInput) {
// const speechText: string = "Returning the ten newest things on thingiverse";
const things = [];
let data: any;
const options = {
method: 'get',
url: `${api.thingiverseUrl}/newest?access_token=${api.thingiverseToken}`, // Thingiverse get newest
}
try {
data = await axios(options);
// console.log('big response', data);
console.log('data', data.data)
for (let i = 0; i < 10; i++) {
things.push(data.data[i])
// console.log('Data', data.data[i])
}
// console.log('Response ', data.data);
} catch (e) {
console.log(e)
}
let speechText: string = '';
// console.log(things);
for (let item of things) {
speechText += ", " + item.name;
// console.log(typeof item.name)
// console.log(item.name)
}
console.log('Speech Text', speechText)
return handlerInput.responseBuilder
.speak('Here are the ten newest items from thingiverse ' + speechText)
// .reprompt("test reprompt")
.withSimpleCard('Search Newest Things', speechText)
.getResponse();
}
}
const CancelIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent';
},
handle(handlerInput) {
const speechText = 'Canceled';
return handlerInput.responseBuilder
.speak(speechText)
.addAudioPlayerStopDirective()
.withShouldEndSession(true)
.getResponse();
}
};
const DefaultHandler = {
canHandle: handlerInput => true,
handle: handlerInput =>
handlerInput.responseBuilder
.speak('I do not understand your request.')
.withShouldEndSession(false)
.getResponse()
}
const myErrorHandler = {
canHandle(handlerInput, error) {
return error.name.startsWith('AskSdk');
},
handle(handlerInput, error) {
return handlerInput.responseBuilder
.speak('An error was encountered while handling your request. Try again later')
.withShouldEndSession(true)
.getResponse();
}
}
export const alexa = Ask.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
OctoprintTestIntentHandler,
PrintThingIntentHandler,
SearchThingIntentHandler,
NewestThingsIntentHandler,
CancelIntentHandler,
DefaultHandler
)
.addErrorHandlers(myErrorHandler)
.lambda();