Last active
December 7, 2017 21:34
-
-
Save simonszu/3ab0ab5431a3d0dbb2188c66b252d42c to your computer and use it in GitHub Desktop.
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
/* 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'); | |
const http = require('https'); | |
const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL). | |
const api_base = "https://trackweather.pixeltakt.net/api/"; | |
var states = { | |
DEFAULT : '_DEFAULT', | |
DETAILWEATHER : '_DETAILWEATHER' | |
} | |
var weatherJSON; | |
function doJSONRequest(uri, callback) { | |
var output = ""; | |
try { | |
console.log("Request ... START!!!"); | |
var req = http.get(uri, function(res){ | |
res.setEncoding('utf-8'); | |
var responseString = ''; | |
res.on('data', function(data) { | |
responseString += data; | |
console.log(data + " GET!") | |
}); | |
res.on('end', function() { | |
console.log(responseString); | |
console.log("Request ... STOP!!!"); | |
output = JSON.parse(responseString); | |
callback(output); | |
}); | |
}); | |
console.log(req); | |
} | |
catch(e) { | |
console.log("Error:" +e); | |
} | |
} | |
const defaultHandlers = Alexa.CreateStateHandler(states.DEFAULT, { | |
'LaunchRequest': function () { | |
this.response.speak("Willkommen bei TrackBuddy!").listen("Willkommen bei TrackBuddy"); | |
this.emit(':responseReady'); | |
}, | |
'getTrackIntent' : function () { | |
}, | |
'getWeatherIntent' : function () { | |
var trackName = this.event.request.intent.slots.TrackName.value; | |
var trackDate = this.event.request.intent.slots.Date.value; | |
var trackTime = this.event.request.intent.slots.Time.value; | |
var trackNumber = this.event.request.intent.slots.Number.value; | |
var requestDate = new Date(); | |
if (trackDate !== undefined) { | |
if (trackDate != "") { | |
// Es wurde ein Datum spezifiziert, also dieses auf das Request-Datum übernehmen | |
var tmpDate = new Date(); | |
// Da js bei Datumskonstruktoren mit Stringeingabe kein Datums-Objekt zurückgibt, | |
// muss das Datum einmal manuell auf das Jahr gesetzt werden. | |
tmpDate.setTime(Date.parse(trackDate)); | |
// Neues Datum in den Request übernehmen | |
requestDate.setFullYear(tmpDate.getFullYear()); | |
requestDate.setMonth(tmpDate.getMonth()); | |
requestDate.setDate(tmpDate.getDate()); | |
} | |
} | |
if (trackTime !== undefined) { | |
if (trackTime != "") { | |
// Es wurde eine Zeit spezifiziert, also dieses auf das Request-Datum übernehmen | |
var tmpDate = new Date(); | |
// What is this? I don't even. | |
tmpDate.setTime(Date.parse(trackTime)); | |
// Neues Datum in den Request übernehmen | |
requestDate.setHours(tmpDate.getHours()); | |
requestDate.setMinutes(tmpDate.getMinutes()); | |
requestDate.setSeconds(tmpDate.getSeconds()); | |
} | |
} | |
if (trackNumber === undefined) { | |
var userId = this.event.session.user.userId; | |
var mythis = this; | |
doJSONRequest(api_base + "speed/" + userId, (output) => { | |
trackNumber = output.speed; | |
mythis.response.speak(trackNumber); | |
mythis.emit(":responseReady"); | |
}); | |
} | |
doJSONRequest(api_base + "weather/" + trackName + "/" + requestDate.getTime() / 1000 + "/" + trackNumber, (output) => { | |
var responseString = ''; | |
switch (output.summary) { | |
case 0: | |
responseString = "Die Welt geht unter. Flieh so schnell du kannst." | |
break; | |
case 1: | |
responseString = "Auf der Route stürmt es derzeit. Du solltest auf die Tour verzichten." | |
break; | |
case 2: | |
responseString = "Es schneit gerade irgendwo auf der Route. Du solltest heute zuhause bleiben" | |
break; | |
case 3: | |
responseString = "Es regnet gerade auf der Route. Bleib besser zuhause." | |
break; | |
case 4: | |
responseString = "Es windet gerade sehr stark auf der Route. Du solltest dir überlgen, ob du zuhause bleiben möchtest." | |
break; | |
case 5: | |
responseString = "Es nieselt gerade auf deiner Route. Pack dir ne Regenjacke ein." | |
break; | |
case 6: | |
responseString = "Es ist neblig auf der Route." | |
break; | |
case 7: | |
responseString = "Das Wetter ist top auf deiner Route. Wenn nicht jetzt, wann dann?" | |
break; | |
default: | |
} | |
this.handler.state = states.DETAILWEATHER; | |
weatherJSON = output; | |
responseString += " Möchtest du mehr Details erfahren?"; | |
mythis.response.speak(responseString).listen(responseString); | |
mythis.emit(":responseReady"); | |
}); | |
}, | |
'setSpeedIntent' : function () { | |
}, | |
'testWeatherIntent' : function () { | |
}, | |
'AMAZON.HelpIntent' : function () { | |
}, | |
'AMAZON.CancelIntent' : function () { | |
this.response.speak('Bis zum nächsten Mal!'); | |
this.emit(':responseReady'); | |
}, | |
'AMAZON.StopIntent' : function () { | |
this.response.speak('Bis zum nächsten Mal!'); | |
this.emit(':responseReady'); | |
}, | |
}); | |
const detailWeatherHandlers = Alexa.CreateStateHandler(states.DETAILWEATHER, { | |
'AMAZON.YesIntent' : function () { | |
}, | |
'AMAZON.NoIntent' : function () { | |
}, | |
}); | |
exports.handler = (event, context, callback) => { | |
const alexa = Alexa.handler(event, context); | |
alexa.registerHandlers(defaultHandlers); | |
alexa.dynamoDBTableName="TrackWeatherUsers"; | |
alexa.execute(); | |
}; |
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
{ | |
"languageModel": { | |
"types": [ | |
{ | |
"name": "GESCHWINDIGKEIT", | |
"values": [ | |
{ | |
"id": null, | |
"name": { | |
"value": "KMH", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Kilometer pro Stunde", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Stundenkilometer", | |
"synonyms": [] | |
} | |
} | |
] | |
}, | |
{ | |
"name": "KURSNAME", | |
"values": [ | |
{ | |
"id": null, | |
"name": { | |
"value": "Dortmund Route", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Route 1", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Bockwurst", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "TU Dortmund", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Mettwurst", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Fenster", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Tür", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Glas", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Gurke", | |
"synonyms": [] | |
} | |
} | |
] | |
}, | |
{ | |
"name": "STADT", | |
"values": [ | |
{ | |
"id": null, | |
"name": { | |
"value": "Dortmund", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Iserlohn", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Berlin", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Hamburg", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Salzburg", | |
"synonyms": [] | |
} | |
} | |
] | |
}, | |
{ | |
"name": "TRACKS", | |
"values": [ | |
{ | |
"id": null, | |
"name": { | |
"value": "Track", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Tracks", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Routen", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Runden", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Rundgänge", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Rundkurse", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "Kurse", | |
"synonyms": [] | |
} | |
} | |
] | |
}, | |
{ | |
"name": "WETTER", | |
"values": [ | |
{ | |
"id": "0", | |
"name": { | |
"value": "extrem", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": "1", | |
"name": { | |
"value": "stürmen", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": "2", | |
"name": { | |
"value": "schneien", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": "3", | |
"name": { | |
"value": "regnen", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": "4", | |
"name": { | |
"value": "windig", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": "5", | |
"name": { | |
"value": "nieseln", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": "6", | |
"name": { | |
"value": "neblig", | |
"synonyms": [] | |
} | |
}, | |
{ | |
"id": "7", | |
"name": { | |
"value": "klar", | |
"synonyms": [] | |
} | |
} | |
] | |
} | |
], | |
"intents": [ | |
{ | |
"name": "AMAZON.CancelIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.HelpIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.StopIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "getTrackIntent", | |
"samples": [ | |
"Gib mir meine {Tracks}", | |
"Gib mir meine {Tracks} von {min} bis {max} Kilometer", | |
"Gib mir meine {Tracks} von {min} Kilometer bis {max} Kilometer", | |
"Gib mir meine {Tracks} von {min} bis {max}", | |
"Gib mir meine {Tracks} in {City} " | |
], | |
"slots": [ | |
{ | |
"name": "min", | |
"type": "AMAZON.NUMBER" | |
}, | |
{ | |
"name": "max", | |
"type": "AMAZON.NUMBER" | |
}, | |
{ | |
"name": "Tracks", | |
"type": "TRACKS" | |
}, | |
{ | |
"name": "City", | |
"type": "STADT" | |
} | |
] | |
}, | |
{ | |
"name": "getWeatherIntent", | |
"samples": [ | |
"Wie ist das Wetter auf {TrackName}", | |
"Wie ist das Wetter auf {TrackName} um {Time}", | |
"Wie ist das Wetter auf {TrackName} um {Time} am {Date}", | |
"Wie ist das Wetter auf {TrackName} mit {Number} {Geschwindigkeit}", | |
"Wie ist das Wetter auf {TrackName} um {Time} mit {Number} {Geschwindigkeit}", | |
"Wie ist das Wetter auf {TrackName} um {Time} am {Date} mit {Number} {Geschwindigkeit}", | |
"Wie ist das Wetter auf {TrackName} um {Time} {Date}", | |
"Wie ist das Wetter auf {TrackName} am {Date} um {Time}", | |
"Wie ist das Wetter auf {TrackName} {Date} {Time}", | |
"Wie ist das Wetter auf {TrackName} {Time} {Date}", | |
"Wie ist das Wetter auf {TrackName} um {Time} {Date} mit {Number} {Geschwindigkeit}", | |
"Wie ist das Wetter auf {TrackName} {Time} {Date} mit {Number} {Geschwindigkeit}", | |
"Wie ist das Wetter auf {TrackName} {Date} {Time} mit {Number} {Geschwindigkeit}" | |
], | |
"slots": [ | |
{ | |
"name": "TrackName", | |
"type": "KURSNAME" | |
}, | |
{ | |
"name": "Time", | |
"type": "AMAZON.TIME" | |
}, | |
{ | |
"name": "Date", | |
"type": "AMAZON.DATE" | |
}, | |
{ | |
"name": "Number", | |
"type": "AMAZON.NUMBER" | |
}, | |
{ | |
"name": "Geschwindigkeit", | |
"type": "GESCHWINDIGKEIT" | |
} | |
] | |
}, | |
{ | |
"name": "setSpeedIntent", | |
"samples": [ | |
"Setze meine Geschwindigkeit auf {Number} {Geschwindigkeit}", | |
"Setze meine Geschwindigkeit auf {Number}", | |
"Setze meinen Speed auf {Number} {Geschwindigkeit}", | |
"Setze meinen Speed auf {Number}" | |
], | |
"slots": [ | |
{ | |
"name": "Number", | |
"type": "AMAZON.NUMBER" | |
}, | |
{ | |
"name": "Geschwindigkeit", | |
"type": "GESCHWINDIGKEIT" | |
} | |
] | |
}, | |
{ | |
"name": "testWeatherIntent", | |
"samples": [ | |
"Wird es auf {TrackName} {Weather}", | |
"Wird es auf {TrackName} um {Time} {Weather}", | |
"Wird es auf {TrackName} um {Time} am {Date} {Weather}" | |
], | |
"slots": [ | |
{ | |
"name": "TrackName", | |
"type": "KURSNAME" | |
}, | |
{ | |
"name": "Weather", | |
"type": "WETTER" | |
}, | |
{ | |
"name": "Time", | |
"type": "AMAZON.TIME" | |
}, | |
{ | |
"name": "Date", | |
"type": "AMAZON.DATE" | |
} | |
] | |
} | |
], | |
"invocationName": "trackbuddy" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment