index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. require('dotenv').config()
  2. const Mastodon = require('mastodon-api');
  3. const { OpenWeatherAPI } = require('openweather-api-node');
  4. class BotClima {
  5. mastodon = null;
  6. weather = null;
  7. constructor(mastodon, weather) {
  8. this.mastodon = mastodon;
  9. this.weather = weather;
  10. }
  11. post(message) {
  12. return this.mastodon.post('statuses', { status: message })
  13. .then(console.log)
  14. .catch(console.error);
  15. }
  16. getCurrentWeather() {
  17. return this.weather.getCurrent();
  18. }
  19. }
  20. exports.handler = async (event, context) => {
  21. console.log(event);
  22. const mastodon = new Mastodon({
  23. api_url: process.env.MASTODON_API_URL,
  24. access_token: process.env.MASTODON_ACCESS_TOKEN
  25. });
  26. const weather = new OpenWeatherAPI({
  27. key: process.env.OPENWEATHERAPI_KEY,
  28. language: 'es',
  29. locationName: process.env.OPENWEATHERAPI_CITY || 'Santiago, CL',
  30. units: 'metric'
  31. })
  32. const bot = new BotClima(mastodon, weather);
  33. const data = await bot.getCurrentWeather();
  34. console.log('Data', data);
  35. const message = `
  36. El reporte del clima en ${process.env.OPENWEATHERAPI_CITY} es:\n
  37. La temperatura actual es de ${data.weather.temp.cur}\u00B0C
  38. La humedad es del ${data.weather.humidity}%
  39. Vientos de ${data.weather.wind.speed} m/s
  40. Condición ${data.weather.description.charAt(0).toUpperCase() + data.weather.description.slice(1)}\n ${data.weather.icon.url}
  41. `;
  42. await bot.post(message);
  43. const response = { statusCode: 200, body: JSON.stringify(message) };
  44. return response;
  45. };