Files
old-tellall/backend/helpers/amazon.js

283 lines
6.5 KiB
JavaScript
Raw Normal View History

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');
2018-01-08 21:50:24 +01:00
var getBuildStatus = function (skillID) {
2018-01-12 01:56:17 +01:00
fetch (
2018-01-13 14:57:41 +01:00
`https://api.amazonalexa.com/v0/skills/${skillID}/interactionModel/locales/en-US/status`,
{
method: 'GET',
headers: {
Authorization: config.TOKEN,
},
}
).then (result => {
return result.text ();
});
2018-01-08 21:50:24 +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) {
2018-01-16 01:44:33 +01:00
if (error) {
reject (error);
2018-01-19 07:43:10 +01:00
} else {
2018-01-16 01:44:33 +01:00
parsedResponse = JSON.parse (body);
2018-01-19 07:43:10 +01:00
if (parsedResponse.refresh_token) {
databaseHelper
.updateTokens (
parsedResponse.refresh_token,
parsedResponse.access_token,
parsedResponse.expires_in
)
.then (() => {
resolve ();
})
.catch (e => {
reject (e);
});
} else {
2018-01-16 01:44:33 +01:00
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 = [];
2018-01-05 00:51:49 +01:00
2018-01-08 21:50:24 +01:00
skill.intents.map (intent => {
2018-01-22 22:34:13 +01:00
allIntents.push ({name: intent.intentName, samples: intent.questions});
2018-01-08 21:50:24 +01:00
});
2018-01-05 00:51:49 +01:00
2018-01-19 07:43:10 +01:00
//Special intent for sending message (Dialog)
2018-01-23 00:20:50 +01:00
2018-01-13 14:57:41 +01:00
allIntents.push ({
2018-01-19 07:43:10 +01:00
name: 'SendMessageIntent',
2018-01-11 04:24:16 +01:00
samples: [
'I would like to send a message',
2018-01-19 07:43:10 +01:00
'I want to send a message',
'Send message',
2018-01-13 14:57:41 +01:00
],
slots: [
2018-01-11 04:24:16 +01:00
{
2018-01-13 14:57:41 +01:00
name: 'Name',
type: 'AMAZON.US_FIRST_NAME',
2018-01-19 07:43:10 +01:00
samples: ['My name is {Name}', 'I am {Name}', '{Name}'],
2018-01-11 04:24:16 +01:00
},
{
2018-01-13 14:57:41 +01:00
name: 'Email',
2018-01-19 07:43:10 +01:00
type: 'EmailSlot',
samples: ['My email is {Email}', '{Email}'],
2018-01-11 04:24:16 +01:00
},
{
2018-01-13 14:57:41 +01:00
name: 'Message',
2018-01-19 07:43:10 +01:00
type: 'MessageSlot',
samples: ['{Message}'],
2018-01-13 14:57:41 +01:00
},
2018-01-11 04:24:16 +01:00
],
});
2018-01-22 22:34:13 +01:00
2018-01-19 07:43:10 +01:00
let customSlotTypes = [
{
name: 'EmailSlot',
values: [
{
id: null,
name: {
value: 'bla@bla.bla',
synonyms: [],
},
},
{
id: null,
name: {
value: 'bla.bla@bla.bla.bla',
synonyms: [],
},
},
{
id: null,
name: {
value: 'bla_bla@bla.bla',
synonyms: [],
},
},
],
},
{
name: 'MessageSlot',
values: [
{
id: null,
name: {
value: 'Quick brown fox jumps over lazy dog',
synonyms: [],
},
},
{
id: null,
name: {
value: 'Quick brown fox jumps over lazy dog. Quick brown fox jumps over lazy dog.',
synonyms: [],
},
},
{
id: null,
name: {
value: 'Quick brown fox jumps over lazy dog. Quick brown fox jumps over lazy dog. Quick brown fox jumps over lazy dog.',
synonyms: [],
},
},
],
},
];
2018-01-23 00:20:50 +01:00
2018-01-19 07:43:10 +01:00
let dialogPrompts = [
{
id: 'Elicit.Intent-SendMessageIntent.IntentSlot-Name',
variations: [
{
type: 'PlainText',
value: 'What is your name ?',
},
{
type: 'PlainText',
value: 'Tell me your name',
},
],
},
{
id: 'Elicit.Intent-SendMessageIntent.IntentSlot-Email',
variations: [
{
type: 'PlainText',
value: 'What is your email ?',
},
{
type: 'PlainText',
value: 'Tell me your email',
},
],
},
{
id: 'Elicit.Intent-SendMessageIntent.IntentSlot-Message',
variations: [
{
type: 'PlainText',
value: 'What is your message',
},
],
},
];
2018-01-23 00:20:50 +01:00
2018-01-19 07:43:10 +01:00
let dialogIntents = [
{
name: 'SendMessageIntent',
confirmationRequired: false,
prompts: {},
slots: [
{
name: 'Name',
type: 'AMAZON.US_FIRST_NAME',
elicitationRequired: true,
confirmationRequired: false,
prompts: {
elicitation: 'Elicit.Intent-SendMessageIntent.IntentSlot-Name',
},
},
{
name: 'Email',
type: 'EmailSlot',
elicitationRequired: true,
confirmationRequired: false,
prompts: {
elicitation: 'Elicit.Intent-SendMessageIntent.IntentSlot-Email',
},
},
{
name: 'Message',
type: 'MessageSlot',
elicitationRequired: true,
confirmationRequired: false,
prompts: {
elicitation: 'Elicit.Intent-SendMessageIntent.IntentSlot-Message',
},
},
],
},
];
2018-01-22 22:34:13 +01:00
result.interactionModel = {};
2018-01-13 14:57:41 +01:00
2018-01-22 22:34:13 +01:00
result.interactionModel.languageModel = {
2018-01-08 21:50:24 +01:00
invocationName: skill.invocationName,
2018-01-22 22:34:13 +01:00
types: customSlotTypes,
2018-01-08 21:50:24 +01:00
intents: allIntents,
};
2018-01-05 00:51:49 +01:00
2018-01-22 22:34:13 +01:00
result.interactionModel.prompts = dialogPrompts;
result.interactionModel.dialog = {};
result.interactionModel.dialog.intents = dialogIntents;
2018-01-19 23:26:59 +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) {
2018-01-23 00:20:50 +01:00
let generatedInteractionModel = generateInteractionModel (skill);
2018-01-08 21:50:24 +01:00
return fetch (
`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`,
{
method: 'POST',
headers: {
Authorization: config.TOKEN,
},
2018-01-22 22:34:13 +01:00
body: generatedInteractionModel,
2018-01-08 21:50:24 +01:00
}
);
};
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);
});
}
});
},
};