31
loading...
This website collects cookies to deliver better user experience
{
"interactionModel": {
"languageModel": {
"invocationName": "random quote",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "RandomQuoteIntent",
"slots": [],
"samples": [
"give me quote",
"I want a quote"
]
}
],
"types": []
}
}
}
{
"interactionModel": {
"languageModel": {
"invocationName": "قول عشوائي",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "AMAZON.FallbackIntent",
"samples": []
},
{
"name": "RandomQuoteIntent",
"slots": [],
"samples": [
"من فضلك أعطني قولاً",
"أريد قولًا"
]
}
],
"types": []
}
}
}
"dependencies": {
"ask-sdk-core": "^2.6.0",
"ask-sdk-model": "^1.18.0",
"aws-sdk": "^2.326.0",
"i18next": "^20.3.2"
}
.addRequestInterceptors(
LocalisationRequestInterceptor
)
const LocalisationRequestInterceptor = {
process(handlerInput) {
i18n.init({
lng: Alexa.getLocale(handlerInput.requestEnvelope),
resources: languageStrings
}).then((t) => {
handlerInput.t = (...args) => t(localizationClient(...args));
});
}
};
const localizationClient = function () {
const args = arguments;
const value = i18n.t(args[0], {
returnObjects: true
});
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length)];
} else {
return value;
}
}
const speakOutput = handlerInput.t('WELCOME_MSG');
module.exports = {
en: {
translation: {
WELCOME_MSG: `Welcome to random quote, say I want a quote`,
}
},
ar: {
translation: {
WELCOME_MSG: `مرحبًا بك في قول عشوائي ، قل أريد قولً`,
}
}
}
{
"name": "CustomQuoteIntent",
"slots": [
{
"name": "customQuote",
"type": "AMAZON.SearchQuery"
}
],
"samples": [
"give me a {customQuote} quote",
"I want a {customQuote} quote"
]
}
{
"name": "CustomQuoteIntent",
"slots": [
{
"name": "customQuote",
"type": "AMAZON.SearchQuery"
}
],
"samples":
"أعطني مقولة {customQuote}",
"أريد مقولة {customQuote}"
]
}
const AWS = require("aws-sdk");
const CONFIG = require("../config/aws");
module.exports.dynamoDBHelper = async function dynamoDBHelper() {
AWS.config.update({region: CONFIG.REGION});
const dynamoDB = new AWS.DynamoDB.DocumentClient();
return dynamoDB;
}
const CONFIG = require("../config/aws");
const tableName = CONFIG.TABLE_NAME;
const dbHelper = require("./dbHelper");
var queries = function () {};
queries.prototype.getQuotes = async (languageID) => {
const params = {
TableName: tableName,
KeyConditionExpression: "#languageID = :language_id",
ExpressionAttributeNames: {
"#languageID": "languageId"
},
ExpressionAttributeValues: {
":language_id": languageID
}
}
const dynamoDB = await dbHelper.dynamoDBHelper();
const response = await dynamoDB.query(params).promise();
return response;
}
module.exports = new queries();
const CustomQuoteIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'CustomQuoteIntent';
},
async handle(handlerInput) {
const slotValue = handlerInput.requestEnvelope.request.intent.slots.customQuote.value;
const languageID = Alexa.getLocale(handlerInput.requestEnvelope);
let speakOutput;
try {
let response = await queries.getQuotes(languageID);
let quoteArray = response.Items[0][slotValue];
speakOutput = quoteArray[Math.floor(Math.random() * quoteArray.length)];
} catch (error) {
console.log('-- ERROR --', error);
speakOutput = handlerInput.t('ERROR');
}
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
let quoteArray = response.Items[0][slotValue];
speakOutput = quoteArray[Math.floor(Math.random() * quoteArray.length)];