浏览代码

remindme message localized to America/Santiago TZ

Pablo B.Y 2 周之前
父节点
当前提交
33917a3715
共有 5 个文件被更改,包括 31 次插入2 次删除
  1. 2 0
      .env.example
  2. 21 0
      package-lock.json
  3. 1 0
      package.json
  4. 5 2
      src/agents/remindme/index.ts
  5. 2 0
      src/config.ts

+ 2 - 0
.env.example

@@ -6,6 +6,8 @@ MASTODON_API_URL = "https://<mastodon-domain>/api/v1/"
 MASTODON_STREAMING_URL = "wss://<mastodon-domain>/api/v1/streaming"
 MASTODON_ACCESS_TOKEN = "<access-token>"
 IMG_PLACEHOLDER = "https://placehold.co/600x400"
+DEFAULT_TIMEZONE = "America/Santiago"
+DEFAULT_LOCALE = "es-CL"
 
 CHILECULTURA = "https://chilecultura.gob.cl/events/"
 CIPER = "https://www.ciperchile.cl/actualidad/"

+ 21 - 0
package-lock.json

@@ -11,6 +11,7 @@
         "axios": "^1.7.9",
         "cheerio": "^1.0.0-rc.12",
         "chrono-node": "^2.9.0",
+        "date-fns-tz": "^3.2.0",
         "dotenv": "^16.4.7",
         "masto": "^6.10.1",
         "redis": "^4.7.0"
@@ -942,6 +943,26 @@
         "url": "https://github.com/sponsors/fb55"
       }
     },
+    "node_modules/date-fns": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.1.0.tgz",
+      "integrity": "sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg==",
+      "license": "MIT",
+      "peer": true,
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/kossnocorp"
+      }
+    },
+    "node_modules/date-fns-tz": {
+      "version": "3.2.0",
+      "resolved": "https://registry.npmjs.org/date-fns-tz/-/date-fns-tz-3.2.0.tgz",
+      "integrity": "sha512-sg8HqoTEulcbbbVXeg84u5UnlsQa8GS5QXMqjjYIhS4abEVVKIUwe0/l/UhrZdKaL/W5eWZNlbTeEIiOXTcsBQ==",
+      "license": "MIT",
+      "peerDependencies": {
+        "date-fns": "^3.0.0 || ^4.0.0"
+      }
+    },
     "node_modules/debug": {
       "version": "4.4.3",
       "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",

+ 1 - 0
package.json

@@ -27,6 +27,7 @@
     "axios": "^1.7.9",
     "cheerio": "^1.0.0-rc.12",
     "chrono-node": "^2.9.0",
+    "date-fns-tz": "^3.2.0",
     "dotenv": "^16.4.7",
     "masto": "^6.10.1",
     "redis": "^4.7.0"

+ 5 - 2
src/agents/remindme/index.ts

@@ -1,4 +1,5 @@
 import { createStreamingAPIClient, createRestAPIClient, mastodon } from "masto";
+import { toZonedTime } from "date-fns-tz";
 import * as chrono from "chrono-node";
 
 import config from "../../config";
@@ -29,7 +30,7 @@ export default class Remindme {
   }
 
   private parseTime(message: string): Array<chrono.ParsedResult> {
-    const parsed = chrono.es.parse(message);
+    const parsed = chrono.es.parse(message, new Date(), { forwardDate: true });
 
     if (!parsed) {
       throw new Error(`No pude entender el recordatorio ${Emojis.SAD_FACE}`);
@@ -85,8 +86,10 @@ export default class Remindme {
                 console.log("Reminder Payload", payload);
               }
 
+	      const parsedTimeToDefaultTZ = toZonedTime(parsedTime[0].date(), config.DEFAULT_TIMEZONE);  
+
               await this.scheduleReminder(parsedTime[0].date().getTime(), payload);
-              status = `@${event.payload.account.acct} \nRecordatorio establecido ${Emojis.CALENDAR}${Emojis.CHECK} \nTe avisaré el ${parsedTime[0].date().toLocaleString()}`;
+              status = `@${event.payload.account.acct} \nRecordatorio establecido ${Emojis.CALENDAR}${Emojis.CHECK} \nTe avisaré el ${parsedTimeToDefaultTZ.toLocaleString(config.DEFAULT_LOCALE)}`;
 
             } catch (error: any) {
               status = error.message;

+ 2 - 0
src/config.ts

@@ -8,6 +8,8 @@ const config = {
   MASTODON_API_URL: process.env.MASTODON_API_URL ?? "https://mastodon.cl/api/v1/",
   MASTODON_STREAMING_URL: process.env.MASTODON_STREAMING_URL ?? "wss://mastodon.cl/api/v1/streaming",
   IMG_PLACEHOLDER: process.env.IMG_PLACEHOLDER ?? "https://placehold.co/600x400",
+  DEFAULT_TIMEZONE: process.env.DEFAULT_TIMEZONE ?? "America/Santiago",
+  DEFAULT_LOCALE: process.env.DEFAULT_LOCALE ?? "es-CL",
   // PORTALES
   CHILECULTURA: process.env.CHILECULTURA ?? "https://chilecultura.gob.cl/",
   CIPER: process.env.CIPER ?? "https://www.ciperchile.cl/actualidad/",