Created
August 31, 2020 14:43
-
-
Save cheapsteak/a53ac28c19076100bd6af7b2fe7e80f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require("lodash"); | |
const got = require("got"); | |
const getHourlyForecast = async () => { | |
const { body } = await got("https://api.climacell.co/v3/weather/forecast/hourly", { | |
method: "GET", | |
searchParams: { | |
lat: process.env.LAT, | |
lon: process.env.LON, | |
unit_system: "si", | |
start_time: "now", | |
fields: [ | |
"temp", | |
"wind_gust:kph", | |
"wind_speed:kph", | |
"wind_direction", | |
"precipitation", | |
"precipitation_type", | |
"precipitation_probability", | |
// "sunset", | |
].join(","), | |
apikey: process.env.CLIMACELL_API_KEY, | |
}, | |
responseType: "json", | |
}); | |
console.log('body', body) | |
return body; | |
}; | |
const timezoneOptions = { | |
timeZone: "America/Toronto", | |
weekday: "long", | |
hour12: true, | |
hour: "numeric", | |
}; | |
const seeMoreHref = `https://widgets-viewer.climacell.co/v1/viewer.html?apikey=${process.env.CLIMACELL_API_KEY}&type=hourly&locationLon=${process.env.LON}&locationLat=${process.env.LAT}&sizeMode=large&fontColor=%23000&backgroundColor=%23fff&fontFamily=verdana&weatherParams=temp%3AC%2Cprecipitation%3Amm%2Fhr%2Cwind_speed%3Akph%2Chumidity%3A%25%2Ccloud_cover%3A%25&precipitationTimeline=true&sunriseSunset=true` | |
const go = async () => { | |
const hourlyForecast = await getHourlyForecast(); | |
const next24hours = hourlyForecast.slice(0, 50); | |
let messages = []; | |
const highWind = next24hours.find( | |
(forecast) => forecast.wind_gust.value > 30 | |
); | |
const highestWind = _.maxBy(next24hours, (x) => x.wind_gust.value); | |
if (highWind) { | |
const windMessage = `Wind gusts of ${Math.round( | |
highWind.wind_gust.value | |
)}km/h expected ${new Date( | |
highWind.observation_time.value | |
).toLocaleDateString("en-CA", timezoneOptions)}.`; | |
messages.push(windMessage); | |
} | |
if ( | |
highWind && | |
highWind.wind_gust.value !== highestWind.wind_gust.value | |
) { | |
messages.push( | |
`Reaching ${Math.round(highestWind.wind_gust.value)}km/h ${new Date( | |
highestWind.observation_time.value | |
).toLocaleDateString("en-CA", timezoneOptions)}` | |
); | |
} | |
const mightPrecipitate = next24hours.find( | |
(forecast) => forecast.precipitation_probability.value > 50 | |
); | |
if (mightPrecipitate) { | |
const precipitationMessage = `${ | |
mightPrecipitate.precipitation_probability.value | |
}% chance of ${mightPrecipitate.precipitation_type.value} ${new Date( | |
mightPrecipitate.observation_time.value | |
).toLocaleDateString("en-CA", timezoneOptions)}`; | |
messages.push(precipitationMessage); | |
} | |
if (messages.length) { | |
got(process.env.WEATHER_SLACK_WEBHOOK, { | |
method: "POST", | |
body: JSON.stringify({ | |
username: "天气咋地", | |
text: messages.join("\n"), | |
blocks: [ | |
{ | |
type: 'context', | |
elements: [ | |
{ | |
type: "plain_text", | |
text: messages.join("\n"), | |
}, | |
] | |
}, | |
{ type: 'actions', elements: [ | |
{ | |
type: 'button', | |
text: { | |
type: "plain_text", | |
text: "see more" | |
}, | |
action_id: 'see more weather', | |
url: seeMoreHref | |
} | |
]} | |
], | |
icon_emoji: mightPrecipitate | |
? ":rain_cloud:" | |
: highWind | |
? ":wind_blowing_face:" | |
: ":thermometer:", | |
}), | |
}); | |
} | |
return messages; | |
}; | |
exports.endpoint = async (request, response) => { | |
try { | |
const messages = await go(); | |
response.writeHead(200); | |
response.end(JSON.stringify(messages)); | |
} catch (e) { | |
response.writeHead(500); | |
response.end(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment