From 94d2883c016b8155c2fb00f9767e17a88b9fb401 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Sun, 3 Dec 2017 17:38:58 +0100 Subject: [PATCH] send update to amazon --- backend/express.js | 25 ++++++++- backend/helpers/amazon.js | 92 ++++++++++++++++++-------------- backend/package.json | 3 +- web/src/App.js | 18 ++++--- web/src/components/IntentItem.js | 2 +- web/src/lib/api.js | 1 - 6 files changed, 89 insertions(+), 52 deletions(-) diff --git a/backend/express.js b/backend/express.js index dbf6b6f..b09669e 100644 --- a/backend/express.js +++ b/backend/express.js @@ -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 diff --git a/backend/helpers/amazon.js b/backend/helpers/amazon.js index 4466cf5..2ebcf9f 100644 --- a/backend/helpers/amazon.js +++ b/backend/helpers/amazon.js @@ -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); + } } + }; \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index de51119..a496860 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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 (http://mattkruse.com/)", "license": "MIT" diff --git a/web/src/App.js b/web/src/App.js index 6928119..e2b5455 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -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"); diff --git a/web/src/components/IntentItem.js b/web/src/components/IntentItem.js index a4fb43b..435da7a 100644 --- a/web/src/components/IntentItem.js +++ b/web/src/components/IntentItem.js @@ -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}

diff --git a/web/src/lib/api.js b/web/src/lib/api.js index 06964bb..229cbf3 100644 --- a/web/src/lib/api.js +++ b/web/src/lib/api.js @@ -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, {