index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require("dotenv").config()
  2. const Mastodon = require("mastodon-api");
  3. const { OpenWeatherAPI } = require("openweather-api-node");
  4. const Log = require("./log");
  5. class BotClima {
  6. mastodon = null;
  7. weather = null;
  8. constructor(mastodon, weather) {
  9. this.mastodon = mastodon;
  10. this.weather = weather;
  11. }
  12. post(message) {
  13. return this.mastodon.post("statuses", { status: message })
  14. .then((data) => Log.debug(data))
  15. .catch((err) => Log.error(err));
  16. }
  17. getCurrentWeather() {
  18. return this.weather.getCurrent();
  19. }
  20. getTempEmoji(temp) {
  21. if (temp > 30) { return "🏜️" }
  22. if (temp > 25) { return "🥵" }
  23. if (temp > 20) { return "😎" }
  24. if (temp > 15) { return "🙂" }
  25. if (temp > 10) { return "😐" }
  26. if (temp > 5) { return "🤧" }
  27. if (temp > 0) { return "🥶" }
  28. if (temp > -5 ) { return"🧊" }
  29. if (temp > -10) { return "☃️" }
  30. if (temp > -15) { return "🏔️" }
  31. }
  32. getHumidityEmoji(humidty){
  33. if (humidty > 90) {return "💦💦"}
  34. if (humidty > 60 ){return "💦"}
  35. if (humidty > 30 ){return "💧✅"}
  36. if (humidty > 10 ){return "💧"}
  37. if (humidty > 0 ){return "🏜️🐪"}
  38. }
  39. getWindEmoji(wind) {
  40. if (wind > 0) { return "🍃" }
  41. if (wind > 4) { return "🍃🍃" }
  42. if (wind > 8) { return "🍃🍃🍃" }
  43. if (wind > 12) { return "🌬️" }
  44. if (wind > 19) { return "🌬️🌬️" }
  45. if (wind > 27) { return "🌫️🌬️" }
  46. if (wind > 34) { return "🌫️🌬️🌬️" }
  47. if (wind > 45) { return "🌪️" }
  48. if (wind > 64) { return "🌪️🌪️" }
  49. }
  50. }
  51. exports.handler = async (event, context) => {
  52. Log.debug(event);
  53. const mastodon = new Mastodon({
  54. api_url: process.env.MASTODON_API_URL,
  55. access_token: process.env.MASTODON_ACCESS_TOKEN
  56. });
  57. const weather = new OpenWeatherAPI({
  58. key: process.env.OPENWEATHERAPI_KEY,
  59. language: "es",
  60. locationName: process.env.OPENWEATHERAPI_CITY || "Santiago, CL",
  61. units: "metric"
  62. })
  63. const bot = new BotClima(mastodon, weather);
  64. const data = await bot.getCurrentWeather();
  65. Log.debug("Weather Data", data);
  66. const message = `
  67. El reporte del clima en ${process.env.OPENWEATHERAPI_CITY_DISPLAY} es:\n
  68. La temperatura actual es de ${data.weather.temp.cur}\u00B0C ${bot.getTempEmoji(parseInt(data.weather.temp.cur))}
  69. La humedad es del ${data.weather.humidity}% ${bot.getHumidityEmoji(data.weather.humidity)}
  70. Vientos de ${data.weather.wind.speed} m/s ${bot.getWindEmoji(data.weather.wind.speed)}
  71. ${data.weather.description.charAt(0).toUpperCase() + data.weather.description.slice(1)}
  72. `;
  73. await bot.post(message);
  74. const response = { statusCode: 200, body: JSON.stringify(message) };
  75. return response;
  76. };