chz-telegram-bot 0.0.27 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"commandAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/commandAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAGjD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE1E,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAI3D,qBAAa,aAAa,CAAC,YAAY,SAAS,YAAY,CACxD,YAAW,gBAAgB;IAE3B,QAAQ,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAC9B,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,iBAAiB,EAAE,OAAO,CAAC;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAC1C,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,GAAG,EAAE,SAAS,CAAC;gBAGX,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,EAC9C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,OAAO,EACjB,cAAc,EAAE,MAAM,EAAE,EACxB,YAAY,EAAE,MAAM,E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AAc,EAAE,MAAM,EAAE,EACxB,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACzC,gBAAgB,EAAE,MAAM,YAAY;IAelC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;IAgD5C,OAAO,CAAC,YAAY;CAmDvB"}
@@ -54,9 +54,9 @@ class CommandAction {
54
54
  return commandTriggerCheckResult_1.CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
55
55
  const isUserAllowed = this.allowedUsers.length == 0 ||
56
56
  this.allowedUsers.includes(ctx.fromUserId);
57
+ const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
57
58
  const cooldownInMilliseconds = (0, timeConvertions_1.secondsToMilliseconds)(this.cooldownInSeconds);
58
- const notOnCooldown = (0, moment_1.default)().valueOf() - state.lastExecutedDate >=
59
- cooldownInMilliseconds;
59
+ const notOnCooldown = (0, moment_1.default)().diff(lastExecutedDate) >= cooldownInMilliseconds;
60
60
  if (isUserAllowed && notOnCooldown) {
61
61
  if (typeof trigger == 'string') {
62
62
  shouldTrigger = ctx.messageText.toLowerCase() == trigger;
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAKrD,qBAAa,eAAe,CAAC,YAAY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CAAC,YAAY,SAAS,YAAY,CAC1D,YAAW,gBAAgB;IAE3B,MAAM,CAAC,SAAS,YAAoB;IAEpC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,GAAG,EAAE,SAAS,CAAC;IAEf,WAAW,uBAA8B;IACzC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACtD,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAGpC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACvC,WAAW,EAAE,UAAU,EACvB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EAAE,EACnB,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,EACrD,gBAAgB,EAAE,MAAM,YAAY;IAYlC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC;YAmC3B,cAAc;IAqC5B,OAAO,CAAC,aAAa;CAaxB"}
@@ -55,9 +55,14 @@ class ScheduledAction {
55
55
  }
56
56
  }
57
57
  shouldTrigger(state) {
58
- const today = (0, moment_1.default)().startOf('day').valueOf();
59
- const isAllowedToTrigger = (0, moment_1.default)().hour().valueOf() >= this.timeinHours;
60
- const hasTriggeredToday = state.lastExecutedDate >= today;
58
+ const startOfToday = (0, moment_1.default)().startOf('day').valueOf();
59
+ const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
60
+ const currentTime = (0, moment_1.default)();
61
+ const scheduledTime = (0, moment_1.default)()
62
+ .startOf('day')
63
+ .add(this.timeinHours, 'hours');
64
+ const isAllowedToTrigger = currentTime.isSameOrAfter(scheduledTime);
65
+ const hasTriggeredToday = lastExecutedDate.isAfter(startOfToday);
61
66
  return isAllowedToTrigger && !hasTriggeredToday;
62
67
  }
63
68
  }
@@ -0,0 +1,2 @@
1
+ export declare function inverseRecord<T extends PropertyKey, U extends PropertyKey>(input: Record<T, U>): Record<U, T>;
2
+ //# sourceMappingURL=inverseRecord.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inverseRecord.d.ts","sourceRoot":"","sources":["../../helpers/inverseRecord.ts"],"names":[],"mappings":"AAAA,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,SAAS,WAAW,EACtE,KAAK,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAId,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CACpB"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.inverseRecord = inverseRecord;
4
+ function inverseRecord(input) {
5
+ return Object.fromEntries(Object.entries(input).map(([key, value]) => [value, key]));
6
+ }
@@ -19,12 +19,12 @@ export declare class TelegramApiService {
19
19
  telegram: Telegram;
20
20
  chats: Record<number, string>;
21
21
  storage: IStorageClient;
22
+ interactions: IBotApiInteractions;
22
23
  constructor(botName: string, telegram: Telegram, storage: IStorageClient, chats: Record<string, number>);
23
24
  flushResponses(): Promise<void>;
24
25
  private pinIfShould;
25
26
  private processResponse;
26
27
  private enqueue;
27
- private getInteractions;
28
28
  createContextForMessage<TActionState extends IActionState>(incomingMessage: IncomingMessage, command: CommandAction<TActionState>): MessageContext<TActionState>;
29
29
  createContextForChat<TActionState extends IActionState>(chatId: number, scheduledAction: ScheduledAction<TActionState>): ChatContext<TActionState>;
30
30
  }
@@ -1 +1 @@
1
- {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,MA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MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,OAAO,EAAE,cAAc,CAAC;IACxB,YAAY,EAAE,mBAAmB,CAAC;gBAG9B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAc3B,cAAc;YA2BN,WAAW;YAqBX,eAAe;IA2E7B,OAAO,CAAC,OAAO;IAIf,uBAAuB,CAAC,YAAY,SAAS,YAAY,EACrD,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,aAAa,CAAC,YAAY,CAAC;IAWxC,oBAAoB,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,MAAM,EACd,eAAe,EAAE,eAAe,CAAC,YAAY,CAAC;CAYrD;AAED,MAAM,WAAW,mBAAmB;IAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,WAAW,GAAG,YAAY,GAAG,YAAY,KAAK,IAAI,CAAC;IACvE,KAAK,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IACpC,KAAK,EAAE,CAAC,YAAY,EAAE,aAAa,KAAK,IAAI,CAAC;CAChD"}
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TelegramApiService = void 0;
4
4
  const chatContext_1 = require("../entities/context/chatContext");
5
5
  const messageContext_1 = require("../entities/context/messageContext");
6
- const reverseRecord_1 = require("../helpers/reverseRecord");
6
+ const inverseRecord_1 = require("../helpers/inverseRecord");
7
7
  const logger_1 = require("./logger");
8
8
  const promises_1 = require("timers/promises");
9
9
  const TELEGRAM_RATELIMIT_DELAY = 35;
@@ -13,8 +13,13 @@ class TelegramApiService {
13
13
  this.messageQueue = [];
14
14
  this.telegram = telegram;
15
15
  this.botName = botName;
16
- this.chats = (0, reverseRecord_1.reverseRecord)(chats);
16
+ this.chats = (0, inverseRecord_1.inverseRecord)(chats);
17
17
  this.storage = storage;
18
+ this.interactions = {
19
+ react: (reaction) => this.enqueue(reaction),
20
+ respond: (response) => this.enqueue(response),
21
+ unpin: (unpinMessage) => this.enqueue(unpinMessage)
22
+ };
18
23
  }
19
24
  async flushResponses() {
20
25
  if (this.isFlushing)
@@ -87,18 +92,11 @@ class TelegramApiService {
87
92
  enqueue(response) {
88
93
  this.messageQueue.push(response);
89
94
  }
90
- getInteractions() {
91
- return {
92
- react: (reaction) => this.enqueue(reaction),
93
- respond: (response) => this.enqueue(response),
94
- unpin: (unpinMessage) => this.enqueue(unpinMessage)
95
- };
96
- }
97
95
  createContextForMessage(incomingMessage, command) {
98
- return new messageContext_1.MessageContext(this.botName, command, this.getInteractions(), incomingMessage, this.storage);
96
+ return new messageContext_1.MessageContext(this.botName, command, this.interactions, incomingMessage, this.storage);
99
97
  }
100
98
  createContextForChat(chatId, scheduledAction) {
101
- return new chatContext_1.ChatContext(this.botName, scheduledAction, this.getInteractions(), chatId, this.chats[chatId], `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
99
+ return new chatContext_1.ChatContext(this.botName, scheduledAction, this.interactions, chatId, this.chats[chatId], `Scheduled:${scheduledAction.key}:${chatId}`, this.storage);
102
100
  }
103
101
  }
104
102
  exports.TelegramApiService = TelegramApiService;
@@ -111,12 +111,13 @@ export class CommandAction<TActionState extends IActionState>
111
111
  const isUserAllowed =
112
112
  this.allowedUsers.length == 0 ||
113
113
  this.allowedUsers.includes(ctx.fromUserId);
114
+
115
+ const lastExecutedDate = moment(state.lastExecutedDate);
114
116
  const cooldownInMilliseconds = secondsToMilliseconds(
115
117
  this.cooldownInSeconds
116
118
  );
117
119
  const notOnCooldown =
118
- moment().valueOf() - state.lastExecutedDate >=
119
- cooldownInMilliseconds;
120
+ moment().diff(lastExecutedDate) >= cooldownInMilliseconds;
120
121
 
121
122
  if (isUserAllowed && notOnCooldown) {
122
123
  if (typeof trigger == 'string') {
@@ -119,11 +119,15 @@ export class ScheduledAction<TActionState extends IActionState>
119
119
  }
120
120
 
121
121
  private shouldTrigger(state: IActionState): boolean {
122
- const today = moment().startOf('day').valueOf();
123
-
124
- const isAllowedToTrigger =
125
- moment().hour().valueOf() >= this.timeinHours;
126
- const hasTriggeredToday = state.lastExecutedDate >= today;
122
+ const startOfToday = moment().startOf('day').valueOf();
123
+ const lastExecutedDate = moment(state.lastExecutedDate);
124
+ const currentTime = moment();
125
+ const scheduledTime = moment()
126
+ .startOf('day')
127
+ .add(this.timeinHours, 'hours');
128
+
129
+ const isAllowedToTrigger = currentTime.isSameOrAfter(scheduledTime);
130
+ const hasTriggeredToday = lastExecutedDate.isAfter(startOfToday);
127
131
 
128
132
  return isAllowedToTrigger && !hasTriggeredToday;
129
133
  }
@@ -1,4 +1,4 @@
1
- export function reverseRecord<T extends PropertyKey, U extends PropertyKey>(
1
+ export function inverseRecord<T extends PropertyKey, U extends PropertyKey>(
2
2
  input: Record<T, U>
3
3
  ) {
4
4
  return Object.fromEntries(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -1,7 +1,7 @@
1
- import { InputFile, Message } from 'telegraf/types';
1
+ import { Message } from 'telegraf/types';
2
2
  import { ChatContext } from '../entities/context/chatContext';
3
3
  import { MessageContext } from '../entities/context/messageContext';
4
- import { reverseRecord } from '../helpers/reverseRecord';
4
+ import { inverseRecord as inverseRecord } from '../helpers/inverseRecord';
5
5
  import { IStorageClient } from '../types/storage';
6
6
  import { Logger } from './logger';
7
7
  import { Reaction } from '../entities/responses/reaction';
@@ -28,6 +28,7 @@ export class TelegramApiService {
28
28
  telegram: Telegram;
29
29
  chats: Record<number, string>;
30
30
  storage: IStorageClient;
31
+ interactions: IBotApiInteractions;
31
32
 
32
33
  constructor(
33
34
  botName: string,
@@ -37,8 +38,14 @@ export class TelegramApiService {
37
38
  ) {
38
39
  this.telegram = telegram;
39
40
  this.botName = botName;
40
- this.chats = reverseRecord(chats);
41
+ this.chats = inverseRecord(chats);
41
42
  this.storage = storage;
43
+
44
+ this.interactions = {
45
+ react: (reaction) => this.enqueue(reaction),
46
+ respond: (response) => this.enqueue(response),
47
+ unpin: (unpinMessage) => this.enqueue(unpinMessage)
48
+ } as IBotApiInteractions;
42
49
  }
43
50
 
44
51
  async flushResponses() {
@@ -96,7 +103,7 @@ export class TelegramApiService {
96
103
  case 'text':
97
104
  sentMessage = await this.telegram.sendMessage(
98
105
  response.chatId,
99
- response.content as string,
106
+ response.content,
100
107
  {
101
108
  reply_to_message_id: response.replyId,
102
109
  parse_mode: 'MarkdownV2',
@@ -110,7 +117,7 @@ export class TelegramApiService {
110
117
  case 'image':
111
118
  sentMessage = await this.telegram.sendPhoto(
112
119
  response.chatId,
113
- response.content as InputFile,
120
+ response.content,
114
121
  response.replyId
115
122
  ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
116
123
  ({ reply_to_message_id: response.replyId } as any)
@@ -122,7 +129,7 @@ export class TelegramApiService {
122
129
  case 'video':
123
130
  sentMessage = await this.telegram.sendVideo(
124
131
  response.chatId,
125
- response.content as InputFile,
132
+ response.content,
126
133
  response.replyId
127
134
  ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
128
135
  ({ reply_to_message_id: response.replyId } as any)
@@ -168,14 +175,6 @@ export class TelegramApiService {
168
175
  this.messageQueue.push(response);
169
176
  }
170
177
 
171
- private getInteractions() {
172
- return {
173
- react: (reaction) => this.enqueue(reaction),
174
- respond: (response) => this.enqueue(response),
175
- unpin: (unpinMessage) => this.enqueue(unpinMessage)
176
- } as IBotApiInteractions;
177
- }
178
-
179
178
  createContextForMessage<TActionState extends IActionState>(
180
179
  incomingMessage: IncomingMessage,
181
180
  command: CommandAction<TActionState>
@@ -183,7 +182,7 @@ export class TelegramApiService {
183
182
  return new MessageContext<TActionState>(
184
183
  this.botName,
185
184
  command,
186
- this.getInteractions(),
185
+ this.interactions,
187
186
  incomingMessage,
188
187
  this.storage
189
188
  );
@@ -196,7 +195,7 @@ export class TelegramApiService {
196
195
  return new ChatContext<TActionState>(
197
196
  this.botName,
198
197
  scheduledAction,
199
- this.getInteractions(),
198
+ this.interactions,
200
199
  chatId,
201
200
  this.chats[chatId],
202
201
  `Scheduled:${scheduledAction.key}:${chatId}`,