Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fetch = require('node-fetch');
- var endpoint = "https://app.wehaus.com/api/v2/" // ENDPOINT GOES HERE
- exports.handler = (event, context) => {
- try {
- if (event.session.new) {
- // New Session
- console.log("NEW SESSION")
- }
- switch (event.request.type) {
- case "LaunchRequest":
- // Launch Request
- console.log(`LAUNCH REQUEST`)
- context.succeed(
- generateResponse(
- buildSpeechletResponse("Welcome to an Alexa by Wehaus", true),
- {}
- )
- )
- break;
- case "IntentRequest":
- // Intent Request
- console.log(`INTENT REQUEST`)
- switch(event.request.intent.name) {
- case "TurnOnAll":
- fetch(endpoint + 'devices', {
- method: 'GET',
- headers: {
- "X-User-Email": "oficina@wehaus.com",
- "X-User-Token": "mKyokxzJyC_3F45QiEry"
- }
- }).then((data) => {
- if(data){
- let response = data.json();
- response.then((devices) => {
- if(devices){
- devices.forEach((device) => {
- if(device.primitive === "ActionDevice"){
- fetch(endpoint + 'device/' + device.id, {
- method: 'PUT',
- headers: {
- "X-User-Email": "oficina@wehaus.com",
- "X-User-Token": "mKyokxzJyC_3F45QiEry"
- },
- body: JSON.stringify({
- state: 'on'
- })
- })
- }
- })
- }
- })
- context.succeed(
- generateResponse(
- buildSpeechletResponse(`All your lights is set on`, true),
- {}
- )
- )
- }
- })
- break;
- case "TurnOffAll":
- fetch(endpoint + 'devices', {
- method: 'GET',
- headers: {
- "X-User-Email": "oficina@wehaus.com",
- "X-User-Token": "mKyokxzJyC_3F45QiEry"
- }
- }).then((data) => {
- if(data){
- let response = data.json();
- response.then((devices) => {
- if(devices){
- devices.forEach((device) => {
- if(device.primitive === "ActionDevice"){
- fetch(endpoint + 'device/' + device.id, {
- method: 'PUT',
- headers: {
- "X-User-Email": "oficina@wehaus.com",
- "X-User-Token": "mKyokxzJyC_3F45QiEry"
- },
- body: JSON.stringify({
- state: 'off'
- })
- })
- }
- })
- }
- })
- context.succeed(
- generateResponse(
- buildSpeechletResponse(`All your lights is set off`, true),
- {}
- )
- )
- }
- })
- break;
- case "TurnOnDevice":
- console.log(event.request.intent.slots.ID.value)
- fetch(endpoint + 'device/' + event.request.intent.slots.ID.value, {
- method: 'PUT',
- headers: {
- "X-User-Email": "oficina@wehaus.com",
- "X-User-Token": "mKyokxzJyC_3F45QiEry"
- },
- body: JSON.stringify({
- state: 'on'
- })
- }).then((data) => {
- if(data){
- context.succeed(
- generateResponse(
- buildSpeechletResponse(`Your device is set on`, true),
- {}
- )
- )
- }
- })
- break;
- case "TurnOffDevice":
- console.log(event.request.intent.slots.ID.value)
- fetch(endpoint + 'device/' + event.request.intent.slots.ID.value, {
- method: 'PUT',
- headers: {
- "X-User-Email": "oficina@wehaus.com",
- "X-User-Token": "mKyokxzJyC_3F45QiEry"
- },
- body: JSON.stringify({
- state: 'off'
- })
- }).then((data) => {
- if(data){
- context.succeed(
- generateResponse(
- buildSpeechletResponse(`Your device is set on`, true),
- {}
- )
- )
- }
- })
- break;
- default:
- throw "Invalid intent"
- }
- break;
- case "SessionEndedRequest":
- // Session Ended Request
- console.log(`SESSION ENDED REQUEST`)
- break;
- default:
- context.fail(`INVALID REQUEST TYPE: ${event.request.type}`)
- }
- } catch(error) { context.fail(`Exception: ${error}`) }
- }
- // Helpers
- buildSpeechletResponse = (outputText, shouldEndSession) => {
- return {
- outputSpeech: {
- type: "PlainText",
- text: outputText
- },
- shouldEndSession: shouldEndSession
- }
- }
- generateResponse = (speechletResponse, sessionAttributes) => {
- return {
- version: "1.0",
- sessionAttributes: sessionAttributes,
- response: speechletResponse
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement