Sending a Weather Forecast with an AWS Lambda and Twilio

The Overall Flow

  1. Lambda is triggered by a CloudWatch scheduled cron job
  2. Lambda calls the OpenWeatherMap API for current conditions
  3. Lambda calls the NOAA API for forecast data
  4. A message is created with all of the content
  5. A text message is sent via Twilio’s APIs

Creating the AWS Lambda and ClaudiaJS

Getting the Weather Forecast

const APIKey = process.env.OPEN_WEATHER_MAP_API_KEY;
const latitude = process.env.LATITUDE;
const longitude = process.env.LONGITUDE;
const units = 'imperial';

// OpenWeatherMapAPI
const openWeatherMapAPIURL = 'https://api.openweathermap.org/data/2.5/weather?lat=' + latitude + '&lon='
+ longitude + '&units=' + units + '&appid=' + APIKey;
const currentWeather = await axios.get(openWeatherMapAPIURL)
.catch((error) => {
console.log(error);
return;
});

// NOAA Metadata
const NOAAMetadata = await axios.get('https://api.weather.gov/points/' + latitude + ',' + longitude)
.catch((error) => {
console.log(error);
return;
});

// NOAA Weekly
const NOAAWeeklyForecast = await axios.get(NOAAMetadata.data.properties.forecast)
.catch((error) => {
console.log(error);
return;
});

// NOAA Hourly
const NOAAHourlyForecast = await axios.get(NOAAMetadata.data.properties.forecastHourly)
.catch((error) => {
console.log(error);
return;
});
const hoursToday = retrieveHours(NOAAHourlyForecast.data.properties.periods);

let highTemp = 0;
hoursToday.forEach((period) => {
if(parseInt(period.temperature) > highTemp) {
highTemp = period.temperature;
}
});

let lowTemp = highTemp;
hoursToday.forEach((period) => {
if(parseInt(period.temperature) < lowTemp) {
lowTemp = period.temperature;
}
});

const sunrise = formatTime(currentWeather.data.sys.sunrise);
const sunset = formatTime(currentWeather.data.sys.sunset);
const message =
'WEATHER TEXT:\n'
+ '\n'
+ 'Good Morning! ☀️ 💦 🌤 ⛈ \n'
+ 'Here\'s the lowdown for today...\n'
+ '\n'
// to show degree symbol on OSX hold shift + option + 8
+ 'temp: ' + currentWeather.data.main.temp.toFixed(0) + '°\n'
+ 'high: ' + highTemp.toString() + '°\n'
+ 'low: ' + lowTemp.toString() + '°\n'
+ 'wind: ' + currentWeather.data.wind.speed.toFixed(0) + ' MPH\n'
+ 'sunrise: ' + sunrise + ' AM\n'
+ 'sunset: ' + sunset + ' PM\n'
+ '\n'
+ 'forecast: ' + NOAAWeeklyForecast.data.properties.periods[0].detailedForecast + '\n'
+ '\n'
+ 'Have a good day! 🎉🎉 🎉 🎉';

Sending the actual Message

let response = 'lambda completed with ';

await client.messages
.create({
body: message,
from: process.env.TWILIO_FROM,
to: process.env.TWILIO_TO
})
.then((success) => {
console.log(success.sid);
response = response + 'success';
})
.catch((error) => {
console.log(error);
response = response + ' error';
});

return response;

Calling the Lambda

Closing Thoughts

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store