Alessio Barducci
Published © GPL3+

Wear Assistant

Wear Assistant is an Alexa Custom Skill that suggest you the way to dress base on the weather of your city.

IntermediateFull instructions provided24 hours889
Wear Assistant

Things used in this project

Story

Read more

Schematics

Skill Architecture

The complete skill system architecture

Interaction Model Example

Code

Lambda Function Code

JavaScript
The sensible data has been removed from the script
var APP_ID = /* removed */;

var AlexaSkill = require('./AlexaSkill');
var http = require("http");
var CryptoJS = require("crypto-js");

var GOODBYE_SENTENCES = ["Have a nice day!", "Goodbye!", "Bye!", "Cheers!"];

var WearAssistant = function () {
    AlexaSkill.call(this, APP_ID);
};

WearAssistant.prototype = Object.create(AlexaSkill.prototype);
WearAssistant.prototype.constructor = WearAssistant;

WearAssistant.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
    console.log("WearAssistant onSessionStarted requestId: " + sessionStartedRequest.requestId
        + ", sessionId: " + session.sessionId);
};

WearAssistant.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
    console.log("WearAssistant onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
    getWelcomeResponse(session, response);
};

WearAssistant.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
    console.log("WearAssistant onSessionEnded requestId: " + sessionEndedRequest.requestId
        + ", sessionId: " + session.sessionId);
};

WearAssistant.prototype.intentHandlers = {
	"SimpleWearSuggestion": function (intent, session, response) {
        getSimpleWearSuggestion(intent, session, response);
    },
	
    "AMAZON.HelpIntent": function (intent, session, response) {
        getWelcomeResponse(session, response);
    },

    "AMAZON.StopIntent": function (intent, session, response) {
        response.tell(GOODBYE_SENTENCES[Math.floor(Math.random() * GOODBYE_SENTENCES.length)]);
    },

    "AMAZON.CancelIntent": function (intent, session, response) {
        response.tell(GOODBYE_SENTENCES[Math.floor(Math.random() * GOODBYE_SENTENCES.length)]);
    }
};

function getSimpleWearSuggestion(intent, session, response) {
	
	var city = intent.slots.City;
	if (typeof city === 'undefined' || !('value' in city))
	{
		getWelcomeResponse(session, response);
	}
	
	var url = /* removed */;
	var seconds = parseInt(new Date().getTime() / 1000);
	var seed = /* removed */;
	console.log("Calculating secret for " + (seconds + seed));
	var secret = CryptoJS.MD5(seconds + seed);
	url = url + "?secret=" + secret + "&city=" + city.value;
	
	console.log("Calling URL: " + url);
	
	http.get(url, function (result) {
		console.log('Response code: ' + result.statusCode);
		
		if (result.statusCode == 200) {
			outText = 'Done!';
			var body = '';
			result.on('data', function (chunk) {
				body += chunk;
			});
			result.on('end', function () {
				var responseObject = JSON.parse(body);
				
				if (responseObject['status'] === 'CITY_NOT_FOUND') {
					console.log("city not found");
					outText = responseObject['textToSay'];
					var responseType = AlexaSkill.speechOutputType.PLAIN_TEXT;
					console.log(responseObject['responseType']);
					if (responseObject['responseType'] === 'SSML') {
						responseType = AlexaSkill.speechOutputType.SSML;
					}
					var speechOutput = {
						speech: outText,
						type: responseType
					};
					response.tell(speechOutput);
				} else {
					console.log("city found");
					outText = responseObject['textToSay'];
					var responseType = AlexaSkill.speechOutputType.PLAIN_TEXT;
					console.log(responseType);
					var speechOutput = {
						speech: outText,
						type: responseType
					};
					
					var cardTitle = responseObject['cardTitle'];
					var cardContent = responseObject['cardContent'];					
					response.tellWithCard(speechOutput, cardTitle, cardContent);
				}
			});
		}
		else
		{
			outText = 'There was a problem, I am not able to retrieve information from Internet. Sorry!';
			var speechOutput = {
				speech: outText,
				type: AlexaSkill.speechOutputType.PLAIN_TEXT
			};
			response.tell(speechOutput);
		}
		
	});
}

function getWelcomeResponse(session, response) {
	var speechText = "Wear Assistant is here to assist you. Do not care to check the weather today, I can check it for you and I'll tell you what clothes you should wear today. Tell me the city where you are living, and I will tell you the perfect way to dress.";
	var repromptText = "Please tell me the town.";
	var speechOutput = {
		speech: speechText,
		type: AlexaSkill.speechOutputType.PLAIN_TEXT
	};
	var repromptOutput = {
		speech: repromptText,
		type: AlexaSkill.speechOutputType.PLAIN_TEXT
	};
	response.ask(speechOutput, repromptOutput);
}


exports.handler = function (event, context) {
    var wearAssistant = new WearAssistant();
    wearAssistant.execute(event, context);
};

Intent Schema

JSON
{
  "intents": [
    {
      "slots": [
        {
          "name": "City",
          "type": "AMAZON.EUROPE_CITY"
        }
      ],
      "intent": "SimpleWearSuggestion"
    },
    {
      "slots": [
        {
          "name": "City",
          "type": "AMAZON.EUROPE_CITY"
        }
      ],
      "intent": "SpecifyCity"
    },
    {
      "intent": "AMAZON.HelpIntent"
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "intent": "AMAZON.CancelIntent"
    }
  ]
}

Sample Utterances

snippets
SimpleWearSuggestion what should I wear today in {City}
SimpleWearSuggestion what should I wear in {City}
SimpleWearSuggestion how should I dress today in {City}
SimpleWearSuggestion how should I dress in {City}
SpecifyCity {City}
SpecifyCity in {City}
SpecifyCity about {City}

Credits

Alessio Barducci

Alessio Barducci

0 projects • 1 follower

Comments