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; };