chz-telegram-bot 0.0.27 → 0.0.28
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.
- package/dist/entities/actions/scheduledAction.js +1 -1
- package/dist/helpers/inverseRecord.d.ts +2 -0
- package/dist/helpers/inverseRecord.d.ts.map +1 -0
- package/dist/helpers/inverseRecord.js +6 -0
- package/dist/services/telegramApi.d.ts +1 -1
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +9 -11
- package/entities/actions/scheduledAction.ts +1 -1
- package/helpers/{reverseRecord.ts → inverseRecord.ts} +1 -1
- package/package.json +1 -1
- package/services/telegramApi.ts +15 -16
|
@@ -56,7 +56,7 @@ class ScheduledAction {
|
|
|
56
56
|
}
|
|
57
57
|
shouldTrigger(state) {
|
|
58
58
|
const today = (0, moment_1.default)().startOf('day').valueOf();
|
|
59
|
-
const isAllowedToTrigger = (0, moment_1.default)().hour().valueOf() >= this.timeinHours;
|
|
59
|
+
const isAllowedToTrigger = (0, moment_1.default)().add(1, 'minute').hour().valueOf() >= this.timeinHours;
|
|
60
60
|
const hasTriggeredToday = state.lastExecutedDate >= today;
|
|
61
61
|
return isAllowedToTrigger && !hasTriggeredToday;
|
|
62
62
|
}
|
|
@@ -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"}
|
|
@@ -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,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAiB,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAGrD,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAIlE,qBAAa,kBAAkB;IAC3B,UAAU,UAAS;IACnB,YAAY,EAAE,WA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,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
|
|
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,
|
|
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.
|
|
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.
|
|
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;
|
|
@@ -122,7 +122,7 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
122
122
|
const today = moment().startOf('day').valueOf();
|
|
123
123
|
|
|
124
124
|
const isAllowedToTrigger =
|
|
125
|
-
moment().hour().valueOf() >= this.timeinHours;
|
|
125
|
+
moment().add(1, 'minute').hour().valueOf() >= this.timeinHours;
|
|
126
126
|
const hasTriggeredToday = state.lastExecutedDate >= today;
|
|
127
127
|
|
|
128
128
|
return isAllowedToTrigger && !hasTriggeredToday;
|
package/package.json
CHANGED
package/services/telegramApi.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Message } from 'telegraf/types';
|
|
2
2
|
import { ChatContext } from '../entities/context/chatContext';
|
|
3
3
|
import { MessageContext } from '../entities/context/messageContext';
|
|
4
|
-
import {
|
|
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 =
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
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.
|
|
198
|
+
this.interactions,
|
|
200
199
|
chatId,
|
|
201
200
|
this.chats[chatId],
|
|
202
201
|
`Scheduled:${scheduledAction.key}:${chatId}`,
|