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'); var amazonHelper = require('./helpers/amazon');
require('isomorphic-fetch');
var express = require('express'); var express = require('express');
var alexa = require('alexa-app'); var alexa = require('alexa-app');
@@ -7,6 +9,9 @@ var bodyParser = require('body-parser');
const dbURL = 'mongodb://localhost:27017/tellall'; const dbURL = 'mongodb://localhost:27017/tellall';
const PORT = 5000; 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; var MongoClient = require ('mongodb').MongoClient;
@@ -58,10 +63,26 @@ router.post ('/updateSkill/:id', async (req, res, next) => {
let skill = req.body; let skill = req.body;
delete skill._id; delete skill._id;
if (id !== '-1'){ 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)=>{ let result = db.collection('skill_list').update({_id: ObjectID(id)}, skill,{upsert:true}, (err, result)=>{
if (err) throw(err); 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{ }else{
//no new skills for now //no new skills for now

View File

@@ -1,52 +1,62 @@
require('isomorphic-fetch');
var ObjectID = require ('mongodb').ObjectID; var ObjectID = require ('mongodb').ObjectID;
var generateInteractionModel = function (skill) {
var getBuildStatus = function(skillID){
try{ try{
let result = {}; fetch(`https://api.amazonalexa.com/v0/skills/${skillID}/interactionModel/locales/en-US/status`, {
let allIntents = []; method: 'GET',
let defaultIntents = [{ headers: {
name: "AMAZON.CancelIntent", Authorization: TOKEN
samples: [] },
}, }).then(l=>l.text()).then(result=>{
{ return result;
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, slots: []});
});
result.languageModel = {
invocationName: skill.invocationName,
intents: allIntents
};
return JSON.stringify(result);
}catch(e){ }catch(e){
console.log("error generate : " + e); console.log("err : " + e);
} }
} }
module.exports = { module.exports = {
sendUpdateToAmazon: function (id, db){ generateInteractionModel: function(skill){
db.collection ('skill_list').find({_id: ObjectID(id)}).toArray((err,result)=>{ try{
if (err){ let result = {};
console.log("Error finding skill"); let allIntents = [];
}else{ let defaultIntents = [{
//generateInteractionModel name: "AMAZON.CancelIntent",
//POST to amazon samples: []
let generatedInteractionModel = generateInteractionModel(result[0]); },
{
} 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": "", "description": "",
"main": "test.js", "main": "test.js",
"dependencies": { "dependencies": {
"alexa-app": "4.2.0",
"body-parser": "^1.13.1", "body-parser": "^1.13.1",
"ejs": "^2.3.1", "ejs": "^2.3.1",
"express": "^4.13.0", "express": "^4.13.0",
"alexa-app": "4.2.0" "isomorphic-fetch": "^2.2.1"
}, },
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)", "author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
"license": "MIT" "license": "MIT"

View File

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

View File

@@ -22,7 +22,7 @@ class IntentItem extends Component {
onClick={()=>{this.state.onClick(this.state.intent,this.state.index)}} onClick={()=>{this.state.onClick(this.state.intent,this.state.index)}}
flat flat
tooltipDelay={INTENT_TITLE_TOOLTIP_DELAY} 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} {this.state.index+1}. {buttonTitle}
</Button> </Button>
<br></br> <br></br>

View File

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