-
-
Save anonymous/119c0367328266fda0b7 to your computer and use it in GitHub Desktop.
BTCprice Alexa Skill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Route the incoming request based on type (LaunchRequest, IntentRequest, | |
// etc.) The JSON body of the request is provided in the event parameter. | |
exports.handler = function (event, context) { | |
try { | |
console.log("event.session.application.applicationId=" + event.session.application.applicationId); | |
/** | |
* Uncomment this if statement and populate with your skill's application ID to | |
* prevent someone else from configuring a skill that sends requests to this function. | |
*/ | |
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.11eee174-e7ab-4ca8-98f0-6931bae5db0b") { | |
context.fail("Invalid Application ID"); | |
} | |
if (event.session.new) { | |
onSessionStarted({requestId: event.request.requestId}, event.session); | |
} | |
if (event.request.type === "LaunchRequest") { | |
onLaunch(event.request, | |
event.session, | |
function callback(sessionAttributes, speechletResponse) { | |
context.succeed(buildResponse(sessionAttributes, speechletResponse)); | |
}); | |
} else if (event.request.type === "IntentRequest") { | |
onIntent(event.request, | |
event.session, | |
function callback(sessionAttributes, speechletResponse) { | |
context.succeed(buildResponse(sessionAttributes, speechletResponse)); | |
}); | |
} else if (event.request.type === "SessionEndedRequest") { | |
onSessionEnded(event.request, event.session); | |
context.succeed(); | |
} | |
} catch (e) { | |
context.fail("Exception: " + e); | |
} | |
}; | |
/** | |
* Called when the session starts. | |
*/ | |
function onSessionStarted(sessionStartedRequest, session) { | |
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId | |
+ ", sessionId=" + session.sessionId); | |
} | |
/** | |
* Called when the user launches the skill without specifying what they want. | |
*/ | |
function onLaunch(launchRequest, session, callback) { | |
console.log("onLaunch requestId=" + launchRequest.requestId | |
+ ", sessionId=" + session.sessionId); | |
// Dispatch to your skill's launch. | |
getWelcomeResponse(callback); | |
} | |
/** | |
* Called when the user specifies an intent for this skill. | |
*/ | |
function onIntent(intentRequest, session, callback) { | |
console.log("onIntent requestId=" + intentRequest.requestId | |
+ ", sessionId=" + session.sessionId); | |
getWelcomeResponse(callback); | |
} | |
/** | |
* Called when the user ends the session. | |
* Is not called when the skill returns shouldEndSession=true. | |
*/ | |
function onSessionEnded(sessionEndedRequest, session) { | |
console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId | |
+ ", sessionId=" + session.sessionId); | |
// Add cleanup logic here | |
} | |
// --------------- Functions that control the skill's behavior ----------------------- | |
function getWelcomeResponse(callback) { | |
// If we wanted to initialize the session to have some attributes we could add those here. | |
var sessionAttributes = {}; | |
var cardTitle = "Bitcoin Price"; | |
var speechOutput = "Welcome to the Bitcoin Price skill, " | |
+ "Please wait while I find the current price of Bitcoin."; | |
//Gets the current bitcoin pice from BTC-e. | |
JSONObject btcdata = getJSONfromURL("https://btc-e.com/api/2/btc_usd/ticker"); | |
int btcusd = -1; | |
int btcusd = btcdata.getInt("avg"); | |
if(btcusd>-1){ | |
var repromptText = "The current price of Bitcoin is " + btcusd + " US Dollars."; | |
} else { | |
var repromptText = "The bitcoin exchange data cannot be reached at the moment. Please try again later."; | |
} | |
var shouldEndSession = true; | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); | |
} | |
// --------------- Helpers that build all of the responses ----------------------- | |
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { | |
return { | |
outputSpeech: { | |
type: "PlainText", | |
text: output | |
}, | |
card: { | |
type: "Simple", | |
title: "SessionSpeechlet - " + title, | |
content: "SessionSpeechlet - " + output | |
}, | |
reprompt: { | |
outputSpeech: { | |
type: "PlainText", | |
text: repromptText | |
} | |
}, | |
shouldEndSession: shouldEndSession | |
} | |
} | |
function buildResponse(sessionAttributes, speechletResponse) { | |
return { | |
version: "1.0", | |
sessionAttributes: sessionAttributes, | |
response: speechletResponse | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment