-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcreate-file-function.js
63 lines (60 loc) · 2.32 KB
/
create-file-function.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
var fs = require('fs')
require('dotenv').config()
var authorize = require('./google/authorize.js')
var clientSecretsFile = 'client_secret.json'
var createFile = require('./google/create-file.js')
var setMimeType = require('./google/set-mime-type.js')
var winston = require('winston')
require('winston-loggly-bulk')
winston.add(winston.transports.Loggly, {
token: process.env.LOGGLY_TOKEN,
subdomain: 'acucciniello',
tags: ['Winston-NodeJS'],
json: true
})
module.exports = CreateFileFunction
// Purpose: To create a file in your google drive
// param (in): intent: given by Alexa, allows code to access parts of the intent request
// param (in): session: given by Alexa, allows code to access parts of the session in the Lambda request
// param (out): request: allows the user to change the response by Alexa
function CreateFileFunction (intent, session, response) {
var accessToken = JSON.stringify(session.user.accessToken)
var fileToMake = intent.slots.fileName.value
var fileType = intent.slots.fileType.value
fs.readFile(clientSecretsFile.toString(), function processClientSecrets (err, content) {
if (err) {
var secretsError = 'There was an issue reaching the skill'
winston.log('info', 'There was an issue reaching the skill')
response.tell(secretsError)
return
} else {
authorize(JSON.parse(content), accessToken, function (err, oauthClient) {
if (err) {
var noOauth = 'You must have a linked account to use this skill. Please use the alexa app to link your account.'
winston.log('info', 'Failed to auth')
response.tellWithLinkAccount(noOauth)
return err
}
setMimeType(fileType, function (err, mime) {
if (err) {
winston.log('info', 'Failed to get MIME Type of file')
response.tell(err)
return err
}
createFile(oauthClient, fileToMake, mime, function (err, name) {
var fileCreated = 'We created a file named: '
if (err) {
winston.log('info', 'Failed to create file')
response.tell(err)
return err
}
fileCreated = fileCreated + name
winston.log('info', 'We created a file')
response.tell(fileCreated)
return
})
})
})
}
})
}