Browse Source

cambio de formato del mensaje con emojis

Pablo Barrera Yaksic 2 months ago
parent
commit
b7eee00a9b
6 changed files with 184 additions and 17 deletions
  1. 3 1
      .env.example
  2. BIN
      bot-clima.zip
  3. 51 16
      index.js
  4. 11 0
      log.js
  5. 118 0
      package-lock.json
  6. 1 0
      package.json

+ 3 - 1
.env.example

@@ -1,4 +1,6 @@
 MASTODON_API_URL="https://domain.com/api/v1/"
 MASTODON_ACCESS_TOKEN="your-mastodon-access-token"
 OPENWEATHERAPI_KEY="your-openweatherapi-key"
-OPENWEATHERAPI_CITY="your-openweatherapi-city"
+OPENWEATHERAPI_CITY="your-openweatherapi-city"
+OPENWEATHERAPI_CITY_DISPLAY="your-openweatherapi-display-city"
+LOG_LEVEL="debug"

BIN
bot-clima.zip


+ 51 - 16
index.js

@@ -1,6 +1,7 @@
-require('dotenv').config()
-const Mastodon = require('mastodon-api');
-const { OpenWeatherAPI } = require('openweather-api-node');
+require("dotenv").config()
+const Mastodon = require("mastodon-api");
+const { OpenWeatherAPI } = require("openweather-api-node");
+const Log = require("./log");
 
 class BotClima {
   mastodon = null;
@@ -12,18 +13,51 @@ class BotClima {
   }
 
   post(message) {
-    return this.mastodon.post('statuses', { status: message })
-      .then(console.log)
-      .catch(console.error);
+    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) => {
-  console.log(event);
+  Log.debug(event);
   const mastodon = new Mastodon({
     api_url: process.env.MASTODON_API_URL,
     access_token: process.env.MASTODON_ACCESS_TOKEN
@@ -31,20 +65,21 @@ exports.handler = async (event, context) => {
   
   const weather = new OpenWeatherAPI({
     key: process.env.OPENWEATHERAPI_KEY,
-    language: 'es',
-    locationName: process.env.OPENWEATHERAPI_CITY || 'Santiago, CL',
-    units: 'metric'
+    language: "es",
+    locationName: process.env.OPENWEATHERAPI_CITY || "Santiago, CL",
+    units: "metric"
   })
 
   const bot = new BotClima(mastodon, weather);
   const data = await bot.getCurrentWeather();
-  console.log('Data', data);
+  Log.debug("Weather Data", data);
+
   const message = `
-  El reporte del clima en ${process.env.OPENWEATHERAPI_CITY} es:\n
-  La temperatura actual es de ${data.weather.temp.cur}\u00B0C
-  La humedad es del ${data.weather.humidity}%
-  Vientos de ${data.weather.wind.speed} m/s
-  Condición ${data.weather.description.charAt(0).toUpperCase() + data.weather.description.slice(1)}\n ${data.weather.icon.url}
+  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) };

+ 11 - 0
log.js

@@ -0,0 +1,11 @@
+require("dotenv").config();
+const log4js = require("log4js");
+log4js.configure({
+    appenders: {
+      console: { type: "console" }
+    },
+    categories: {
+      default: { appenders: ["console"], level: process.env.LOG_LEVEL || "info" }
+    }
+});
+module.exports = log4js.getLogger("console");

+ 118 - 0
package-lock.json

@@ -10,6 +10,7 @@
       "license": "ISC",
       "dependencies": {
         "dotenv": "^16.4.5",
+        "log4js": "^6.9.1",
         "mastodon-api": "^1.3.0",
         "openweather-api-node": "^3.1.2"
       }
@@ -399,6 +400,14 @@
         "node": ">=0.10"
       }
     },
+    "node_modules/date-format": {
+      "version": "4.0.14",
+      "resolved": "https://registry.npmjs.org/date-format/-/date-format-4.0.14.tgz",
+      "integrity": "sha512-39BOQLs9ZjKh0/patS9nrT8wc3ioX3/eA/zgbKNopnF2wCqJEoxywwwElATYvRsXdnOxA/OQeQoFZ3rFjVajhg==",
+      "engines": {
+        "node": ">=4.0"
+      }
+    },
     "node_modules/dateformat": {
       "version": "2.2.0",
       "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-2.2.0.tgz",
@@ -867,6 +876,11 @@
         "node": ">=0.10.0"
       }
     },
+    "node_modules/flatted": {
+      "version": "3.3.1",
+      "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.3.1.tgz",
+      "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw=="
+    },
     "node_modules/forever-agent": {
       "version": "0.6.1",
       "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
@@ -888,6 +902,19 @@
         "node": ">= 0.12"
       }
     },
+    "node_modules/fs-extra": {
+      "version": "8.1.0",
+      "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
+      "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
+      "dependencies": {
+        "graceful-fs": "^4.2.0",
+        "jsonfile": "^4.0.0",
+        "universalify": "^0.1.0"
+      },
+      "engines": {
+        "node": ">=6 <7 || >=8"
+      }
+    },
     "node_modules/fs.realpath": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@@ -1329,6 +1356,14 @@
       "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
       "integrity": "sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA=="
     },
+    "node_modules/jsonfile": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
+      "integrity": "sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==",
+      "optionalDependencies": {
+        "graceful-fs": "^4.1.6"
+      }
+    },
     "node_modules/jsonify": {
       "version": "0.0.1",
       "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.1.tgz",
@@ -1479,6 +1514,42 @@
         "lodash.escape": "^3.0.0"
       }
     },
+    "node_modules/log4js": {
+      "version": "6.9.1",
+      "resolved": "https://registry.npmjs.org/log4js/-/log4js-6.9.1.tgz",
+      "integrity": "sha512-1somDdy9sChrr9/f4UlzhdaGfDR2c/SaD2a4T7qEkG4jTS57/B3qmnjLYePwQ8cqWnUHZI0iAKxMBpCZICiZ2g==",
+      "dependencies": {
+        "date-format": "^4.0.14",
+        "debug": "^4.3.4",
+        "flatted": "^3.2.7",
+        "rfdc": "^1.3.0",
+        "streamroller": "^3.1.5"
+      },
+      "engines": {
+        "node": ">=8.0"
+      }
+    },
+    "node_modules/log4js/node_modules/debug": {
+      "version": "4.3.4",
+      "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
+      "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
+      "dependencies": {
+        "ms": "2.1.2"
+      },
+      "engines": {
+        "node": ">=6.0"
+      },
+      "peerDependenciesMeta": {
+        "supports-color": {
+          "optional": true
+        }
+      }
+    },
+    "node_modules/log4js/node_modules/ms": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
+      "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
+    },
     "node_modules/mastodon-api": {
       "version": "1.3.0",
       "resolved": "https://registry.npmjs.org/mastodon-api/-/mastodon-api-1.3.0.tgz",
@@ -1871,6 +1942,11 @@
         "node": ">=0.10.0"
       }
     },
+    "node_modules/rfdc": {
+      "version": "1.3.1",
+      "resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.3.1.tgz",
+      "integrity": "sha512-r5a3l5HzYlIC68TpmYKlxWjmOP6wiPJ1vWv2HeLhNsRZMrCkxeqxiHlQ21oXmQ4F3SiryXBHhAD7JZqvOJjFmg=="
+    },
     "node_modules/rimraf": {
       "version": "2.6.3",
       "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz",
@@ -1983,6 +2059,40 @@
         "node": ">=0.10.0"
       }
     },
+    "node_modules/streamroller": {
+      "version": "3.1.5",
+      "resolved": "https://registry.npmjs.org/streamroller/-/streamroller-3.1.5.tgz",
+      "integrity": "sha512-KFxaM7XT+irxvdqSP1LGLgNWbYN7ay5owZ3r/8t77p+EtSUAfUgtl7be3xtqtOmGUl9K9YPO2ca8133RlTjvKw==",
+      "dependencies": {
+        "date-format": "^4.0.14",
+        "debug": "^4.3.4",
+        "fs-extra": "^8.1.0"
+      },
+      "engines": {
+        "node": ">=8.0"
+      }
+    },
+    "node_modules/streamroller/node_modules/debug": {
+      "version": "4.3.4",
+      "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
+      "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
+      "dependencies": {
+        "ms": "2.1.2"
+      },
+      "engines": {
+        "node": ">=6.0"
+      },
+      "peerDependenciesMeta": {
+        "supports-color": {
+          "optional": true
+        }
+      }
+    },
+    "node_modules/streamroller/node_modules/ms": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
+      "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
+    },
     "node_modules/string_decoder": {
       "version": "1.1.1",
       "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
@@ -2187,6 +2297,14 @@
       "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
       "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA=="
     },
+    "node_modules/universalify": {
+      "version": "0.1.2",
+      "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
+      "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==",
+      "engines": {
+        "node": ">= 4.0.0"
+      }
+    },
     "node_modules/uri-js": {
       "version": "4.4.1",
       "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",

+ 1 - 0
package.json

@@ -19,6 +19,7 @@
   "license": "ISC",
   "dependencies": {
     "dotenv": "^16.4.5",
+    "log4js": "^6.9.1",
     "mastodon-api": "^1.3.0",
     "openweather-api-node": "^3.1.2"
   }