Vignesh J
Published © GPL3+

Drizzle Cloud

An Alexa skill which calls the Dark Sky API to determine if it will rain in the next hour or not by specifying the coordinates of your city.

IntermediateFull instructions provided5 hours881

Things used in this project

Hardware components

Amazon Echo
Amazon Alexa Amazon Echo
×1

Software apps and online services

AWS Lambda
Amazon Web Services AWS Lambda
Dark Sky API
Alexa Skills Kit
Amazon Alexa Alexa Skills Kit

Story

Read more

Schematics

VOICE USER INTERFACE

Code

Drizzle

JavaScript
Copy and Paste this code in Edit Codeline while creating LAMBDA function
'use strict';

GLOBAL.darkSkySecretKey = "YOUR DARK SKY API  KEY HERE";
GLOBAL.location = "38.903741,-77.039107"; //Latiude and Longitude Co-ordinates of your city Eg: Washington DC 



//  Helpers that build all of the responses 

function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
    return {
        outputSpeech: {
            type: 'PlainText',
            text: output,
        },
        card: {
            type: 'Simple',
            title:"Drizzle",
            content:output,
        },
        reprompt: {
            outputSpeech: {
                type: 'PlainText',
                text: repromptText,
            },
        },
        shouldEndSession,
    };
}

function buildResponse(sessionAttributes, speechletResponse) {
    return {
        version: '1.0',
        sessionAttributes,
        response: speechletResponse,
    };
}

// --------------- Functions that control the skill's behavior -----------------

function getRainStatus(callback) {
    
    const sessionAttributes = {};
    const cardTitle = 'DRIZZLE CLOUD';

    const httpTransport = require('https');
    const responseEncoding = 'utf8';
    const urlPath = '/forecast/' + darkSkySecretKey + '/' + location;

    const httpOptions = {
        hostname: 'api.darksky.net',
        port: '443',
        path: urlPath,
        method: 'GET',
        headers: {}
    };
    httpOptions.headers['User-Agent'] = 'node ' + process.version;

    const request = httpTransport.request(httpOptions, (res) => {
        let responseBufs = [];
        let responseStr = '';

        res.on('data', (chunk) => {
            if (Buffer.isBuffer(chunk)) {
                responseBufs.push(chunk);
            }
            else {
                responseStr = responseStr + chunk;
            }
        }).on('end', () => {
            responseStr = responseBufs.length > 0 ?
                Buffer.concat(responseBufs).toString(responseEncoding) : responseStr;

            var fbResponse = JSON.parse(responseStr);
            const speechOutput = fbResponse.minutely.summary;
            const repromptText = '';
            const shouldEndSession = true;
            callback(sessionAttributes,
                buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
        });

    })
    .setTimeout(0)
    .on('error', (error) => {
        // callback(error);
        const speechOutput = 'Dark Sky is not available, you\'ll need to look outside';
        // If the user either does not reply to the welcome message or says something that is not
        // understood, they will be prompted again with this text.
        const repromptText = '';
        const shouldEndSession = true;
        callback(sessionAttributes,
            buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
    });
    request.write("")
    request.end();

}

// Events 

/*
 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.
    getRainStatus(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}`);
    // Do stuff...
    getRainStatus(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
}


//  Main handler

// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = (event, context, callback) => {
    try {
        console.log(`event.session.application.applicationId=${event.session.application.applicationId}`);
        
        if (event.session.new) {
            onSessionStarted({ requestId: event.request.requestId }, event.session);
        }

        if (event.request.type === 'LaunchRequest') {
            onLaunch(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        } else if (event.request.type === 'IntentRequest') {
            onIntent(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        } else if (event.request.type === 'SessionEndedRequest') {
            onSessionEnded(event.request, event.session);
            callback();
        }
    } catch (err) {
        callback(err);
    }
};

Credits

Vignesh J

Vignesh J

4 projects • 7 followers
Hello Friends I am Vignesh ,B.E (Electronics &Communication)Graduate.Intrested in doing IOT Based projects

Comments