2018-01-08 21:50:24 +01:00
require ( 'isomorphic-fetch' ) ;
2018-01-12 01:56:17 +01:00
const config = require ( '../config/config' ) ;
2018-01-08 21:50:24 +01:00
var request = require ( 'request' ) ;
var databaseHelper = require ( './database' ) ;
2017-12-02 22:48:45 +01:00
2018-01-08 21:50:24 +01:00
var getBuildStatus = function ( skillID ) {
2018-01-12 01:56:17 +01:00
fetch (
2018-01-08 21:50:24 +01:00
` https://api.amazonalexa.com/v0/skills/ ${ skillID } /interactionModel/locales/en-US/status ` ,
{
method : 'GET' ,
headers : {
Authorization : config . TOKEN ,
} ,
}
)
. then ( l => l . text ( ) )
. then ( result => {
return result ;
} ) ;
} ;
2017-12-02 22:48:45 +01:00
2018-01-08 21:50:24 +01:00
var refreshTokens = function ( ) {
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 ,
} ,
} ;
2017-12-03 00:15:11 +01:00
2018-01-08 21:50:24 +01:00
request ( options , function ( error , response , body ) {
if ( error ) reject ( error ) ;
parsedResponse = JSON . parse ( body ) ;
if ( parsedResponse . refresh _token )
return databaseHelper . updateTokens (
parsedResponse . refresh _token ,
parsedResponse . access _token ,
parsedResponse . expires _in
) ;
2018-01-11 14:05:44 +00:00
console . log ( 'Token refresh failed' ) ;
console . log ( body ) ;
reject ( body ) ;
2018-01-05 00:51:49 +01:00
} ) ;
2018-01-08 21:50:24 +01:00
} ) ;
} ;
2018-01-05 00:51:49 +01:00
2018-01-08 21:50:24 +01:00
var generateInteractionModel = function ( skill ) {
let result = { } ;
let allIntents = [ ] ;
let defaultIntents = [
{
name : 'AMAZON.CancelIntent' ,
samples : [ ] ,
} ,
{
name : 'AMAZON.HelpIntent' ,
samples : [ ] ,
} ,
{
name : 'AMAZON.StopIntent' ,
samples : [ ] ,
} ,
] ;
2018-01-05 00:51:49 +01:00
2018-01-08 21:50:24 +01:00
/ *
2018-01-05 00:51:49 +01:00
defaultIntents . map ( intent => {
allIntents . push ( intent ) ;
} ) ;
* /
2018-01-08 21:50:24 +01:00
skill . intents . map ( intent => {
allIntents . push ( { name : intent . intentName , samples : intent . questions } ) ;
} ) ;
2018-01-05 00:51:49 +01:00
2018-01-11 14:05:44 +00:00
2018-01-11 04:24:16 +01:00
//Special Email Intents :
allIntents . push ( {
name : 'EmailIntentLaunch' ,
slots : [ ] ,
samples : [
'I want to send a message' ,
'I would like to send a message' ,
'I would like to leave a message' ,
'Leave a message'
]
} ) ;
2018-01-11 14:05:44 +00:00
2018-01-11 04:24:16 +01:00
allIntents . push ( {
name : 'EmailIntent' ,
slots : [
{
2018-01-11 14:05:44 +00:00
'name' : 'Name' ,
'type' : 'AMAZON.US_FIRST_NAME'
2018-01-11 04:24:16 +01:00
} ,
{
2018-01-11 14:05:44 +00:00
'name' : 'Email' ,
'type' : 'AMAZON.LITERAL'
2018-01-11 04:24:16 +01:00
} ,
{
2018-01-11 14:05:44 +00:00
'name' : 'Message' ,
'type' : 'AMAZON.LITERAL'
2018-01-11 04:24:16 +01:00
} ,
{
2018-01-11 14:05:44 +00:00
'name' : 'Data' ,
'type' : 'AMAZON.LITERAL'
2018-01-11 04:24:16 +01:00
}
] ,
2018-01-11 14:05:44 +00:00
2018-01-11 04:24:16 +01:00
samples : [
'My name is {Name}' ,
'I am {Name}' ,
2018-01-11 14:05:44 +00:00
'{exampleww at wwdwdw|Data}' ,
'My email is {example at efefegedd|Email}' ,
'Send replay to {example at abcdefg|Email}' ,
'My message is {The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.|Message}'
2018-01-11 04:24:16 +01:00
]
2018-01-11 14:05:44 +00:00
2018-01-11 04:24:16 +01:00
} ) ;
2018-01-11 14:05:44 +00:00
2018-01-08 21:50:24 +01:00
result . interactionModel = { } ;
2018-01-05 00:51:49 +01:00
2018-01-08 21:50:24 +01:00
result . interactionModel . languageModel = {
invocationName : skill . invocationName ,
intents : allIntents ,
} ;
2018-01-05 00:51:49 +01:00
2018-01-08 21:50:24 +01:00
return JSON . stringify ( result ) ;
} ;
2018-01-05 00:51:49 +01:00
2018-01-08 21:50:24 +01:00
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 ) ,
}
) ;
} ;
2018-01-05 00:51:49 +01:00
module . exports = {
2018-01-08 21:50:24 +01:00
updateSkill : function ( skill ) {
return new Promise ( ( resolve , reject ) => {
if ( new Date ( ) / 1000 > config . TOKEN _EXPIRES _IN ) {
refreshTokens ( )
. then ( ( ) => {
uploadSkill ( skill ) . then ( response => {
resolve ( response . status ) ;
} ) ;
} )
. catch ( e => {
reject ( e ) ;
} ) ;
} else {
uploadSkill ( skill )
. then ( response => {
resolve ( response . status ) ;
} )
. catch ( e => {
reject ( e ) ;
} ) ;
}
} ) ;
} ,
} ;