send update to amazon

This commit is contained in:
GotPPay
2017-12-03 17:38:58 +01:00
parent 74ee9de93f
commit 94d2883c01
6 changed files with 89 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
var amazonHelper = require('./helpers/amazon');
require('isomorphic-fetch');
var express = require('express');
var alexa = require('alexa-app');
@@ -7,6 +9,9 @@ var bodyParser = require('body-parser');
const dbURL = 'mongodb://localhost:27017/tellall';
const PORT = 5000;
const TOKEN = 'Atza|IwEBIMv0spn-eZhjP8-R2Jjkb4VUi-EY4V3MX-wlJyr2P6YBUmIChl7VKgRberEdQ-Wolp53SqfwHbxlSo4-rdUgFYFAxImB622boeqUBBCtwybP0OTBJBT1CVm_FBr48Li5LkwR9DxPciCnc052ddohpjGuGZxsCqIJo2c-7LPEhH0Lx63VOFgfPIfBsrVqjDbPNrr5ApPUijKYZWurktb6ytTks8eFUSTyv3FDbE5HEng0qpE4mgSjdgBkEc8BgUfpm2QGvctINH9SioUJOJonxgTPYhbD4BEd-jdQHsltKFzkb3-Rm3lFhEu3_CQFxCeQ6yGe1the_qID3vnPfpSY6hyblgF5L_5d7bE7BWg8fm6rSwXv67L2sMwBOji6cuR0wVVvXfYxmgGIMGkVQrq-SPSdtCpx2BvBz2SqqN6a_98svP8ukvhwc_oJsN6VwEmTDFgutNf-XuGkuVii-k9-DncwuwD00LvJG1FhBvbvSyuv-a3LAJSqTmroemTDG0xzLn7ULFY5p-93sM0_ZNGFeW-lL_r1ldqM_5lFRKDta1Tkg7lT9-rHftgnpGs4zv7vGLIPzHpNiXjsKyCk-wMQrihhWlR15kiz7oKDeTf6wCIETg';
const USER_ID = 'amzn1.ask.account.AGE34MG3VIQ75D4Q5CXFK25GUOXHZ2MIVSII3QUCOT3CP44IPE25PLD3DWW7HLWO2KVC7PC7VUXSZZEFPVHJ6PTDYVYYESJE35CE6VEXCE3BI3AK24TGO4CJXFF3SZ7IA4QVJRZC6EN4MUF2WUP4IB4CGDLRZZMYQENOFNBYWFXZHMZ5PA6S6ERK7NGEPUSDHXWKH26UGMIATMI';
var MongoClient = require ('mongodb').MongoClient;
@@ -58,10 +63,26 @@ router.post ('/updateSkill/:id', async (req, res, next) => {
let skill = req.body;
delete skill._id;
if (id !== '-1'){
let completeResult = {databaseUpdate:false, amazonUpdate:false, amazonMessage:''};
let result = db.collection('skill_list').update({_id: ObjectID(id)}, skill,{upsert:true}, (err, result)=>{
if (err) throw(err);
amazonHelper.sendUpdateToAmazon(id, db);
res.json(result);
completeResult.databaseUpdate = (JSON.parse(result).nModified===1);
let generatedInteractionModel = amazonHelper.generateInteractionModel(skill);
fetch(`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`, {
method: 'POST',
headers: {
Authorization: TOKEN
},
body: generatedInteractionModel
}).then(l=>l.text()).then(result=>{
completeResult.amazonUpdate = (JSON.stringify(result)===JSON.stringify({}));
completeResult.amazonMessage = JSON.stringify(result);
res.json(completeResult);
});
});
}else{
//no new skills for now

View File

@@ -1,52 +1,62 @@
require('isomorphic-fetch');
var ObjectID = require ('mongodb').ObjectID;
var generateInteractionModel = function (skill) {
var getBuildStatus = function(skillID){
try{
let result = {};
let allIntents = [];
let defaultIntents = [{
name: "AMAZON.CancelIntent",
samples: []
},
{
name: "AMAZON.HelpIntent",
samples: []
},
{
name: "AMAZON.StopIntent",
samples: []
}];
defaultIntents.map(intent=>{
allIntents.push(intent);
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;
});
skill.intents.map(intent=>{
allIntents.push({name: intent.intentName, samples: intent.questions, slots: []});
});
result.languageModel = {
invocationName: skill.invocationName,
intents: allIntents
};
return JSON.stringify(result);
}catch(e){
console.log("error generate : " + e);
console.log("err : " + e);
}
}
module.exports = {
sendUpdateToAmazon: function (id, db){
db.collection ('skill_list').find({_id: ObjectID(id)}).toArray((err,result)=>{
if (err){
console.log("Error finding skill");
}else{
//generateInteractionModel
//POST to amazon
let generatedInteractionModel = generateInteractionModel(result[0]);
}
});
generateInteractionModel: function(skill){
try{
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);
}catch(e){
console.log("error generate : " + e);
}
}
};

View File

@@ -4,10 +4,11 @@
"description": "",
"main": "test.js",
"dependencies": {
"alexa-app": "4.2.0",
"body-parser": "^1.13.1",
"ejs": "^2.3.1",
"express": "^4.13.0",
"alexa-app": "4.2.0"
"isomorphic-fetch": "^2.2.1"
},
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
"license": "MIT"

View File

@@ -99,8 +99,10 @@ class App extends Component {
this.setState({invocationName: name, invocationAnswer: answer});
try{
updateSkill(this.createSkill(this.state.allIntents,name,answer)).then(l=>l.text()).then(result=>{
if (JSON.parse(result).nModified!==1)
alert("Error");
let jResult = JSON.parse(result);
if (!(jResult.databaseUpdate && jResult.amazonUpdate)){
alert('Database update : ' + jResult.databaseUpdate + '\r\nAmazon update : ' + jResult.amazonUpdate + '\r\nMessage : ' + jResult.amazonMessage);
}
});
}catch(e){
alert("exception");
@@ -117,8 +119,10 @@ class App extends Component {
newAllIntents.splice(id,1);
this.setState({allIntents: newAllIntents, selectedIntent: {intentName:'', questions:[''],answer:''}});
updateSkill(this.createSkill(newAllIntents)).then(l=>l.text()).then(result=>{
if (JSON.parse(result).nModified!==1)
alert("Error");
let jResult = JSON.parse(result);
if (!(jResult.databaseUpdate && jResult.amazonUpdate)){
alert('Database update : ' + jResult.databaseUpdate + '\r\nAmazon update : ' + jResult.amazonUpdate + '\r\nMessage : ' + jResult.amazonMessage);
}
});
}catch(e){
alert("exception");
@@ -139,8 +143,10 @@ class App extends Component {
}
try{
updateSkill(this.createSkill(newAllIntents)).then(l=>l.text()).then(result=>{
if (JSON.parse(result).nModified!==1)
alert("Error");
let jResult = JSON.parse(result);
if (!(jResult.databaseUpdate && jResult.amazonUpdate)){
alert('Database update : ' + jResult.databaseUpdate + '\r\nAmazon update : ' + jResult.amazonUpdate + '\r\nMessage : ' + jResult.amazonMessage);
}
});
}catch(e){
alert("exception");

View File

@@ -22,7 +22,7 @@ class IntentItem extends Component {
onClick={()=>{this.state.onClick(this.state.intent,this.state.index)}}
flat
tooltipDelay={INTENT_TITLE_TOOLTIP_DELAY}
tooltipLabel={this.state.intent.questions[0].length>INTENT_TITLE_MAX_LENGTH ? this.state.intent.questions[0] : ''}>
tooltipLabel={this.state.intent.intentName.length>INTENT_TITLE_MAX_LENGTH ? this.state.intent.questions[0] : ''}>
{this.state.index+1}. {buttonTitle}
</Button>
<br></br>

View File

@@ -17,7 +17,6 @@ export const deleteSkill = (id)=>{
}
export const updateSkill = (skill)=>{
console.log(skill);
let id = (skill._id) ? skill._id : -1;
let url = `http://${BASE_URL}/updateSkill/${id}`
return fetch(url, {