120 lines
3.3 KiB
JavaScript
120 lines
3.3 KiB
JavaScript
require('isomorphic-fetch');
|
|
const config = require('../config');
|
|
var request = require("request");
|
|
var token = require('./token');
|
|
|
|
|
|
var getBuildStatus = function(skillID){
|
|
try{
|
|
fetch(`https://api.amazonalexa.com/v0/skills/${skillID}/interactionModel/locales/en-US/status`, {
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: TOKEN
|
|
},
|
|
}).then(l=>l.text()).then(result=>{
|
|
return result;
|
|
});
|
|
}catch(e){
|
|
console.log("err : " + e);
|
|
}
|
|
}
|
|
|
|
var refreshToken = function(db){
|
|
return new Promise((resolve, reject)=>{
|
|
var options = { method: 'POST',
|
|
url: 'https://api.amazon.com/auth/o2/token',
|
|
headers:
|
|
{ 'cache-control': 'no-cache',
|
|
'content-type': 'application/x-www-form-urlencoded' },
|
|
form:
|
|
{ grant_type: 'refresh_token',
|
|
refresh_token: config.REFRESH_TOKEN,
|
|
client_id: config.CLIENT_ID,
|
|
client_secret: config.CLIENT_SECRET } };
|
|
|
|
request(options, function (error, response, body) {
|
|
if (error) reject(error);
|
|
parsedResponse = JSON.parse(body);
|
|
if (parsedResponse.refresh_token){
|
|
try{
|
|
token.updateTokens(parsedResponse.refresh_token, parsedResponse.access_token, parsedResponse.expires_in,db);
|
|
resolve();
|
|
}catch(e){
|
|
reject(e);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
var generateInteractionModel = function(skill){
|
|
let result = {};
|
|
let allIntents = [];
|
|
let defaultIntents = [{
|
|
name: "AMAZON.CancelIntent",
|
|
samples: []
|
|
},
|
|
{
|
|
name: "AMAZON.HelpIntent",
|
|
samples: []
|
|
},
|
|
{
|
|
name: "AMAZON.StopIntent",
|
|
samples: []
|
|
}];
|
|
|
|
/*
|
|
defaultIntents.map(intent=>{
|
|
allIntents.push(intent);
|
|
});
|
|
*/
|
|
|
|
skill.intents.map(intent=>{
|
|
allIntents.push({name: intent.intentName, samples: intent.questions});
|
|
});
|
|
|
|
result.interactionModel = {};
|
|
|
|
result.interactionModel.languageModel = {
|
|
invocationName: skill.invocationName,
|
|
intents: allIntents
|
|
};
|
|
|
|
return JSON.stringify(result);
|
|
}
|
|
|
|
var uploadSkill = function(skill){
|
|
return fetch(`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`, {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: config.TOKEN
|
|
},
|
|
body: generateInteractionModel(skill)
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
updateSkill: function(skill, db){
|
|
console.log("update skill function");
|
|
|
|
return new Promise((resolve,reject)=>{
|
|
if (new Date() / 1000 > config.TOKEN_EXPIRES_IN){
|
|
refreshToken(db).then(()=>{
|
|
uploadSkill(skill).then(response=>{
|
|
resolve(response.status);
|
|
});
|
|
}).catch(e=>{
|
|
reject(e);
|
|
});
|
|
}else{
|
|
uploadSkill(skill).then(response=>{
|
|
resolve(response.status);
|
|
}).catch(e=>{
|
|
reject(e);
|
|
})
|
|
}
|
|
});
|
|
|
|
|
|
}
|
|
}; |