123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- require("dotenv").config()
- const Mastodon = require("mastodon-api");
- const { OpenWeatherAPI } = require("openweather-api-node");
- const Log = require("./log");
- class BotClima {
- mastodon = null;
- weather = null;
- constructor(mastodon, weather) {
- this.mastodon = mastodon;
- this.weather = weather;
- }
- post(message) {
- return this.mastodon.post("statuses", { status: message })
- .then((data) => Log.debug(data))
- .catch((err) => Log.error(err));
- }
- getCurrentWeather() {
- return this.weather.getCurrent();
- }
- getTempEmoji(temp) {
- if (temp > 30) { return "🏜️" }
- if (temp > 25) { return "🥵" }
- if (temp > 20) { return "😎" }
- if (temp > 15) { return "🙂" }
- if (temp > 10) { return "😐" }
- if (temp > 5) { return "🤧" }
- if (temp > 0) { return "🥶" }
- if (temp > -5 ) { return"🧊" }
- if (temp > -10) { return "☃️" }
- if (temp > -15) { return "🏔️" }
- }
- getHumidityEmoji(humidty){
- if (humidty > 90) {return "🍹🌴"}
- if (humidty > 60 ){return "💦"}
- if (humidty > 30 ){return "💧✅"}
- if (humidty > 10 ){return "💧"}
- if (humidty > 0 ){return "🏜️🐪"}
- }
- getWindEmoji(wind) {
- if (wind > 0) { return "🍃" }
- if (wind > 4) { return "🌿" }
- if (wind > 8) { return "🎋" }
- if (wind > 12) { return "🌬️" }
- if (wind > 19) { return "🌬️🌬️" }
- if (wind > 27) { return "🌫️🌬️" }
- if (wind > 34) { return "🌫️🌬️🌬️" }
- if (wind > 45) { return "🌪️" }
- if (wind > 64) { return "🌪️🌪️" }
- }
- }
- exports.handler = async (event, context) => {
- Log.debug(event);
- const mastodon = new Mastodon({
- api_url: process.env.MASTODON_API_URL,
- access_token: process.env.MASTODON_ACCESS_TOKEN
- });
-
- const weather = new OpenWeatherAPI({
- key: process.env.OPENWEATHERAPI_KEY,
- language: "es",
- locationName: process.env.OPENWEATHERAPI_CITY || "Santiago, CL",
- units: "metric"
- })
- const bot = new BotClima(mastodon, weather);
- const data = await bot.getCurrentWeather();
- Log.debug("Weather Data", data);
- const message = `
- El reporte del clima en ${process.env.OPENWEATHERAPI_CITY_DISPLAY} es:\n
- La temperatura actual es de ${data.weather.temp.cur}\u00B0C ${bot.getTempEmoticon(parseInt(data.weather.temp.cur))}
- La humedad es del ${data.weather.humidity}% ${bot.getHumidityEmoticon(data.weather.humidity)}
- Vientos de ${data.weather.wind.speed} m/s ${bot.getWindEmoticon(data.weather.wind.speed)}
- ${data.weather.description.charAt(0).toUpperCase() + data.weather.description.slice(1)}
- `;
- await bot.post(message);
- const response = { statusCode: 200, body: JSON.stringify(message) };
- return response;
- };
|