chz-telegram-bot 0.0.1

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.
Files changed (83) hide show
  1. package/build.ps1 +34 -0
  2. package/bun.lock +506 -0
  3. package/dist/entities/actions/commandAction.js +79 -0
  4. package/dist/entities/actions/scheduledAction.js +65 -0
  5. package/dist/entities/bot.js +75 -0
  6. package/dist/entities/cachedStateFactory.js +9 -0
  7. package/dist/entities/commandTriggerCheckResult.js +22 -0
  8. package/dist/entities/context/chatContext.js +30 -0
  9. package/dist/entities/context/messageContext.js +46 -0
  10. package/dist/entities/incomingMessage.js +20 -0
  11. package/dist/entities/responses/imageMessage.js +11 -0
  12. package/dist/entities/responses/reaction.js +11 -0
  13. package/dist/entities/responses/textMessage.js +11 -0
  14. package/dist/entities/responses/videoMessage.js +11 -0
  15. package/dist/entities/states/actionStateBase.js +8 -0
  16. package/dist/entities/states/potuzhnoState.js +13 -0
  17. package/dist/entities/taskRecord.js +10 -0
  18. package/dist/entities/transactionResult.js +9 -0
  19. package/dist/helpers/builders/commandActionBuilder.js +61 -0
  20. package/dist/helpers/builders/scheduledActionBuilder.js +42 -0
  21. package/dist/helpers/escapeMarkdown.js +17 -0
  22. package/dist/helpers/getWeek.js +12 -0
  23. package/dist/helpers/noop.js +13 -0
  24. package/dist/helpers/randomInt.js +8 -0
  25. package/dist/helpers/reverseMap.js +6 -0
  26. package/dist/helpers/timeConvertions.js +14 -0
  27. package/dist/helpers/toArray.js +6 -0
  28. package/dist/index.js +33 -0
  29. package/dist/services/logger.js +17 -0
  30. package/dist/services/storage.js +79 -0
  31. package/dist/services/taskScheduler.js +37 -0
  32. package/dist/services/telegramApi.js +94 -0
  33. package/dist/types/actionState.js +2 -0
  34. package/dist/types/actionWithState.js +2 -0
  35. package/dist/types/cachedValueAccessor.js +2 -0
  36. package/dist/types/commandCondition.js +2 -0
  37. package/dist/types/daysOfTheWeek.js +13 -0
  38. package/dist/types/handlers.js +2 -0
  39. package/dist/types/replyMessage.js +2 -0
  40. package/dist/types/scheduledItem.js +2 -0
  41. package/dist/types/timeValues.js +2 -0
  42. package/entities/actions/commandAction.ts +143 -0
  43. package/entities/actions/scheduledAction.ts +121 -0
  44. package/entities/bot.ts +143 -0
  45. package/entities/cachedStateFactory.ts +14 -0
  46. package/entities/commandTriggerCheckResult.ts +30 -0
  47. package/entities/context/chatContext.ts +57 -0
  48. package/entities/context/messageContext.ts +94 -0
  49. package/entities/incomingMessage.ts +27 -0
  50. package/entities/responses/imageMessage.ts +21 -0
  51. package/entities/responses/reaction.ts +20 -0
  52. package/entities/responses/textMessage.ts +20 -0
  53. package/entities/responses/videoMessage.ts +21 -0
  54. package/entities/states/actionStateBase.ts +5 -0
  55. package/entities/states/potuzhnoState.ts +5 -0
  56. package/entities/taskRecord.ts +13 -0
  57. package/entities/transactionResult.ts +11 -0
  58. package/eslint.config.js +10 -0
  59. package/helpers/builders/commandActionBuilder.ts +88 -0
  60. package/helpers/builders/scheduledActionBuilder.ts +67 -0
  61. package/helpers/escapeMarkdown.ts +12 -0
  62. package/helpers/getWeek.ts +8 -0
  63. package/helpers/noop.ts +13 -0
  64. package/helpers/randomInt.ts +7 -0
  65. package/helpers/reverseMap.ts +3 -0
  66. package/helpers/timeConvertions.ts +13 -0
  67. package/helpers/toArray.ts +3 -0
  68. package/index.ts +47 -0
  69. package/package.json +30 -0
  70. package/services/logger.ts +30 -0
  71. package/services/storage.ts +111 -0
  72. package/services/taskScheduler.ts +66 -0
  73. package/services/telegramApi.ts +172 -0
  74. package/tsconfig.json +110 -0
  75. package/types/actionState.ts +3 -0
  76. package/types/actionWithState.ts +6 -0
  77. package/types/cachedValueAccessor.ts +1 -0
  78. package/types/commandCondition.ts +6 -0
  79. package/types/daysOfTheWeek.ts +9 -0
  80. package/types/handlers.ts +14 -0
  81. package/types/replyMessage.ts +6 -0
  82. package/types/scheduledItem.ts +6 -0
  83. package/types/timeValues.ts +29 -0
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const storage_1 = __importDefault(require("../../services/storage"));
7
+ const transactionResult_1 = __importDefault(require("../transactionResult"));
8
+ const moment_1 = __importDefault(require("moment"));
9
+ const logger_1 = __importDefault(require("../../services/logger"));
10
+ const toArray_1 = __importDefault(require("../../helpers/toArray"));
11
+ const commandTriggerCheckResult_1 = __importDefault(require("../commandTriggerCheckResult"));
12
+ const timeConvertions_1 = require("../../helpers/timeConvertions");
13
+ class CommandAction {
14
+ constructor(trigger, handler, name, active, cooldown, chatsBlacklist, allowedUsers, condition, stateConstructor) {
15
+ this.triggers = (0, toArray_1.default)(trigger);
16
+ this.handler = handler;
17
+ this.name = name;
18
+ this.cooldownInSeconds = cooldown;
19
+ this.active = active;
20
+ this.chatsBlacklist = chatsBlacklist;
21
+ this.allowedUsers = allowedUsers;
22
+ this.condition = condition;
23
+ this.stateConstructor = stateConstructor;
24
+ this.key = `command:${this.name.replace('.', '-')}`;
25
+ }
26
+ async exec(ctx) {
27
+ if (!this.active || this.chatsBlacklist.includes(ctx.chatId))
28
+ return;
29
+ const isConditionMet = await this.condition(ctx);
30
+ if (!isConditionMet)
31
+ return;
32
+ const state = await storage_1.default.getActionState(this, ctx.chatId);
33
+ const { shouldTrigger, matchResults, skipCooldown } = this.triggers
34
+ .map((x) => this.checkTrigger(ctx, x, state))
35
+ .reduce((acc, curr) => acc.mergeWith(curr), commandTriggerCheckResult_1.default.DoNotTrigger);
36
+ if (!shouldTrigger)
37
+ return;
38
+ logger_1.default.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, ` - Executing [${this.name}] in ${ctx.chatId}`);
39
+ ctx.matchResults = matchResults;
40
+ await this.handler(ctx, state);
41
+ if (skipCooldown) {
42
+ ctx.startCooldown = false;
43
+ }
44
+ if (ctx.startCooldown) {
45
+ state.lastExecutedDate = (0, moment_1.default)().valueOf();
46
+ }
47
+ ctx.updateActions.forEach((action) => action(state));
48
+ await storage_1.default.commitTransactionForAction(this, ctx.chatId, new transactionResult_1.default(state, ctx.startCooldown && shouldTrigger));
49
+ }
50
+ checkTrigger(ctx, trigger, state) {
51
+ let shouldTrigger = false;
52
+ const matchResults = [];
53
+ if (!ctx.fromUserId)
54
+ return commandTriggerCheckResult_1.default.DontTriggerAndSkipCooldown;
55
+ const isUserAllowed = this.allowedUsers.length == 0 ||
56
+ this.allowedUsers.includes(ctx.fromUserId);
57
+ const cooldownInMilliseconds = (0, timeConvertions_1.secondsToMilliseconds)(this.cooldownInSeconds);
58
+ const notOnCooldown = (0, moment_1.default)().valueOf() - state.lastExecutedDate >=
59
+ cooldownInMilliseconds;
60
+ if (isUserAllowed && notOnCooldown) {
61
+ if (typeof trigger == 'string') {
62
+ shouldTrigger = ctx.messageText.toLowerCase() == trigger;
63
+ }
64
+ else {
65
+ let matchCount = ctx.messageText.match(trigger)?.length ?? 0;
66
+ if (matchCount > 0) {
67
+ for (; matchCount > 0; matchCount--) {
68
+ const execResult = trigger.exec(ctx.messageText);
69
+ if (execResult)
70
+ matchResults.push(execResult);
71
+ }
72
+ shouldTrigger = true;
73
+ }
74
+ }
75
+ }
76
+ return new commandTriggerCheckResult_1.default(shouldTrigger, matchResults, !isUserAllowed);
77
+ }
78
+ }
79
+ exports.default = CommandAction;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const storage_1 = __importDefault(require("../../services/storage"));
7
+ const transactionResult_1 = __importDefault(require("../transactionResult"));
8
+ const moment_1 = __importDefault(require("moment"));
9
+ const logger_1 = __importDefault(require("../../services/logger"));
10
+ const taskScheduler_1 = __importDefault(require("../../services/taskScheduler"));
11
+ const async_sema_1 = require("async-sema");
12
+ const actionStateBase_1 = __importDefault(require("../states/actionStateBase"));
13
+ const timeConvertions_1 = require("../../helpers/timeConvertions");
14
+ class ScheduledAction {
15
+ constructor(name, handler, timeinHours, active, whitelist, cachedStateFactories) {
16
+ this.cachedState = new Map();
17
+ this.stateConstructor = () => new actionStateBase_1.default();
18
+ this.name = name;
19
+ this.handler = handler;
20
+ this.timeinHours = timeinHours;
21
+ this.active = active;
22
+ this.chatsWhitelist = whitelist;
23
+ this.cachedStateFactories = cachedStateFactories;
24
+ this.key = `scheduled:${this.name.replace('.', '-')}`;
25
+ }
26
+ async exec(ctx) {
27
+ if (!this.active || !this.chatsWhitelist.includes(ctx.chatId))
28
+ return;
29
+ const state = await storage_1.default.getActionState(this, ctx.chatId);
30
+ const isAllowedToTrigger = this.shouldTrigger(state);
31
+ if (isAllowedToTrigger) {
32
+ logger_1.default.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, ` - Executing [${this.name}] in ${ctx.chatId}`);
33
+ await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName));
34
+ state.lastExecutedDate = (0, moment_1.default)().valueOf();
35
+ await storage_1.default.commitTransactionForAction(this, ctx.chatId, new transactionResult_1.default(state, isAllowedToTrigger));
36
+ }
37
+ }
38
+ async getCachedValue(key, botName) {
39
+ if (!this.cachedStateFactories.has(key)) {
40
+ throw new Error(`No shared cache was set up for the key [${key}] in action '${this.name}'`);
41
+ }
42
+ await ScheduledAction.semaphore.acquire();
43
+ try {
44
+ if (this.cachedState.has(key)) {
45
+ return this.cachedState.get(key);
46
+ }
47
+ const cachedItemFactory = this.cachedStateFactories.get(key);
48
+ const value = await cachedItemFactory.getValue();
49
+ this.cachedState.set(key, value);
50
+ taskScheduler_1.default.createOnetimeTask(`Drop cached value [${this.name} : ${key}]`, () => this.cachedState.delete(key), (0, timeConvertions_1.hoursToMilliseconds)(cachedItemFactory.invalidationTimeoutInHours), botName);
51
+ return value;
52
+ }
53
+ finally {
54
+ ScheduledAction.semaphore.release();
55
+ }
56
+ }
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;
61
+ return isAllowedToTrigger && !hasTriggeredToday;
62
+ }
63
+ }
64
+ ScheduledAction.semaphore = new async_sema_1.Sema(1);
65
+ exports.default = ScheduledAction;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const telegraf_1 = require("telegraf");
7
+ const telegramApi_1 = __importDefault(require("../services/telegramApi"));
8
+ const incomingMessage_1 = __importDefault(require("./incomingMessage"));
9
+ const taskScheduler_1 = __importDefault(require("../services/taskScheduler"));
10
+ const logger_1 = __importDefault(require("../services/logger"));
11
+ const timeConvertions_1 = require("../helpers/timeConvertions");
12
+ const storage_1 = __importDefault(require("../services/storage"));
13
+ class Bot {
14
+ constructor(name, token, commands, scheduled, chats) {
15
+ this.messageQueue = [];
16
+ this.name = name;
17
+ this.commands = commands;
18
+ this.scheduled = scheduled;
19
+ this.chats = chats;
20
+ logger_1.default.logWithTraceId(this.name, `System:Bot-${this.name}-Start`, 'System', 'Starting bot...');
21
+ this.telegraf = new telegraf_1.Telegraf(token);
22
+ this.api = new telegramApi_1.default(this.name, this.telegraf, this.chats);
23
+ this.telegraf.on('message', async (ctx) => {
24
+ const msg = new incomingMessage_1.default(ctx.update.message);
25
+ const messageContent = msg.text || '<non-text message>';
26
+ const messageFromName = msg.from?.first_name ?? 'Unknown';
27
+ const messageFromId = msg.from?.id ?? 'Unknown';
28
+ logger_1.default.logWithTraceId(this.name, msg.traceId, msg.chatName, `${messageFromName} (${messageFromId}): ${messageContent}`);
29
+ if (msg.text) {
30
+ this.messageQueue.push(msg);
31
+ }
32
+ });
33
+ this.telegraf.launch();
34
+ taskScheduler_1.default.createTask('MessageProcessing', async () => {
35
+ while (this.messageQueue.length > 0) {
36
+ await this.processMessages();
37
+ }
38
+ }, (0, timeConvertions_1.secondsToMilliseconds)(0.3), false, this.name);
39
+ taskScheduler_1.default.createTask('ScheduledProcessing', async () => {
40
+ await this.runScheduled();
41
+ }, (0, timeConvertions_1.hoursToMilliseconds)(0.5), true, this.name);
42
+ storage_1.default.saveMetadata([...this.commands, ...this.scheduled], this.name);
43
+ }
44
+ stop(code) {
45
+ logger_1.default.logWithTraceId(this.name, `System:Bot-${this.name}-Stop`, 'System', 'Stopping bot...');
46
+ this.telegraf.stop(code);
47
+ }
48
+ async runScheduled() {
49
+ for (const [chatName, chatId] of this.chats.entries()) {
50
+ for (const trig of this.scheduled) {
51
+ const ctx = this.api.createContextForChat(chatId, trig.name);
52
+ try {
53
+ await trig.exec(ctx);
54
+ }
55
+ catch (error) {
56
+ console.dir(error);
57
+ logger_1.default.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ async processMessages() {
63
+ const msg = this.messageQueue.pop();
64
+ for (const cmd of this.commands) {
65
+ const ctx = this.api.createContextForMessage(msg);
66
+ try {
67
+ await cmd.exec(ctx);
68
+ }
69
+ catch (error) {
70
+ logger_1.default.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, error, ctx);
71
+ }
72
+ }
73
+ }
74
+ }
75
+ exports.default = Bot;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class CachedStateFactory {
4
+ constructor(itemFactory, invalidationTimeout) {
5
+ this.getValue = itemFactory;
6
+ this.invalidationTimeoutInHours = invalidationTimeout;
7
+ }
8
+ }
9
+ exports.default = CachedStateFactory;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class CommandTriggerCheckResult {
4
+ static get DontTriggerAndSkipCooldown() {
5
+ return new CommandTriggerCheckResult(false, [], true);
6
+ }
7
+ static get DoNotTrigger() {
8
+ return new CommandTriggerCheckResult(false, [], false);
9
+ }
10
+ constructor(shouldTrigger, matchResults, skipCooldown) {
11
+ this.shouldTrigger = shouldTrigger;
12
+ this.matchResults = matchResults;
13
+ this.skipCooldown = skipCooldown;
14
+ }
15
+ mergeWith(other) {
16
+ this.shouldTrigger = this.shouldTrigger || other.shouldTrigger;
17
+ this.matchResults = this.matchResults.concat(other.matchResults);
18
+ this.skipCooldown = this.skipCooldown || other.skipCooldown;
19
+ return this;
20
+ }
21
+ }
22
+ exports.default = CommandTriggerCheckResult;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const imageMessage_1 = __importDefault(require("../responses/imageMessage"));
7
+ const textMessage_1 = __importDefault(require("../responses/textMessage"));
8
+ const videoMessage_1 = __importDefault(require("../responses/videoMessage"));
9
+ const path_1 = require("path");
10
+ class ChatContext {
11
+ constructor(botName, interactions, chatId, chatName, traceId) {
12
+ this.botName = botName;
13
+ this.interactions = interactions;
14
+ this.chatId = chatId;
15
+ this.chatName = chatName;
16
+ this.traceId = traceId;
17
+ }
18
+ sendTextToChat(text) {
19
+ this.interactions.respond(new textMessage_1.default(text, this.chatId, undefined, this.traceId));
20
+ }
21
+ sendImageToChat(name) {
22
+ const filePath = `./content/${name}.png`;
23
+ this.interactions.respond(new imageMessage_1.default({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId));
24
+ }
25
+ sendVideoToChat(name) {
26
+ const filePath = `./content/${name}.mp4`;
27
+ this.interactions.respond(new videoMessage_1.default({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId));
28
+ }
29
+ }
30
+ exports.default = ChatContext;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const actionStateBase_1 = __importDefault(require("../states/actionStateBase"));
7
+ const imageMessage_1 = __importDefault(require("../responses/imageMessage"));
8
+ const textMessage_1 = __importDefault(require("../responses/textMessage"));
9
+ const videoMessage_1 = __importDefault(require("../responses/videoMessage"));
10
+ const chatContext_1 = __importDefault(require("./chatContext"));
11
+ const storage_1 = __importDefault(require("../../services/storage"));
12
+ const path_1 = require("path");
13
+ const reaction_1 = __importDefault(require("../responses/reaction"));
14
+ class MessageContext extends chatContext_1.default {
15
+ constructor(botName, interactions, chatId, chatName, messageId, messageText, fromUserId, traceId, fromUserName) {
16
+ super(botName, interactions, chatId, chatName, traceId);
17
+ this.matchResults = [];
18
+ this.startCooldown = true;
19
+ this.updateActions = [];
20
+ this.messageId = messageId;
21
+ this.messageText = messageText;
22
+ this.fromUserId = fromUserId;
23
+ this.fromUserName = fromUserName;
24
+ }
25
+ async loadStateOf(commandName) {
26
+ return ((await storage_1.default.load(`command:${commandName.replace('.', '-')}`))[this.chatId] ?? new actionStateBase_1.default());
27
+ }
28
+ updateState(stateUpdateAction) {
29
+ this.updateActions.push(stateUpdateAction);
30
+ }
31
+ replyWithText(text) {
32
+ this.interactions.respond(new textMessage_1.default(text, this.chatId, this.messageId, this.traceId));
33
+ }
34
+ replyWithImage(name) {
35
+ const filePath = `./content/${name}.png`;
36
+ this.interactions.respond(new imageMessage_1.default({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId));
37
+ }
38
+ replyWithVideo(name) {
39
+ const filePath = `./content/${name}.mp4`;
40
+ this.interactions.respond(new videoMessage_1.default({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId));
41
+ }
42
+ react(emoji) {
43
+ this.interactions.react(new reaction_1.default(this.traceId, this.chatId, this.messageId, emoji));
44
+ }
45
+ }
46
+ exports.default = MessageContext;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const randomInt_1 = __importDefault(require("../helpers/randomInt"));
7
+ class IncomingMessage {
8
+ constructor(ctxMessage) {
9
+ this.traceId = (0, randomInt_1.default)(10000, 99999);
10
+ this.message_id = ctxMessage.message_id;
11
+ this.chat = ctxMessage.chat;
12
+ this.from = ctxMessage.from;
13
+ this.text = ctxMessage.text || '';
14
+ this.chatName =
15
+ 'title' in ctxMessage.chat
16
+ ? ctxMessage.chat.title + ' ' + ctxMessage.chat.id
17
+ : 'DM';
18
+ }
19
+ }
20
+ exports.default = IncomingMessage;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ImageMessage {
4
+ constructor(image, chatId, replyId, traceId) {
5
+ this.content = image;
6
+ this.chatId = chatId;
7
+ this.replyId = replyId;
8
+ this.traceId = traceId;
9
+ }
10
+ }
11
+ exports.default = ImageMessage;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Reaction {
4
+ constructor(traceId, chatId, messageId, emoji) {
5
+ this.chatId = chatId;
6
+ this.messageId = messageId;
7
+ this.emoji = emoji;
8
+ this.traceId = traceId;
9
+ }
10
+ }
11
+ exports.default = Reaction;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class TextMessage {
4
+ constructor(text, chatId, replyId, traceId) {
5
+ this.content = text;
6
+ this.chatId = chatId;
7
+ this.replyId = replyId;
8
+ this.traceId = traceId;
9
+ }
10
+ }
11
+ exports.default = TextMessage;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class VideoMessage {
4
+ constructor(video, chatId, replyId, traceId) {
5
+ this.content = video;
6
+ this.chatId = chatId;
7
+ this.replyId = replyId;
8
+ this.traceId = traceId;
9
+ }
10
+ }
11
+ exports.default = VideoMessage;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ActionStateBase {
4
+ constructor() {
5
+ this.lastExecutedDate = 0;
6
+ }
7
+ }
8
+ exports.default = ActionStateBase;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const actionStateBase_1 = __importDefault(require("./actionStateBase"));
7
+ class PotuzhnoState extends actionStateBase_1.default {
8
+ constructor() {
9
+ super(...arguments);
10
+ this.scoreBoard = {};
11
+ }
12
+ }
13
+ exports.default = PotuzhnoState;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class TaskRecord {
4
+ constructor(name, taskId, interval) {
5
+ this.name = name;
6
+ this.taskId = taskId;
7
+ this.interval = interval;
8
+ }
9
+ }
10
+ exports.default = TaskRecord;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class TransactionResult {
4
+ constructor(data, shouldUpdate) {
5
+ this.data = data;
6
+ this.shouldUpdate = shouldUpdate;
7
+ }
8
+ }
9
+ exports.default = TransactionResult;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CommandActionBuilder = exports.CommandActionBuilderWithState = void 0;
7
+ const commandAction_1 = __importDefault(require("../../entities/actions/commandAction"));
8
+ const actionStateBase_1 = __importDefault(require("../../entities/states/actionStateBase"));
9
+ const toArray_1 = __importDefault(require("../toArray"));
10
+ const noop_1 = __importDefault(require("../noop"));
11
+ class CommandActionBuilderWithState {
12
+ constructor(name, stateConstructor) {
13
+ this.trigger = [];
14
+ this.active = true;
15
+ this.cooldownSeconds = 0;
16
+ this.blacklist = [];
17
+ this.allowedUsers = [];
18
+ this.handler = noop_1.default.call;
19
+ this.condition = noop_1.default.true;
20
+ this.name = name;
21
+ this.stateConstructor = stateConstructor;
22
+ }
23
+ on(trigger) {
24
+ this.trigger = trigger;
25
+ return this;
26
+ }
27
+ from(id) {
28
+ this.allowedUsers = (0, toArray_1.default)(id);
29
+ return this;
30
+ }
31
+ do(handler) {
32
+ this.handler = handler;
33
+ return this;
34
+ }
35
+ when(condition) {
36
+ this.condition = condition;
37
+ return this;
38
+ }
39
+ disabled() {
40
+ this.active = false;
41
+ return this;
42
+ }
43
+ cooldown(seconds) {
44
+ this.cooldownSeconds = seconds;
45
+ return this;
46
+ }
47
+ ignoreChat(chatId) {
48
+ this.blacklist.push(chatId);
49
+ return this;
50
+ }
51
+ build() {
52
+ return new commandAction_1.default(this.trigger, this.handler, this.name, this.active, this.cooldownSeconds, this.blacklist, this.allowedUsers, this.condition, this.stateConstructor);
53
+ }
54
+ }
55
+ exports.CommandActionBuilderWithState = CommandActionBuilderWithState;
56
+ class CommandActionBuilder extends CommandActionBuilderWithState {
57
+ constructor(name) {
58
+ super(name, () => new actionStateBase_1.default());
59
+ }
60
+ }
61
+ exports.CommandActionBuilder = CommandActionBuilder;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const scheduledAction_1 = __importDefault(require("../../entities/actions/scheduledAction"));
7
+ const cachedStateFactory_1 = __importDefault(require("../../entities/cachedStateFactory"));
8
+ const noop_1 = __importDefault(require("../noop"));
9
+ class ScheduledActionBuilder {
10
+ constructor(name) {
11
+ this.active = true;
12
+ this.time = 0;
13
+ this.cachedStateFactories = new Map();
14
+ this.whitelist = [];
15
+ this.handler = noop_1.default.call;
16
+ this.name = name;
17
+ }
18
+ allowIn(chatId) {
19
+ this.whitelist.push(chatId);
20
+ return this;
21
+ }
22
+ runAt(time) {
23
+ this.time = time;
24
+ return this;
25
+ }
26
+ do(handler) {
27
+ this.handler = handler;
28
+ return this;
29
+ }
30
+ withSharedCache(key, itemFactory, invalidationTimeoutInHours = 20) {
31
+ this.cachedStateFactories.set(key, new cachedStateFactory_1.default(itemFactory, invalidationTimeoutInHours));
32
+ return this;
33
+ }
34
+ disabled() {
35
+ this.active = false;
36
+ return this;
37
+ }
38
+ build() {
39
+ return new scheduledAction_1.default(this.name, this.handler, this.time, this.active, this.whitelist, this.cachedStateFactories);
40
+ }
41
+ }
42
+ exports.default = ScheduledActionBuilder;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = escapeMarkdown;
7
+ const markdown_escape_1 = __importDefault(require("markdown-escape"));
8
+ function escapeMarkdown(text) {
9
+ return (0, markdown_escape_1.default)(text)
10
+ .replaceAll(/\./g, '\\.')
11
+ .replaceAll(/-/g, '\\-')
12
+ .replaceAll(/\+/g, '\\+')
13
+ .replaceAll(/\{/g, '\\{')
14
+ .replaceAll(/!/g, '\\!')
15
+ .replaceAll(/\|/g, '\\|')
16
+ .replaceAll(/\}/g, '\\}');
17
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = getCurrentWeek;
7
+ const moment_1 = __importDefault(require("moment"));
8
+ function getCurrentWeek() {
9
+ const firstDay = (0, moment_1.default)().startOf('isoWeek').format('YYYY-MM-DD');
10
+ const lastDay = (0, moment_1.default)().endOf('isoWeek').format('YYYY-MM-DD');
11
+ return { firstDay, lastDay };
12
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable @typescript-eslint/no-unused-vars */
4
+ class Noop {
5
+ static async true(arg1) {
6
+ return true;
7
+ }
8
+ static async false(arg1) {
9
+ return false;
10
+ }
11
+ static async call(arg1) { }
12
+ }
13
+ exports.default = Noop;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = randomInteger;
4
+ function randomInteger(x1, x2) {
5
+ const range = x2 - x1 + 1;
6
+ const random = Math.floor(Math.random() * range);
7
+ return random + x1;
8
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reverseMap = reverseMap;
4
+ function reverseMap(map) {
5
+ return new Map(Array.from(map, ([key, value]) => [value, key]));
6
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.secondsToMilliseconds = secondsToMilliseconds;
4
+ exports.hoursToMilliseconds = hoursToMilliseconds;
5
+ exports.hoursToSeconds = hoursToSeconds;
6
+ function secondsToMilliseconds(value) {
7
+ return (value * 1000);
8
+ }
9
+ function hoursToMilliseconds(value) {
10
+ return (value * 60 * 60 * 1000);
11
+ }
12
+ function hoursToSeconds(value) {
13
+ return (value * 60 * 60);
14
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = toArray;
4
+ function toArray(value) {
5
+ return Array.isArray(value) ? value : [value];
6
+ }