|
var Arrow = require('arrow'); |
|
var verifier = require('alexa-verifier'); |
|
|
|
var appName = "Sync Demo"; |
|
var helpTxt = "Welcome to "+appName+". You can ask "+appName+" for new purchase orders."; |
|
var launchTxt = "Welcome to "+appName+". You can ask "+appName+" to get your list of folders."; |
|
var userNotFoundTxt = "You need to setup your account before using this skill. Contact your administrator."; |
|
var failedLoginTxt = "Login to Syncplicity failed. Contact your administrator."; |
|
|
|
var POFolderName = "PO"; |
|
var isVerifier = false; |
|
var newFilesOnly; |
|
|
|
var userVars = { |
|
userFound: false, |
|
userLoggedInToSync: false, |
|
userFname: null, |
|
userLastPOTimeCheck: null, |
|
userMobile: null, |
|
newFilesOnly: true, |
|
syncAppToken: null, |
|
syncAppAuth: null, |
|
syncClient_id: null, |
|
syncAccess_token: null |
|
}; |
|
|
|
var AlexaAppHandler = Arrow.API.extend({ |
|
group: 'alexa', |
|
path: '/api/alexaapphandler', |
|
method: 'POST', |
|
description: 'this is an api that shows how to handle requests from the Alexa Skill Interface', |
|
parameters: { |
|
version: {description: 'version'}, |
|
session: {description: 'session'}, |
|
request: {description: 'request'}, |
|
context: {description: 'context', optional: true} |
|
}, |
|
action: function(req, resp, next) { |
|
console.log('\nalexaapphandler: AlexaAppHandler API called'); |
|
var requestRawBody = JSON.stringify(req.body); |
|
console.log('\nalexaapphandler: requestRawBody = '+requestRawBody); |
|
|
|
if(isVerifier) { |
|
verifyAlexaRequest(req, function(e){ |
|
if(e.success) { |
|
alexaskill(req, resp, next); |
|
} else { |
|
console.log(e.data); |
|
} |
|
}); |
|
} else { |
|
alexaskill(req, resp, next); |
|
} |
|
|
|
} |
|
}); |
|
module.exports = AlexaAppHandler; |
|
|
|
var alexaskill = function(req, resp, next) { |
|
console.log('alexaapphandler: alexaskill called'); |
|
|
|
resetUserVars(userVars); |
|
|
|
switch (req.body.request.type) { |
|
case "LaunchRequest": |
|
console.log("alexaapphandler: LaunchRequest"); |
|
|
|
newFilesOnly = true; |
|
getFolderHandler(req, resp, next, userVars, newFilesOnly); |
|
|
|
break; |
|
|
|
case "IntentRequest": |
|
|
|
switch (req.body.request.intent.name) { |
|
|
|
case "newpoIntent": |
|
console.log("alexaapphandler: newpoIntent"); |
|
|
|
newFilesOnly = true; |
|
getFolderHandler(req, resp, next, userVars, newFilesOnly); |
|
|
|
break; |
|
|
|
case "allpoIntent": |
|
console.log("alexaapphandler: allpoIntent"); |
|
|
|
newFilesOnly = false; |
|
getFolderHandler(req, resp, next, userVars, newFilesOnly); |
|
|
|
break; |
|
|
|
case "AMAZON.YesIntent": |
|
console.log("alexaapphandler: AMAZON.YesIntent"); |
|
|
|
// sendResponse(req, resp, next, "You said yes. Goodbye for now.", true, {}); |
|
|
|
if('attributes' in req.body.session) { |
|
if('sessionState' in req.body.session.attributes) { |
|
if(req.body.session.attributes.sessionState === "readSyncFolders") { |
|
readFoldersResponse(req, resp, next, req.body.session.attributes.folders, userVars); |
|
} else if(req.body.session.attributes.sessionState === "readSyncFolder") { |
|
readFileName(req, resp, next, req.body.session.attributes); |
|
} else if(req.body.session.attributes.sessionState === "readFile") { |
|
sendFile(req, resp, next, req.body.session.attributes); |
|
} else { |
|
sendResponse(req, resp, next, "Sorry, I'm not sure what you are saying yes to. Goodbye for now.", true, {}); |
|
} |
|
} else { |
|
sendResponse(req, resp, next, "Sorry, I'm not sure what you are saying yes to. Goodbye for now.", true, {}); |
|
} |
|
} else { |
|
sendResponse(req, resp, next, "Sorry, I'm not sure what you are saying yes to. Goodbye for now.", true, {}); |
|
} |
|
|
|
break; |
|
|
|
case "AMAZON.NoIntent": |
|
console.log("alexaapphandler: AMAZON.NoIntent"); |
|
|
|
if(req.body.session.attributes.sessionState === "readFile") { |
|
req.body.session.attributes.sessionState = "readSyncFolder"; |
|
req.body.session.attributes.fileIndex = req.body.session.attributes.fileIndex+1; |
|
if(req.body.session.attributes.fileIndex >= req.body.session.attributes.numFiles) { |
|
sendResponse(req, resp, next, "That was the last file. Goodbye", true, "", {}); |
|
} else { |
|
readFileName(req, resp, next, req.body.session.attributes); |
|
} |
|
|
|
} else { |
|
sendResponse(req, resp, next, "Goodbye", true, "", {}); |
|
} |
|
|
|
|
|
break; |
|
|
|
case "AMAZON.HelpIntent": |
|
console.log("alexaapphandler: AMAZON.HelpIntent"); |
|
|
|
login(req.body.session.user.userId, userVars, function(e){ |
|
sendResponse(req, resp, next, generateHelpText(userVars), true, "", {}); |
|
}); |
|
|
|
break; |
|
|
|
default: |
|
console.log("alexaapphandler: Invalid intent"); |
|
} |
|
|
|
break; |
|
|
|
case "SessionEndedRequest": |
|
// Session Ended Request |
|
console.log("alexaapphandler: SessionEndedRequest"); |
|
break; |
|
|
|
default: |
|
console.log('alexaapphandler: INVALID REQUEST TYPE:' + req.body.request.type); |
|
} |
|
}; |
|
|
|
function sendFile(req, resp, next, sessionAttributes) { |
|
console.log("alexaapphandler: sendFile called, sessionAttributes = "+JSON.stringify(sessionAttributes)); |
|
|
|
var files = sessionAttributes.files; |
|
var fileIndex = sessionAttributes.fileIndex; |
|
var numFiles = sessionAttributes.numFiles; |
|
|
|
shareFile(sessionAttributes.userVars, files[fileIndex].SyncpointId, files[fileIndex].Filename, function(e){ |
|
console.log("alexaapphandler: shareFile returned "+JSON.stringify(e.data)); |
|
if(fileIndex == numFiles-1) { |
|
sendResponse(req, resp, next, "Text message sent ... That was the last file. Goodbye.", true, "", {}); |
|
} else { |
|
sessionAttributes.fileIndex = fileIndex+1; |
|
var nextFile = files[sessionAttributes.fileIndex]; |
|
sessionAttributes.sessionState = "readFile"; |
|
sendResponse(req, resp, next, "Text message sent ... The next file is "+nextFile.Filename+". Would you like me to send the file to you?", false, "Would you like me to send the file to you?", sessionAttributes); |
|
} |
|
sendSMS(sessionAttributes.userVars.userMobile, "Your PO: "+files[fileIndex].Filename+"\n"+e.data[0].LandingPageUrl); |
|
}); |
|
|
|
}; |
|
|
|
function sendSMS(mobile, msg) { |
|
console.log('alexaapphandler: sendSMS called'); |
|
|
|
var model = Arrow.getModel("appc.twilio/message"); |
|
model.create({to: mobile, body: msg }, function(err, data){ |
|
if(err) { |
|
console.log('alexaapphandler: sendSMS: SMS send failed, err = '+JSON.stringify(err)); |
|
} |
|
}); |
|
|
|
}; |
|
|
|
function shareFile(userVars, syncpointId, filepath, callback) { |
|
console.log('alexaapphandler: shareFile called'); |
|
console.log('alexaapphandler: shareFile called, syncpointId = '+syncpointId); |
|
console.log('alexaapphandler: shareFile called, filepath = '+filepath); |
|
|
|
var request = require("request"); |
|
|
|
var options = { |
|
method: 'POST', |
|
url: 'https://api.syncplicity.com/syncpoint/links.svc/', |
|
headers: { |
|
'Cache-Control': 'no-cache', |
|
'Content-Type': 'application/json', |
|
Accept: 'application/json', |
|
AppKey: userVars.syncClient_id, |
|
Authorization: 'Bearer '+userVars.syncAccess_token |
|
}, |
|
json: true, |
|
body: |
|
[ { SyncPointId: syncpointId, |
|
VirtualPath: filepath, |
|
ShareLinkPolicy: 3, |
|
PasswordProtectPolicy: 1, |
|
Users: [ { EmailAddress: '[email protected]' } ], |
|
Message: 'This was sent via Syncplicity API.' } ] |
|
}; |
|
|
|
request(options, function (error, response, body) { |
|
|
|
if (error) { |
|
console.log('alexaapphandler: share file error = '+JSON.stringify(error)); |
|
if(callback) {callback({success:false, data: 'Error sharing files'});} |
|
} else { |
|
console.log('alexaapphandler: share file sucess = '+JSON.stringify(body)); |
|
if(callback) {callback({success:true, data: body});} |
|
} |
|
|
|
}); |
|
}; |
|
|
|
function getFile(userVars, syncpointId, fileversionId, callback) { |
|
console.log('alexaapphandler: getFile called'); |
|
|
|
var request = require("request"); |
|
|
|
var options = { |
|
method: 'GET', |
|
url: 'https://data.syncplicity.com/retrieveFile.php?vToken='+syncpointId+'-'+fileversionId, |
|
headers: { |
|
'Cache-Control': 'no-cache', |
|
'Content-Type': 'application/json', |
|
Accept: 'application/json', |
|
AppKey: userVars.syncClient_id, |
|
Authorization: 'Bearer '+userVars.syncAccess_token |
|
} |
|
}; |
|
|
|
request(options, function (error, response, body) { |
|
|
|
console.log('alexaapphandler: get file body = '+JSON.stringify(body)); |
|
|
|
if (error) { |
|
console.log('alexaapphandler: get file error = '+error); |
|
if(callback) {callback({success:false, data: 'Error getting files'});} |
|
} else { |
|
if(body.HttpError) { |
|
console.log('alexaapphandler: get file error, body = '+body); |
|
if(callback) {callback({success:false, data: 'Error getting sync points'});} |
|
} else { |
|
if(callback) {callback({success:true, data: body});} |
|
} |
|
} |
|
|
|
}); |
|
}; |
|
|
|
function readFileName(req, resp, next, sessionAttributes) { |
|
console.log("alexaapphandler: readFileName called, sessionAttributes = "+JSON.stringify(sessionAttributes)); |
|
|
|
var files = sessionAttributes.files; |
|
var fileIndex = sessionAttributes.fileIndex; |
|
var currentFile = files[fileIndex]; |
|
|
|
sessionAttributes.sessionState = "readFile"; |
|
sendResponse(req, resp, next, currentFile.Filename+". Would you like me to send the file to your cell phone?", false, "Would you like me to send the file to your cell phone?", sessionAttributes); |
|
|
|
}; |
|
|
|
function readFoldersResponse(req, resp, next, folders, userVars, outputText) { |
|
console.log("alexaapphandler: readFoldersResponse called"); |
|
console.log("alexaapphandler: readFoldersResponse called, folders = "+JSON.stringify(folders)); |
|
console.log("alexaapphandler: readFoldersResponse called, userVars = "+JSON.stringify(userVars)); |
|
console.log("alexaapphandler: readFoldersResponse called, outputText = "+outputText); |
|
|
|
|
|
if(folders.length > 1) { |
|
outputText = ", "+folders.length+" folders were found. "; |
|
folders.forEach(function(item, index){ |
|
if(index === folders.length-1) { |
|
outputText += " , and "+item.name; |
|
} else { |
|
outputText += " , "+item.name; |
|
} |
|
}); |
|
sendResponse(req, resp, next, outputText, true, "", {}); |
|
|
|
} else if(folders.length === 1) { |
|
outputText = ", "+folders.length+" folder was found. "; |
|
outputText += folders[0].name; |
|
sendResponse(req, resp, next, outputText, true, "", {}); |
|
|
|
} else { |
|
sendResponse(req, resp, next, "No folders were found.", true, "", {}); |
|
} |
|
}; |
|
|
|
function login(userId, userVars, callback) { |
|
console.log('alexaapphandler: login() called'); |
|
|
|
getUser(userId, function(e){ |
|
if(e.success) { |
|
console.log("alexaapphandler: login - User found"); |
|
userVars.userFname = e.data.fname; |
|
userVars.userFound = true; |
|
userVars.userMobile = e.data.mobile; |
|
userVars.syncAppToken = e.data.syncAppToken; |
|
userVars.syncAppAuth = e.data.syncAppAuth; |
|
userVars.tfa = e.data.tfa; |
|
userVars.userLastPOTimeCheck = e.data.lastPOCheckTimeStamp; |
|
|
|
syncLogin(userVars, function(e){ |
|
if(e.success) { |
|
console.log("alexaapphandler: login - Syncplicity login success = "+e.data); |
|
userVars.userLoggedInToSync = true; |
|
if(callback) {callback({success: true, data:e.data});} |
|
} else { |
|
console.log("alexaapphandler: login - Syncplicity login error = "+e.data); |
|
if(callback) {callback({success: false, data:e.data});} |
|
} |
|
}); |
|
} else { |
|
console.log("alexaapphandler: login - User NOT found"); |
|
if(callback) {callback({success: false, data:e.data});} |
|
} |
|
}); |
|
}; |
|
|
|
function getUser(alexaUserid, callback) { |
|
console.log('alexaapphandler: getUser() called'); |
|
|
|
var model = Arrow.getModel("user"); |
|
model.query({alexaUserid: alexaUserid}, function(err, data) { |
|
console.log('alexaapphandler: getUser - err = '+JSON.stringify(err)); |
|
console.log('alexaapphandler: getUser - data = '+JSON.stringify(data)); |
|
|
|
if (err) { |
|
console.log('alexaapphandler: getUser - error querying user database, err = ' + JSON.stringify(err)); |
|
if(callback) {callback({success:false, msg:"error querying user database"});} |
|
} else { |
|
if (data.length != 1) { |
|
console.log('alexaapphandler: getUser - no matches found in user database'); |
|
if(callback) {callback({success:false, msg:"no matches found in user database"});} |
|
} else { |
|
if(callback) {callback({success:true, data: data[0]});} |
|
var dateNow = new Date(); |
|
data[0].lastPOCheckTimeStamp = dateNow; |
|
data[0].update(); |
|
} |
|
} |
|
}); |
|
}; |
|
|
|
function syncLogin(data, callback) { |
|
console.log('alexaapphandler:syncLogin called'); |
|
|
|
var request = require("request"); |
|
|
|
var options = { |
|
method: 'POST', |
|
url: 'https://api.syncplicity.com/oauth/token', |
|
headers: { |
|
'Cache-Control': 'no-cache', |
|
'Content-Type': 'application/x-www-form-urlencoded', |
|
// 'Sync-App-Token': data.syncAppToken+'0', // force fail login for debugging |
|
'Sync-App-Token': data.syncAppToken, |
|
Authorization: 'Basic '+ data.syncAppAuth}, |
|
form: { grant_type: 'client_credentials' } |
|
}; |
|
|
|
request(options, function (error, response, body) { |
|
// console.log('alexaapphandler:syncLogin body = '+body); |
|
var responseBody = JSON.parse(body); |
|
if (error) { |
|
console.log('alexaapphandler:syncLogin error = '+error); |
|
if(callback) {callback({success:false, data: 'Error reaching syncplicity server'});} |
|
} else { |
|
if(responseBody.HttpError) { |
|
console.log('alexaapphandler:syncLogin error, body = '+body); |
|
if(callback) {callback({success:false, data: 'Error logging into syncplicity server'});} |
|
} else { |
|
console.log('alexaapphandler:syncLogin success'); |
|
if(callback) {callback({success:true, data: body});} |
|
} |
|
} |
|
}); |
|
|
|
}; |
|
|
|
function getSyncPoints(userVars, callback) { |
|
console.log('alexaapphandler:getSyncPoints called'); |
|
|
|
var request = require("request"); |
|
|
|
var options = { |
|
method: 'GET', |
|
url: 'https://api.syncplicity.com/syncpoint/syncpoints.svc/', |
|
qs: { include: 'children', includeType: '1,2,3,4,5,6,7,8' }, |
|
headers: { |
|
'Cache-Control': 'no-cache', |
|
'Content-Type': 'application/json', |
|
Accept: 'application/json', |
|
AppKey: userVars.syncClient_id, |
|
Authorization: 'Bearer '+userVars.syncAccess_token |
|
} |
|
}; |
|
|
|
request(options, function (error, response, body) { |
|
var responseBody = JSON.parse(body); |
|
if (error) { |
|
console.log('alexaapphandler:syncPoint error = '+error); |
|
if(callback) {callback({success:false, data: 'Error getting sync points'});} |
|
} else { |
|
if(responseBody.HttpError) { |
|
console.log('alexaapphandler:syncPoint error, body = '+body); |
|
if(callback) {callback({success:false, data: 'Error getting sync points'});} |
|
} else { |
|
console.log('alexaapphandler:get sync point success, body = '+body); |
|
if(callback) {callback({success:true, data: body});} |
|
} |
|
} |
|
}); |
|
|
|
}; |
|
|
|
function readFilesResponse(req, resp, next, files, foldername, userVars) { |
|
console.log("alexaapphandler: readFilesResponse called"); |
|
|
|
console.log("alexaapphandler: readFilesResponse files = "+JSON.stringify(files)); |
|
|
|
var numFiles = files.length; |
|
|
|
var sessionAttributes = { |
|
sessionState: "readSyncFolder", |
|
files: files, |
|
fileIndex: 0, |
|
numFiles: numFiles, |
|
userVars: userVars |
|
}; |
|
|
|
var prefix = " "; |
|
if(userVars.newFilesOnly) { |
|
prefix = " new "; |
|
} |
|
|
|
if(numFiles == 0) { |
|
// sendResponse(req, resp, next, "No new purchase orders found.", true, "", sessionAttributes); |
|
sendResponse(req, resp, next, "No"+prefix+"purchase orders found.", true, "", sessionAttributes); |
|
} else if(numFiles == 1) { |
|
sendResponse(req, resp, next, numFiles+prefix+"purchase order found. Would you like me to read the filename to you?", false, "Would you like me to read the filename to you?", sessionAttributes); |
|
// sendResponse(req, resp, next, numFiles+" new purchase order found. Would you like me to read the filename to you?", false, "Would you like me to read the filename to you?", sessionAttributes); |
|
} else { |
|
// sendResponse(req, resp, next, numFiles+" new purchase orders found. Would you like me to read the filenames to you?", false, "Would you like me to read the filenames to you?", sessionAttributes); |
|
sendResponse(req, resp, next, numFiles+prefix+"purchase orders found. Would you like me to read the filenames to you?", false, "Would you like me to read the filenames to you?", sessionAttributes); |
|
} |
|
}; |
|
|
|
function getFiles(userVars, syncpointId, folderId, callback) { |
|
console.log('alexaapphandler: getFiles called'); |
|
|
|
var request = require("request"); |
|
|
|
var options = { |
|
method: 'GET', |
|
url: 'https://api.syncplicity.com/sync/folder_files.svc/'+folderId+'/folder/'+syncpointId+'/files', |
|
headers: { |
|
'Cache-Control': 'no-cache', |
|
'Content-Type': 'application/json', |
|
Accept: 'application/json', |
|
AppKey: userVars.syncClient_id, |
|
Authorization: 'Bearer '+userVars.syncAccess_token |
|
} |
|
}; |
|
|
|
request(options, function (error, response, body) { |
|
var responseBody = JSON.parse(body); |
|
if (error) { |
|
console.log('alexaapphandler: get files error = '+error); |
|
if(callback) {callback({success:false, data: 'Error getting files'});} |
|
} else { |
|
if(responseBody.HttpError) { |
|
console.log('alexaapphandler: get files error, body = '+body); |
|
if(callback) {callback({success:false, data: 'Error getting sync points'});} |
|
} else { |
|
console.log('alexaapphandler: get files success, body = '+JSON.stringify(JSON.parse(body))); |
|
var timeSince = '0'; // get all POs |
|
if(userVars.newFilesOnly) { |
|
timeSince = userVars.userLastPOTimeCheck; // get new POs |
|
} |
|
var files = getNewerFiles(JSON.parse(body), timeSince); |
|
if(callback) {callback({success:true, data: files});} |
|
} |
|
} |
|
}); |
|
}; |
|
|
|
function getNewerFiles(allFiles, timeSince) { |
|
// console.log('alexaapphandler: getNewerFiles called, allFiles = '+JSON.stringify(allFiles)); |
|
console.log('alexaapphandler: getNewerFiles called'); |
|
console.log('alexaapphandler: getNewerFiles called, timeSince = '+timeSince); |
|
|
|
var newerFiles = []; |
|
allFiles.forEach(function(file){ |
|
// console.log('alexaapphandler: getNewerFiles - file.CreationTimeUtc = '+file.CreationTimeUtc); |
|
console.log('alexaapphandler: getNewerFiles - file.CreationTimeUtc = '+file.CreationTimeUtc); |
|
console.log('alexaapphandler: getNewerFiles - timeSince = '+timeSince); |
|
var lastCheck = new Date(timeSince); |
|
var fileDate = new Date(file.CreationTimeUtc) |
|
if(fileDate > lastCheck) { |
|
// console.log('alexaapphandler: getNewerFiles - file.CreationTimeUtc <= timeSince is true'); |
|
newerFiles.push(file); |
|
} |
|
}); |
|
// console.log('alexaapphandler: getNewerFiles - newerFiles = '+newerFiles); |
|
return JSON.stringify(newerFiles); |
|
}; |
|
|
|
function getMatchingFolder(folders, foldername) { |
|
console.log('alexaapphandler: getMatchingFolder called'); |
|
|
|
console.log('folders = '+JSON.stringify(folders)); |
|
console.log('foldername = '+foldername); |
|
|
|
var matchingFolder = {}; |
|
folders.forEach(function(folder){ |
|
if(folder.Name.toLowerCase() === foldername.toLowerCase()) { |
|
matchingFolder = folder; |
|
} |
|
}); |
|
console.log('matchingFolder = '+JSON.stringify(matchingFolder)); |
|
return matchingFolder; |
|
}; |
|
|
|
|
|
var getFoldersHandler = function(req, resp, next, userVars) { |
|
console.log('alexaapphandler: getFoldersHandler called'); |
|
|
|
login(req.body.session.user.userId, userVars, function(e){ |
|
console.log("alexaapphandler: getFoldersHandler: login, e = "+JSON.stringify(e)); |
|
|
|
if(e.success) { |
|
|
|
var user = JSON.parse(e.data); |
|
console.log("alexaapphandler: getFoldersHandler: login, user = "+JSON.stringify(user)); |
|
userVars.syncClient_id=user.client_id; |
|
userVars.syncAccess_token=user.access_token; |
|
|
|
getSyncPoints(userVars, function(f){ |
|
var folders = JSON.parse(f.data); |
|
console.log("alexaapphandler: getFoldersHandler - getSyncPoints folders length = "+folders.length); |
|
var outputText = "Hello "+userVars.userFname; |
|
if(folders.length === 0) { |
|
outputText += ", you have no Syncplicity folders."; |
|
sendResponse(req, resp, next, outputText, true, "", {}); |
|
} else { |
|
var repromptText = "Would you like me to read your folder names to you?"; |
|
var sessionAttributes = { |
|
sessionState: "readSyncFolders", |
|
folders: reduceSyncpointReply(folders), |
|
folderIndex: 0 |
|
}; |
|
if(folders.length === 1) { |
|
outputText += ", you have one Syncplicity folder. Would you like me to read the folder name to you?"; |
|
} else { |
|
outputText += ", you have "+folders.length+" Syncplicity folders. Would you like me to read your folder names to you?"; |
|
} |
|
sendResponse(req, resp, next, outputText, false, repromptText, sessionAttributes); |
|
} |
|
}); |
|
} else { |
|
sendResponse(req, resp, next, "Problem logging in. Please try again or contact your administrator.", true, {}); |
|
} |
|
}); |
|
}; |
|
|
|
function reduceSyncpointReply(syncpointReply) { |
|
console.log('alexaapphandler:reduceSyncpointReply called'); |
|
|
|
var folderArray = []; |
|
syncpointReply.forEach(function(folder){ |
|
folderArray.push({ |
|
name: folder.Name |
|
}); |
|
}); |
|
return folderArray; |
|
}; |
|
|
|
var getFolderHandler = function(req, resp, next, userVars, newFilesOnly){ |
|
console.log('alexaapphandler: getFolderHandler called'); |
|
|
|
login(req.body.session.user.userId, userVars, function(e){ |
|
console.log("alexaapphandler: getFolderHandler: login, e = "+JSON.stringify(e)); |
|
|
|
var outputText; |
|
if(e.success) { |
|
|
|
var user = JSON.parse(e.data); |
|
console.log("alexaapphandler: getFolderHandler: login, user = "+JSON.stringify(user)); |
|
userVars.syncClient_id=user.client_id; |
|
userVars.syncAccess_token=user.access_token; |
|
userVars.newFilesOnly=newFilesOnly; |
|
|
|
getSyncPoints(userVars, function(f){ |
|
if(f.success) { |
|
var folders = JSON.parse(f.data); |
|
console.log("alexaapphandler: getFolderHandler - getSyncPoints folders length = "+folders.length); |
|
var matchingFolder = getMatchingFolder(folders, POFolderName); |
|
if(!isEmpty(matchingFolder)) { |
|
getFiles(userVars, matchingFolder.RootFolderId, matchingFolder.Id, function(g){ |
|
if(g.success) { |
|
readFilesResponse(req, resp, next, JSON.parse(g.data), POFolderName, userVars); |
|
} else { |
|
outputText = "Hello "+userVars.userFname+", I had a problem access the Syncplicity server, please try again"; |
|
sendResponse(req, resp, next, outputText, true, "", {}); |
|
} |
|
}); |
|
} else { |
|
outputText = "Hello "+userVars.userFname+", "+POFolderName+" folder not found, please try again"; |
|
sendResponse(req, resp, next, outputText, true, "", {}); |
|
} |
|
} else { |
|
outputText = "Hello "+userVars.userFname+", I had a problem access the Syncplicity server, please try again"; |
|
sendResponse(req, resp, next, outputText, true, "", {}); |
|
} |
|
}); |
|
} else { |
|
sendResponse(req, resp, next, "Problem logging in. Please try again or contact your administrator.", true, {}); |
|
} |
|
}); |
|
}; |
|
|
|
function isEmpty(obj) { |
|
console.log('alexaapphandler: isEmpty called'); |
|
|
|
for(var prop in obj) { |
|
if(obj.hasOwnProperty(prop)) |
|
return false; |
|
} |
|
|
|
return JSON.stringify(obj) === JSON.stringify({}); |
|
}; |
|
|
|
function resetUserVars(userVars) { |
|
console.log('alexaapphandler: resetUserVars called'); |
|
|
|
userVars.userFound = false; |
|
userVars.userLoggedInToSync = false; |
|
userVars.userFname=null; |
|
userVars.userLastPOTimeCheck=null; |
|
userVars.userMobile=null; |
|
userVars.newFilesOnly=true; |
|
userVars.syncAppToken=null; |
|
userVars.syncAppAuth=null; |
|
userVars.syncClient_id=null; |
|
userVars.syncAccess_token=null; |
|
}; |
|
|
|
function sendResponse(req, resp, next, outputText, closeSession, repromptText, sessionAttributes) { |
|
console.log("alexaapphandler: sendResponse called"); |
|
console.log("alexaapphandler: sessionAttributes = "+JSON.stringify(sessionAttributes)); |
|
|
|
resp.response.status(200); |
|
resp.send({ |
|
"sessionAttributes": sessionAttributes, |
|
"version": "1.0", |
|
"response": { |
|
"shouldEndSession": closeSession, |
|
"outputSpeech": { |
|
"type": "SSML", |
|
ssml: "<speak>"+outputText+"</speak>" |
|
}, |
|
reprompt: { |
|
"outputSpeech": { |
|
type: "SSML", |
|
ssml: "<speak>"+repromptText+"</speak>" |
|
} |
|
}, |
|
"card": { |
|
type: "Standard", |
|
title: appName, |
|
text: outputText, |
|
"image": { |
|
"smallImageUrl": "https://s3.amazonaws.com/lbsyncalexaassets/sync_icon_512.png", |
|
"largeImageUrl": "https://s3.amazonaws.com/lbsyncalexaassets/sync_icon_512.png" |
|
} |
|
} |
|
} |
|
}); |
|
next(); |
|
}; |
|
|
|
function verifyAlexaRequest(req, callback) { |
|
console.log('alexaapphandler: verifyAlexaRequest called'); |
|
|
|
var cert_url = req.headers['signaturecertchainurl']; |
|
var signature = req.headers['signature']; |
|
var requestRawBody = JSON.stringify(req.body); |
|
console.log('\alexaapphandler: header signaturecertchainurl = '+cert_url); |
|
console.log('\alexaapphandler: header signature = '+signature); |
|
console.log('\alexaapphandler: requestRawBody = '+requestRawBody); |
|
|
|
if (cert_url && signature) { |
|
verifier(cert_url, signature, requestRawBody, function(error) { |
|
if (!error) { |
|
if(callback) {callback({success: true});} |
|
} else { |
|
console.log('alexaapphandler: API verify request error = ' + error); |
|
if(callback) {callback({success: false, data:error});} |
|
} |
|
}); |
|
} else { |
|
console.log('alexaapphandler: verify request error. Proper headers not found'); |
|
if(callback) {callback({success: false, data:'verify request error. Proper headers not found'});} |
|
} |
|
}; |
|
|
|
function generateLaunchText(userVars) { |
|
console.log('alexaapphandler: generateLaunchText called'); |
|
|
|
if(!userVars.userFound) { |
|
return launchTxt + ", " +userNotFoundTxt; |
|
} else if(!userVars.userLoggedInToSync) { |
|
return "Hello "+userVars.userFname+", "+launchTxt+", "+ failedLoginTxt; |
|
} else { |
|
return "Hello "+userVars.userFname+", "+launchTxt; |
|
} |
|
}; |
|
|
|
function generateHelpText(userVars) { |
|
console.log('alexaapphandler: generateHelpText called'); |
|
|
|
if(!userVars.userFound) { |
|
return helpTxt + ", " +userNotFoundTxt; |
|
} else if(!userVars.userLoggedInToSync) { |
|
return "Hello "+userVars.userFname+", "+helpTxt+", "+ failedLoginTxt; |
|
} else { |
|
return "Hello "+userVars.userFname+", "+helpTxt; |
|
} |
|
}; |