Skip to content

Instantly share code, notes, and snippets.

@gpickin
Created April 23, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpickin/4a57d996df15079abb7b7136e25bf414 to your computer and use it in GitHub Desktop.
Save gpickin/4a57d996df15079abb7b7136e25bf414 to your computer and use it in GitHub Desktop.
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills
* nodejs skill development kit.
* This sample supports multiple lauguages. (en-US, en-GB, de-DE).
* The Intent Schema, Custom Slots and Sample Utterances for this skill, as well
* as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-fact
**/
'use strict';
const Alexa = require('alexa-sdk');
//=========================================================================================================================================
//TODO: The items below this comment need your attention.
//=========================================================================================================================================
//Replace with your app ID (OPTIONAL). You can find this value at the top of your skill's page on http://developer.amazon.com.
//Make sure to enclose your value in quotes, like this: const APP_ID = 'amzn1.ask.skill.bb4045e6-b3e8-4133-b650-72923c5980f1';
const APP_ID = undefined;
const SKILL_NAME = 'Forgebox';
const GET_FACT_MESSAGE = "Here's your information: ";
const HELP_MESSAGE = 'Forgebox, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';
//=========================================================================================================================================
//Editing anything below this line might break your skill.
//=========================================================================================================================================
const handlers = {
'LaunchRequest': function () {
this.emit('forgeboxDownloads');
},
'forgeboxDownloads': function () {
let slug = "kiwisays";
slug = this.event.request.intent.slots.slug.value;
//console.log( this.event.request.intent );
//console.log( 'past the if' );
//this.response.speak('hi');
//this.emit(':responseReady');
const $this = this;
const http = require('http');
const options = {
hostname: 'edf6f5f8.ngrok.io',
port: 80,
path: '/forgebox/entry/' + slug + '/downloads',
method: 'GET'
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', function (chunk) {
//console.log('BODY: ' + chunk);
const bodyString = chunk;
const bodyObject = JSON.parse( chunk );
if( bodyObject.error ){
const speechOutput = 'Error loading data';
$this.response.cardRenderer(SKILL_NAME, speechOutput);
$this.response.speak(speechOutput);
$this.emit(':responseReady');
} else {
const speechOutput = GET_FACT_MESSAGE + 'The package with the slug ' + slug + ' has ' + bodyObject.data + ' hits';
$this.response.cardRenderer(SKILL_NAME, 'The package with the slug ' + slug + ' has ' + bodyObject.data + ' hits');
$this.response.speak(speechOutput);
$this.emit(':responseReady');
}
});
});
req.on('error', (e) => {
console.error(e);
const speechOutput = 'Error loading data';
$this.response.cardRenderer(SKILL_NAME, speechOutput);
$this.response.speak(speechOutput);
$this.emit(':responseReady');
});
req.end();
},
'AMAZON.HelpIntent': function () {
const speechOutput = HELP_MESSAGE;
const reprompt = HELP_REPROMPT;
this.response.speak(speechOutput).listen(reprompt);
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
};
exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment