Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Srikanth NandirajuAkshay Krishnaiah
Created May 13, 2016 © Apache-2.0

Breathe

Breathe is a Alexa Skill that can tell the Air quality for a particular city. Alexa can now make recommendations for outdoor activities.

BeginnerFull instructions provided10 hours25
Breathe

Things used in this project

Story

Read more

Code

Breath

JavaScript
Code used to power the app
exports.handler = function(event, context) {

	if (event.request.intent) {
		if (event.request.intent.name == "FindCity") {
			var cityname = event.request.intent.slots.cityname.value;
			var http = require('http');
			var url = "http://api.breezometer.com/baqi/?key=30a932be434241a4a6e7b25b2564d736&location=" + cityname;

			http.get(url, function(response) {
				var data = '';
				response.on('data', function(x) {
					data += x;
				});
				response.on('end', function() {
					data = JSON.parse(data);
					var message = data.breezometer_description + ", in ," + cityname;
					message += ", Ask me for summary, or recommendations for children, sports, health, or pollutant";

					var sessionAttributes = {
						airdata : data,
						cityname : cityname
					};
					console.log("CITY NAME " + cityname);
					speak(message, context, sessionAttributes);
				});
			});

		} else if (event.request.intent.name == "GetSummary") {
			console.log("INTENT IS GET SUMMARY");
			getSummary(event, context);
		} else if (event.request.intent.name == "GetChildren") {
			console.log("INTENT IS GET CHILDREN");
			getRecommendationFor(event, context, 'children');
		} else if (event.request.intent.name == "GetSports") {
			console.log("INTENT IS GET SPORT");
			getRecommendationFor(event, context, 'sport');
		} else if (event.request.intent.name == "GetHealth") {
			console.log("INTENT IS GET HEALTH");
			getRecommendationFor(event, context, 'health');
		} else if (event.request.intent.name == "GetIndoors") {
			console.log("INTENT IS GET INDOORS");
			getRecommendationFor(event, context, 'inside');
		} else if (event.request.intent.name == "GetOutdoors") {
			console.log("INTENT IS GET OUTDOORS");
			getRecommendationFor(event, context, 'outside');
		} else if (event.request.intent.name == "GetPollutant") {
			console.log("INTENT IS GetPollutant");
			getRecommendationFor(event, context, 'pollutant');
		}

	} else {
		speak("Ask me for Air quality for a city. For example, Say get details for Santa Clara", context, undefined);
	}

};

function handleLaunch(event, context) {

}

function getSummary(event, context) {
	if (event.session.attributes) {
		data = event.session.attributes.airdata;
		cityname = event.session.attributes.cityname;

		var message = "In " + cityname + " , " + data.breezometer_description;
		message += ", The air quality index is " + data.breezometer_aqi;

		message += " , " + getCleanText(data.dominant_pollutant_text.main);
		message += ", Possible effects, " + getCleanText(data.dominant_pollutant_text.effects);
		message += ", Causes, " + getCleanText(data.dominant_pollutant_text.causes);

		message += ", For children, " + getCleanText(data.random_recommendations.children);
		message += ", For sports, " + getCleanText(data.random_recommendations.sport);
		message += ", For inside, " + getCleanText(data.random_recommendations.inside);
		message += ", For health, " + getCleanText(data.random_recommendations.outside);

		var sessionAttributes = {
			airdata : data,
			cityname : cityname
		};
		speak(message, context, sessionAttributes);
	} else
		speak("No data in session", context, undefined);
}

function getRecommendationFor(event, context, recType) {
	if (event.session.attributes) {
		data = event.session.attributes.airdata;
		cityname = event.session.attributes.cityname;

		var message = "In " + cityname + " for " + recType + "," + data.random_recommendations[recType];

		if ('pollutant' == recType) {
			message = "The dominant pollutant is , " + getCleanText(data.dominant_pollutant_description);
		}

		var sessionAttributes = {
			airdata : data,
			cityname : cityname
		};
		speak(message, context, sessionAttributes);
	} else
		speak("No data in session", context, undefined);
}

function getCleanText(inputText) {
	
	if(!inputText){
		return "No data available";
	}
	
	var cleanText = inputText.replace(/[^a-zA-Z ]/g, "");
	cleanText = cleanText.replace(/&/g, "and");
	return cleanText;
}

function speak(text, context, sessionAttributes) {
	var response = {
		outputSpeech : {
			type : "PlainText",
			text : text
		},
		card : {
			type : "Simple",
			title : "IOT",
			content : text
		},
		shouldEndSession : false
	};

	if (sessionAttributes) {
		context.succeed({
			response : response,
			sessionAttributes : sessionAttributes,
		});
	} else {
		context.succeed({
			response : response,
		});
	}
}

alexa.js

JavaScript
This is the Lambda function that handles voice commands to get air quality infformation
exports.handler = function(event, context) {

	if (event.request.intent) {
		if (event.request.intent.name == "FindCity") {
			var cityname = event.request.intent.slots.cityname.value;
			var http = require('http');
			var url = "http://api.breezometer.com/baqi/?key=production-key&location=" + cityname;

			http.get(url, function(response) {
				var data = '';
				response.on('data', function(x) {
					data += x;
				});
				response.on('end', function() {
					data = JSON.parse(data);
					var message = data.breezometer_description + ", in ," + cityname;
					message += ", Ask me for summary, or recommendations for children, sports, health, or pollutant";

					var sessionAttributes = {
						airdata : data,
						cityname : cityname
					};
					console.log("CITY NAME " + cityname);
					speak(message, context, sessionAttributes);
				});
			});

		} else if (event.request.intent.name == "GetSummary") {
			console.log("INTENT IS GET SUMMARY");
			getSummary(event, context);
		} else if (event.request.intent.name == "GetChildren") {
			console.log("INTENT IS GET CHILDREN");
			getRecommendationFor(event, context, 'children');
		} else if (event.request.intent.name == "GetSports") {
			console.log("INTENT IS GET SPORT");
			getRecommendationFor(event, context, 'sport');
		} else if (event.request.intent.name == "GetHealth") {
			console.log("INTENT IS GET HEALTH");
			getRecommendationFor(event, context, 'health');
		} else if (event.request.intent.name == "GetIndoors") {
			console.log("INTENT IS GET INDOORS");
			getRecommendationFor(event, context, 'inside');
		} else if (event.request.intent.name == "GetOutdoors") {
			console.log("INTENT IS GET OUTDOORS");
			getRecommendationFor(event, context, 'outside');
		} else if (event.request.intent.name == "GetPollutant") {
			console.log("INTENT IS GetPollutant");
			getRecommendationFor(event, context, 'pollutant');
		}

	} else {
		speak("Ask me for Air quality for a city. For example, Say get details for Santa Clara", context, undefined);
	}

};

function handleLaunch(event, context) {

}

function getSummary(event, context) {
	if (event.session.attributes) {
		data = event.session.attributes.airdata;
		cityname = event.session.attributes.cityname;

		var message = "In " + cityname + " , " + data.breezometer_description;
		message += ", The air quality index is " + data.breezometer_aqi;

		message += " , " + getCleanText(data.dominant_pollutant_text.main);
		message += ", Possible effects, " + getCleanText(data.dominant_pollutant_text.effects);
		message += ", Causes, " + getCleanText(data.dominant_pollutant_text.causes);

		message += ", For children, " + getCleanText(data.random_recommendations.children);
		message += ", For sports, " + getCleanText(data.random_recommendations.sport);
		message += ", For inside, " + getCleanText(data.random_recommendations.inside);
		message += ", For health, " + getCleanText(data.random_recommendations.outside);

		var sessionAttributes = {
			airdata : data,
			cityname : cityname
		};
		speak(message, context, sessionAttributes);
	} else
		speak("No data in session", context, undefined);
}

function getRecommendationFor(event, context, recType) {
	if (event.session.attributes) {
		data = event.session.attributes.airdata;
		cityname = event.session.attributes.cityname;

		var message = "In " + cityname + " for " + recType + "," + data.random_recommendations[recType];

		if ('pollutant' == recType) {
			message = "The dominant pollutant is , " + getCleanText(data.dominant_pollutant_description);
		}

		var sessionAttributes = {
			airdata : data,
			cityname : cityname
		};
		speak(message, context, sessionAttributes);
	} else
		speak("No data in session", context, undefined);
}

function getCleanText(inputText) {
	
	if(!inputText){
		return "No data available";
	}
	
	var cleanText = inputText.replace(/[^a-zA-Z ]/g, "");
	cleanText = cleanText.replace(/&/g, "and");
	return cleanText;
}

function speak(text, context, sessionAttributes) {
	var response = {
		outputSpeech : {
			type : "PlainText",
			text : text
		},
		card : {
			type : "Simple",
			title : "IOT",
			content : text
		},
		shouldEndSession : false
	};

	if (sessionAttributes) {
		context.succeed({
			response : response,
			sessionAttributes : sessionAttributes,
		});
	} else {
		context.succeed({
			response : response,
		});
	}
}

Credits

Srikanth Nandiraju

Srikanth Nandiraju

1 project • 0 followers
Code is King !
Akshay Krishnaiah

Akshay Krishnaiah

1 project • 0 followers
Global Product Head - PayPal Research Labs by day, entrepreneur by night! Warriors Fan Forever!

Comments