chz-telegram-bot 0.5.4 → 0.6.8

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 (234) hide show
  1. package/README.md +41 -31
  2. package/dist/builtin/helpAction.d.ts +2 -0
  3. package/dist/builtin/helpAction.d.ts.map +1 -0
  4. package/dist/builtin/helpAction.js +14 -0
  5. package/dist/dtos/chatHistoryMessage.d.ts +35 -0
  6. package/dist/dtos/chatHistoryMessage.d.ts.map +1 -0
  7. package/dist/dtos/chatHistoryMessage.js +32 -0
  8. package/dist/dtos/chatInfo.d.ts +17 -0
  9. package/dist/dtos/chatInfo.d.ts.map +1 -0
  10. package/dist/dtos/chatInfo.js +16 -0
  11. package/dist/dtos/commandTriggerCheckResult.d.ts +24 -0
  12. package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -0
  13. package/dist/dtos/commandTriggerCheckResult.js +34 -0
  14. package/dist/dtos/cooldownInfo.d.ts +13 -0
  15. package/dist/dtos/cooldownInfo.d.ts.map +1 -0
  16. package/dist/dtos/cooldownInfo.js +12 -0
  17. package/dist/dtos/incomingMessage.d.ts +19 -0
  18. package/dist/dtos/incomingMessage.d.ts.map +1 -0
  19. package/dist/dtos/incomingMessage.js +63 -0
  20. package/dist/dtos/incomingQuery.d.ts +10 -0
  21. package/dist/dtos/incomingQuery.d.ts.map +1 -0
  22. package/dist/dtos/incomingQuery.js +14 -0
  23. package/dist/dtos/messageInfo.d.ts +22 -0
  24. package/dist/dtos/messageInfo.d.ts.map +1 -0
  25. package/dist/dtos/messageInfo.js +20 -0
  26. package/dist/dtos/propertyProviderSets.d.ts +16 -0
  27. package/dist/dtos/propertyProviderSets.d.ts.map +1 -0
  28. package/dist/dtos/propertyProviderSets.js +1 -0
  29. package/dist/dtos/replyInfo.d.ts +6 -0
  30. package/dist/dtos/replyInfo.d.ts.map +1 -0
  31. package/dist/dtos/replyInfo.js +8 -0
  32. package/dist/dtos/responses/delay.d.ts +16 -0
  33. package/dist/dtos/responses/delay.d.ts.map +1 -0
  34. package/dist/dtos/responses/delay.js +15 -0
  35. package/dist/dtos/responses/imageMessage.d.ts +22 -0
  36. package/dist/dtos/responses/imageMessage.d.ts.map +1 -0
  37. package/dist/dtos/responses/imageMessage.js +21 -0
  38. package/dist/dtos/responses/inlineQueryResponse.d.ts +13 -0
  39. package/dist/dtos/responses/inlineQueryResponse.d.ts.map +1 -0
  40. package/dist/dtos/responses/inlineQueryResponse.js +15 -0
  41. package/dist/dtos/responses/reaction.d.ts +16 -0
  42. package/dist/dtos/responses/reaction.d.ts.map +1 -0
  43. package/dist/dtos/responses/reaction.js +17 -0
  44. package/dist/dtos/responses/textMessage.d.ts +23 -0
  45. package/dist/dtos/responses/textMessage.d.ts.map +1 -0
  46. package/dist/dtos/responses/textMessage.js +24 -0
  47. package/dist/dtos/responses/unpin.d.ts +15 -0
  48. package/dist/dtos/responses/unpin.d.ts.map +1 -0
  49. package/dist/dtos/responses/unpin.js +15 -0
  50. package/dist/dtos/responses/videoMessage.d.ts +22 -0
  51. package/dist/dtos/responses/videoMessage.d.ts.map +1 -0
  52. package/dist/dtos/responses/videoMessage.js +21 -0
  53. package/dist/dtos/userInfo.d.ts +12 -0
  54. package/dist/dtos/userInfo.d.ts.map +1 -0
  55. package/dist/dtos/userInfo.js +12 -0
  56. package/dist/entities/actions/commandAction.d.ts +31 -0
  57. package/dist/entities/actions/commandAction.d.ts.map +1 -0
  58. package/dist/entities/actions/commandAction.js +149 -0
  59. package/dist/entities/actions/inlineQueryAction.d.ts +14 -0
  60. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -0
  61. package/dist/entities/actions/inlineQueryAction.js +51 -0
  62. package/dist/entities/actions/replyCaptureAction.d.ts +15 -0
  63. package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -0
  64. package/dist/entities/actions/replyCaptureAction.js +66 -0
  65. package/dist/entities/actions/scheduledAction.d.ts +24 -0
  66. package/dist/entities/actions/scheduledAction.d.ts.map +1 -0
  67. package/dist/entities/actions/scheduledAction.js +92 -0
  68. package/dist/entities/botInstance.d.ts +36 -0
  69. package/dist/entities/botInstance.d.ts.map +1 -0
  70. package/dist/entities/botInstance.js +39 -0
  71. package/dist/entities/cachedStateFactory.d.ts +7 -0
  72. package/dist/entities/cachedStateFactory.d.ts.map +1 -0
  73. package/dist/entities/cachedStateFactory.js +8 -0
  74. package/dist/entities/context/baseContext.d.ts +39 -0
  75. package/dist/entities/context/baseContext.d.ts.map +1 -0
  76. package/dist/entities/context/baseContext.js +56 -0
  77. package/dist/entities/context/chatContext.d.ts +50 -0
  78. package/dist/entities/context/chatContext.d.ts.map +1 -0
  79. package/dist/entities/context/chatContext.js +65 -0
  80. package/dist/entities/context/inlineQueryContext.d.ts +27 -0
  81. package/dist/entities/context/inlineQueryContext.d.ts.map +1 -0
  82. package/dist/entities/context/inlineQueryContext.js +29 -0
  83. package/dist/entities/context/messageContext.d.ts +92 -0
  84. package/dist/entities/context/messageContext.d.ts.map +1 -0
  85. package/dist/entities/context/messageContext.js +116 -0
  86. package/dist/entities/context/replyContext.d.ts +89 -0
  87. package/dist/entities/context/replyContext.d.ts.map +1 -0
  88. package/dist/entities/context/replyContext.js +124 -0
  89. package/dist/entities/states/actionStateBase.d.ts +6 -0
  90. package/dist/entities/states/actionStateBase.d.ts.map +1 -0
  91. package/dist/entities/states/actionStateBase.js +4 -0
  92. package/dist/entities/taskRecord.d.ts +8 -0
  93. package/dist/entities/taskRecord.d.ts.map +1 -0
  94. package/dist/entities/taskRecord.js +10 -0
  95. package/dist/eslint.config.d.ts +3 -0
  96. package/dist/eslint.config.d.ts.map +1 -0
  97. package/dist/eslint.config.js +51 -0
  98. package/dist/helpers/builders/commandActionBuilder.d.ts +100 -0
  99. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -0
  100. package/dist/helpers/builders/commandActionBuilder.js +146 -0
  101. package/dist/helpers/builders/inlineQueryActionBuilder.d.ts +37 -0
  102. package/dist/helpers/builders/inlineQueryActionBuilder.d.ts.map +1 -0
  103. package/dist/helpers/builders/inlineQueryActionBuilder.js +50 -0
  104. package/dist/helpers/builders/scheduledActionBuilder.d.ts +68 -0
  105. package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -0
  106. package/dist/helpers/builders/scheduledActionBuilder.js +95 -0
  107. package/dist/helpers/mapUtils.d.ts +10 -0
  108. package/dist/helpers/mapUtils.d.ts.map +1 -0
  109. package/dist/helpers/mapUtils.js +13 -0
  110. package/dist/helpers/noop.d.ts +9 -0
  111. package/dist/helpers/noop.d.ts.map +1 -0
  112. package/dist/helpers/noop.js +17 -0
  113. package/dist/helpers/objectFromEntries.d.ts +2 -0
  114. package/dist/helpers/objectFromEntries.d.ts.map +1 -0
  115. package/dist/helpers/objectFromEntries.js +3 -0
  116. package/dist/helpers/timeConvertions.d.ts +5 -0
  117. package/dist/helpers/timeConvertions.d.ts.map +1 -0
  118. package/dist/helpers/timeConvertions.js +9 -0
  119. package/dist/helpers/toArray.d.ts +2 -0
  120. package/dist/helpers/toArray.d.ts.map +1 -0
  121. package/dist/helpers/toArray.js +3 -0
  122. package/dist/helpers/traceFactory.d.ts +3 -0
  123. package/dist/helpers/traceFactory.d.ts.map +1 -0
  124. package/dist/helpers/traceFactory.js +3 -0
  125. package/dist/index.d.ts +27 -0
  126. package/dist/index.d.ts.map +1 -0
  127. package/dist/index.js +20 -0
  128. package/dist/main.d.ts +49 -0
  129. package/dist/main.d.ts.map +1 -0
  130. package/dist/main.js +33 -0
  131. package/dist/services/actionProcessingService.d.ts +25 -0
  132. package/dist/services/actionProcessingService.d.ts.map +1 -0
  133. package/dist/services/actionProcessingService.js +50 -0
  134. package/dist/services/actionProcessors/baseProcessor.d.ts +18 -0
  135. package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -0
  136. package/dist/services/actionProcessors/baseProcessor.js +33 -0
  137. package/dist/services/actionProcessors/commandActionProcessor.d.ts +19 -0
  138. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -0
  139. package/dist/services/actionProcessors/commandActionProcessor.js +130 -0
  140. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +11 -0
  141. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -0
  142. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +69 -0
  143. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +17 -0
  144. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -0
  145. package/dist/services/actionProcessors/scheduledActionProcessor.js +55 -0
  146. package/dist/services/jsonFileStorage.d.ts +25 -0
  147. package/dist/services/jsonFileStorage.d.ts.map +1 -0
  148. package/dist/services/jsonFileStorage.js +133 -0
  149. package/dist/services/nodeTimeoutScheduler.d.ts +13 -0
  150. package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -0
  151. package/dist/services/nodeTimeoutScheduler.js +50 -0
  152. package/dist/services/responseProcessingQueue.d.ts +12 -0
  153. package/dist/services/responseProcessingQueue.d.ts.map +1 -0
  154. package/dist/services/responseProcessingQueue.js +37 -0
  155. package/dist/services/telegramApi.d.ts +23 -0
  156. package/dist/services/telegramApi.d.ts.map +1 -0
  157. package/dist/services/telegramApi.js +163 -0
  158. package/dist/types/action.d.ts +14 -0
  159. package/dist/types/action.d.ts.map +1 -0
  160. package/dist/types/action.js +1 -0
  161. package/dist/types/actionState.d.ts +5 -0
  162. package/dist/types/actionState.d.ts.map +1 -0
  163. package/dist/types/actionState.js +1 -0
  164. package/dist/types/cachedValueAccessor.d.ts +2 -0
  165. package/dist/types/cachedValueAccessor.d.ts.map +1 -0
  166. package/dist/types/cachedValueAccessor.js +1 -0
  167. package/dist/types/capture.d.ts +24 -0
  168. package/dist/types/capture.d.ts.map +1 -0
  169. package/dist/types/capture.js +1 -0
  170. package/dist/types/commandCondition.d.ts +8 -0
  171. package/dist/types/commandCondition.d.ts.map +1 -0
  172. package/dist/types/commandCondition.js +1 -0
  173. package/dist/types/commandTrigger.d.ts +2 -0
  174. package/dist/types/commandTrigger.d.ts.map +1 -0
  175. package/dist/types/commandTrigger.js +1 -0
  176. package/dist/types/events.d.ts +193 -0
  177. package/dist/types/events.d.ts.map +1 -0
  178. package/dist/types/events.js +69 -0
  179. package/dist/types/externalAliases.d.ts +11 -0
  180. package/dist/types/externalAliases.d.ts.map +1 -0
  181. package/dist/types/externalAliases.js +1 -0
  182. package/dist/types/handlers.d.ts +21 -0
  183. package/dist/types/handlers.d.ts.map +1 -0
  184. package/dist/types/handlers.js +1 -0
  185. package/dist/types/inputFile.d.ts +5 -0
  186. package/dist/types/inputFile.d.ts.map +1 -0
  187. package/dist/types/inputFile.js +1 -0
  188. package/dist/types/logger.d.ts +1 -0
  189. package/dist/types/logger.d.ts.map +1 -0
  190. package/dist/types/logger.js +1 -0
  191. package/dist/types/messageSendingOptions.d.ts +9 -0
  192. package/dist/types/messageSendingOptions.d.ts.map +1 -0
  193. package/dist/types/messageSendingOptions.js +1 -0
  194. package/dist/types/messageTypes.d.ts +20 -0
  195. package/dist/types/messageTypes.d.ts.map +1 -0
  196. package/dist/types/messageTypes.js +18 -0
  197. package/dist/types/propertyProvider.d.ts +8 -0
  198. package/dist/types/propertyProvider.d.ts.map +1 -0
  199. package/dist/types/propertyProvider.js +1 -0
  200. package/dist/types/response.d.ts +39 -0
  201. package/dist/types/response.d.ts.map +1 -0
  202. package/dist/types/response.js +9 -0
  203. package/dist/types/scheduler.d.ts +7 -0
  204. package/dist/types/scheduler.d.ts.map +1 -0
  205. package/dist/types/scheduler.js +1 -0
  206. package/dist/types/storage.d.ts +11 -0
  207. package/dist/types/storage.d.ts.map +1 -0
  208. package/dist/types/storage.js +1 -0
  209. package/dist/types/timeValues.d.ts +15 -0
  210. package/dist/types/timeValues.d.ts.map +1 -0
  211. package/dist/types/timeValues.js +1 -0
  212. package/dist/types/trace.d.ts +6 -0
  213. package/dist/types/trace.d.ts.map +1 -0
  214. package/dist/types/trace.js +1 -0
  215. package/entities/actions/commandAction.ts +11 -3
  216. package/entities/actions/inlineQueryAction.ts +9 -1
  217. package/entities/actions/replyCaptureAction.ts +9 -3
  218. package/entities/actions/scheduledAction.ts +31 -10
  219. package/entities/botInstance.ts +18 -25
  220. package/entities/context/baseContext.ts +9 -4
  221. package/index.ts +1 -1
  222. package/main.ts +1 -10
  223. package/package.json +38 -38
  224. package/services/actionProcessingService.ts +11 -15
  225. package/services/actionProcessors/baseProcessor.ts +9 -9
  226. package/services/actionProcessors/commandActionProcessor.ts +35 -46
  227. package/services/actionProcessors/inlineQueryActionProcessor.ts +24 -20
  228. package/services/actionProcessors/scheduledActionProcessor.ts +5 -10
  229. package/services/jsonFileStorage.ts +27 -1
  230. package/services/nodeTimeoutScheduler.ts +22 -22
  231. package/services/telegramApi.ts +53 -23
  232. package/types/events.ts +285 -0
  233. package/services/jsonLogger.ts +0 -112
  234. package/types/logger.ts +0 -39
@@ -0,0 +1,133 @@
1
+ import { existsSync, mkdirSync } from 'fs';
2
+ import { readFile, writeFile } from 'fs/promises';
3
+ import { Sema as Semaphore } from 'async-sema';
4
+ import { getOrSetIfNotExists } from '../helpers/mapUtils';
5
+ import { BotEventType } from '../types/events';
6
+ function buildPath(storagePath, botName, actionKey) {
7
+ return `${storagePath}/${botName}/${actionKey.replaceAll(':', '/')}.json`;
8
+ }
9
+ export class JsonFileStorage {
10
+ eventEmitter;
11
+ filePaths = new Map();
12
+ locks = new Map();
13
+ cache;
14
+ storagePath;
15
+ botName;
16
+ constructor(botName, actions, eventEmitter, path) {
17
+ this.eventEmitter = eventEmitter;
18
+ this.cache = new Map();
19
+ this.botName = botName;
20
+ this.storagePath = path ?? 'storage';
21
+ if (!existsSync(`${this.storagePath}/${this.botName}/`)) {
22
+ mkdirSync(`${this.storagePath}/${this.botName}/`, {
23
+ recursive: true
24
+ });
25
+ }
26
+ for (const action of actions) {
27
+ this.locks.set(action.key, new Semaphore(1));
28
+ this.filePaths.set(action.key, buildPath(this.storagePath, this.botName, action.key));
29
+ }
30
+ }
31
+ backfillEmptyActionStates(action, data) {
32
+ for (const [stringKey, value] of Object.entries(data)) {
33
+ if (value)
34
+ continue;
35
+ data[Number.parseInt(stringKey)] = action.stateConstructor();
36
+ }
37
+ return true;
38
+ }
39
+ async lock(key, action) {
40
+ const lock = getOrSetIfNotExists(this.locks, key, new Semaphore(1));
41
+ this.eventEmitter.emit(BotEventType.storageLockAcquiring, key);
42
+ await lock.acquire();
43
+ this.eventEmitter.emit(BotEventType.storageLockAcquired, key);
44
+ try {
45
+ return await action();
46
+ }
47
+ finally {
48
+ lock.release();
49
+ this.eventEmitter.emit(BotEventType.storageLockReleased, key);
50
+ }
51
+ }
52
+ tryGetFromCache(key) {
53
+ return this.cache.get(key);
54
+ }
55
+ async loadFromFile(key) {
56
+ const targetPath = getOrSetIfNotExists(this.filePaths, key, buildPath(this.storagePath, this.botName, key));
57
+ const fileContent = await readFile(targetPath, {
58
+ encoding: 'utf-8',
59
+ flag: 'a+'
60
+ });
61
+ if (fileContent) {
62
+ const data = JSON.parse(fileContent);
63
+ this.cache.set(key, data);
64
+ }
65
+ return (this.cache.get(key) ?? {});
66
+ }
67
+ async updateCacheAndSaveToFile(data, key) {
68
+ this.eventEmitter.emit(BotEventType.storageStateSaving, {
69
+ data,
70
+ key
71
+ });
72
+ this.cache.set(key, data);
73
+ const targetPath = getOrSetIfNotExists(this.filePaths, key, buildPath(this.storagePath, this.botName, key));
74
+ await writeFile(targetPath, JSON.stringify(data), { flag: 'w+' });
75
+ this.eventEmitter.emit(BotEventType.storageStateSaved, {
76
+ data,
77
+ key
78
+ });
79
+ }
80
+ async load(key) {
81
+ return (this.tryGetFromCache(key) ??
82
+ (await this.lock(key, async () => {
83
+ return await this.loadFromFile(key);
84
+ })));
85
+ }
86
+ async saveMetadata(actions) {
87
+ const targetPath = `${this.storagePath}/${this.botName}/Metadata-${this.botName}.json`;
88
+ await writeFile(targetPath, JSON.stringify(actions), {
89
+ flag: 'w+'
90
+ });
91
+ }
92
+ async getActionState(action, chatId) {
93
+ this.eventEmitter.emit(BotEventType.storageStateLoading, {
94
+ action,
95
+ chatId
96
+ });
97
+ const value = this.tryGetFromCache(action.key) ??
98
+ (await this.lock(action.key, async () => {
99
+ return await this.loadFromFile(action.key);
100
+ }));
101
+ const result = Object.assign(action.stateConstructor(), value[chatId]);
102
+ this.eventEmitter.emit(BotEventType.storageStateLoaded, {
103
+ action,
104
+ chatId,
105
+ state: result
106
+ });
107
+ return result;
108
+ }
109
+ async saveActionExecutionResult(action, chatId, state) {
110
+ await this.lock(action.key, async () => {
111
+ const data = this.tryGetFromCache(action.key) ??
112
+ (await this.loadFromFile(action.key));
113
+ data[chatId] = state;
114
+ if (this.backfillEmptyActionStates(action, data))
115
+ await this.updateCacheAndSaveToFile(data, action.key);
116
+ });
117
+ }
118
+ async close() {
119
+ for (const lock of this.locks.values()) {
120
+ await lock.acquire();
121
+ }
122
+ }
123
+ async updateStateFor(action, chatId, update) {
124
+ await this.lock(action.key, async () => {
125
+ const data = this.tryGetFromCache(action.key) ??
126
+ (await this.loadFromFile(action.key));
127
+ const state = Object.assign(action.stateConstructor(), data[chatId]);
128
+ await update(state);
129
+ if (this.backfillEmptyActionStates(action, data))
130
+ await this.updateCacheAndSaveToFile(data, action.key);
131
+ });
132
+ }
133
+ }
@@ -0,0 +1,13 @@
1
+ import { TaskRecord } from '../entities/taskRecord';
2
+ import { TypedEventEmitter } from '../types/events';
3
+ import { IScheduler } from '../types/scheduler';
4
+ import { Milliseconds } from '../types/timeValues';
5
+ export declare class NodeTimeoutScheduler implements IScheduler {
6
+ readonly eventEmitter: TypedEventEmitter;
7
+ readonly activeTasks: TaskRecord[];
8
+ constructor(eventEmitter: TypedEventEmitter);
9
+ stopAll(): void;
10
+ createTask(name: string, action: () => unknown, interval: Milliseconds, executeRightAway: boolean, ownerName: string): void;
11
+ createOnetimeTask(name: string, action: () => unknown, delay: Milliseconds, ownerName: string): void;
12
+ }
13
+ //# sourceMappingURL=nodeTimeoutScheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nodeTimeoutScheduler.d.ts","sourceRoot":"","sources":["../../services/nodeTimeoutScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAgB,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,qBAAa,oBAAqB,YAAW,UAAU;IAGvC,QAAQ,CAAC,YAAY,EAAE,iBAAiB;IAFpD,QAAQ,CAAC,WAAW,EAAE,UAAU,EAAE,CAAM;gBAEnB,YAAY,EAAE,iBAAiB;IAEpD,OAAO;IAMP,UAAU,CACN,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,OAAO,EACrB,QAAQ,EAAE,YAAY,EACtB,gBAAgB,EAAE,OAAO,EACzB,SAAS,EAAE,MAAM;IAyBrB,iBAAiB,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,OAAO,EACrB,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM;CAkBxB"}
@@ -0,0 +1,50 @@
1
+ import { TaskRecord } from '../entities/taskRecord';
2
+ import { BotEventType } from '../types/events';
3
+ export class NodeTimeoutScheduler {
4
+ eventEmitter;
5
+ activeTasks = [];
6
+ constructor(eventEmitter) {
7
+ this.eventEmitter = eventEmitter;
8
+ }
9
+ stopAll() {
10
+ for (const task of this.activeTasks) {
11
+ clearInterval(task.taskId);
12
+ }
13
+ }
14
+ createTask(name, action, interval, executeRightAway, ownerName) {
15
+ const taskId = setInterval(() => {
16
+ action();
17
+ this.eventEmitter.emit(BotEventType.taskRun, {
18
+ name,
19
+ ownerName,
20
+ interval
21
+ });
22
+ }, interval);
23
+ const task = new TaskRecord(name, taskId, interval);
24
+ if (executeRightAway) {
25
+ setImmediate(action);
26
+ }
27
+ this.eventEmitter.emit(BotEventType.taskCreated, {
28
+ name,
29
+ ownerName,
30
+ interval
31
+ });
32
+ this.activeTasks.push(task);
33
+ }
34
+ createOnetimeTask(name, action, delay, ownerName) {
35
+ const actionWrapper = () => {
36
+ this.eventEmitter.emit(BotEventType.taskRun, {
37
+ name,
38
+ ownerName,
39
+ delay
40
+ });
41
+ action();
42
+ };
43
+ setTimeout(actionWrapper, delay);
44
+ this.eventEmitter.emit(BotEventType.taskCreated, {
45
+ name,
46
+ ownerName,
47
+ delay
48
+ });
49
+ }
50
+ }
@@ -0,0 +1,12 @@
1
+ export type QueueItem = {
2
+ priority: number;
3
+ callback: () => Promise<void>;
4
+ };
5
+ export declare class ResponseProcessingQueue {
6
+ rateLimiter: () => Promise<void>;
7
+ items: QueueItem[];
8
+ isFlushing: boolean;
9
+ enqueue(item: QueueItem): void;
10
+ flushReadyItems(): Promise<void>;
11
+ }
12
+ //# sourceMappingURL=responseProcessingQueue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAQF,qBAAa,uBAAuB;IAChC,WAAW,sBAAwD;IACnE,KAAK,EAAE,SAAS,EAAE,CAAM;IACxB,UAAU,UAAS;IAEnB,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;CAkBxB"}
@@ -0,0 +1,37 @@
1
+ import { RateLimit } from 'async-sema';
2
+ function notEmpty(arr) {
3
+ return arr.length > 0;
4
+ }
5
+ const TELEGRAM_RATELIMIT_DELAY = 35;
6
+ export class ResponseProcessingQueue {
7
+ rateLimiter = RateLimit(1, { timeUnit: TELEGRAM_RATELIMIT_DELAY });
8
+ items = [];
9
+ isFlushing = false;
10
+ enqueue(item) {
11
+ if (this.items.length === 0 ||
12
+ item.priority >= this.items[this.items.length - 1].priority) {
13
+ this.items.push(item);
14
+ return;
15
+ }
16
+ let insertIndex = this.items.length;
17
+ while (insertIndex > 0 &&
18
+ this.items[insertIndex - 1].priority > item.priority) {
19
+ insertIndex--;
20
+ }
21
+ this.items.splice(insertIndex, 0, item);
22
+ }
23
+ async flushReadyItems() {
24
+ if (this.isFlushing)
25
+ return;
26
+ this.isFlushing = true;
27
+ while (notEmpty(this.items)) {
28
+ if (Date.now() >= this.items[0].priority) {
29
+ await this.rateLimiter();
30
+ const [item] = this.items;
31
+ this.items.shift();
32
+ await item.callback();
33
+ }
34
+ }
35
+ this.isFlushing = false;
36
+ }
37
+ }
@@ -0,0 +1,23 @@
1
+ import { IStorageClient } from '../types/storage';
2
+ import { BotResponse } from '../types/response';
3
+ import { IReplyCapture } from '../types/capture';
4
+ import { ChatInfo } from '../dtos/chatInfo';
5
+ import { TelegramApiClient } from '../types/externalAliases';
6
+ import { TypedEventEmitter } from '../types/events';
7
+ export declare const TELEGRAM_ERROR_QUOTE_INVALID = "QUOTE_TEXT_INVALID";
8
+ export declare class TelegramApiService {
9
+ private readonly queue;
10
+ private readonly telegram;
11
+ private readonly storage;
12
+ private readonly eventEmitter;
13
+ private readonly captureRegistrationCallback;
14
+ private readonly botName;
15
+ private readonly methodMap;
16
+ constructor(botName: string, telegram: TelegramApiClient, storage: IStorageClient, eventEmitter: TypedEventEmitter, captureRegistrationCallback: (capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo) => void);
17
+ enqueueBatchedResponses(responses: BotResponse[]): void;
18
+ flushResponses(): void;
19
+ private pinIfShould;
20
+ private processResponse;
21
+ private sendApiRequest;
22
+ }
23
+ //# sourceMappingURL=telegramApi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EACH,WAAW,EAGd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAIjD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAmB,MAAM,0BAA0B,CAAC;AAC9E,OAAO,EAAgB,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAElE,eAAO,MAAM,4BAA4B,uBAAuB,CAAC;AAEjE,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiC;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoB;IAC7C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAoB;IACjD,OAAO,CAAC,QAAQ,CAAC,2BAA2B,CAKlC;IAEV,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAYxB;gBAGE,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,iBAAiB,EAC3B,OAAO,EAAE,cAAc,EACvB,YAAY,EAAE,iBAAiB,EAC/B,2BAA2B,EAAE,CACzB,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,QAAQ,KACjB,IAAI;IASb,uBAAuB,CAAC,SAAS,EAAE,WAAW,EAAE;IAoDhD,cAAc;YAIA,WAAW;YA6BX,eAAe;YAqBf,cAAc;CAkG/B"}
@@ -0,0 +1,163 @@
1
+ import { ResponseProcessingQueue } from './responseProcessingQueue';
2
+ import { BotEventType } from '../types/events';
3
+ export const TELEGRAM_ERROR_QUOTE_INVALID = 'QUOTE_TEXT_INVALID';
4
+ export class TelegramApiService {
5
+ queue = new ResponseProcessingQueue();
6
+ telegram;
7
+ storage;
8
+ eventEmitter;
9
+ captureRegistrationCallback;
10
+ botName;
11
+ methodMap = {
12
+ inlineQuery: 'answerInlineQuery',
13
+ text: 'sendMessage',
14
+ react: 'setMessageReaction',
15
+ unpin: 'unpinChatMessage',
16
+ pin: 'pinChatMessage',
17
+ image: 'sendPhoto',
18
+ video: 'sendVideo',
19
+ delay: null
20
+ };
21
+ constructor(botName, telegram, storage, eventEmitter, captureRegistrationCallback) {
22
+ this.telegram = telegram;
23
+ this.botName = botName;
24
+ this.storage = storage;
25
+ this.eventEmitter = eventEmitter;
26
+ this.captureRegistrationCallback = captureRegistrationCallback;
27
+ }
28
+ enqueueBatchedResponses(responses) {
29
+ let offset = 0;
30
+ for (const response of responses) {
31
+ if (response.kind == 'delay') {
32
+ offset += response.delay;
33
+ continue;
34
+ }
35
+ const queueItem = {
36
+ callback: async () => {
37
+ try {
38
+ await this.processResponse(response);
39
+ }
40
+ catch (error) {
41
+ if ('message' in error) {
42
+ const telegramResponse = error;
43
+ if (telegramResponse.message.includes(TELEGRAM_ERROR_QUOTE_INVALID)) {
44
+ this.eventEmitter.emit(BotEventType.error, {
45
+ error: new Error('Quote error recieved, retrying without quote')
46
+ });
47
+ try {
48
+ await this.processResponse(response, true);
49
+ }
50
+ catch (error) {
51
+ this.eventEmitter.emit(BotEventType.error, {
52
+ error: error
53
+ });
54
+ }
55
+ return;
56
+ }
57
+ }
58
+ this.eventEmitter.emit(BotEventType.error, {
59
+ error: error
60
+ });
61
+ }
62
+ },
63
+ priority: response.createdAt + offset
64
+ };
65
+ this.queue.enqueue(queueItem);
66
+ }
67
+ }
68
+ flushResponses() {
69
+ void this.queue.flushReadyItems();
70
+ }
71
+ async pinIfShould(response, message) {
72
+ if (response.shouldPin) {
73
+ this.eventEmitter.emit(BotEventType.apiRequestSending, {
74
+ response: null,
75
+ telegramMethod: this.methodMap['pin']
76
+ });
77
+ await this.telegram.pinChatMessage(response.chatInfo.id, message.message_id, { disable_notification: true });
78
+ this.eventEmitter.emit(BotEventType.apiRequestSent, {
79
+ response: null,
80
+ telegramMethod: this.methodMap['pin']
81
+ });
82
+ await this.storage.updateStateFor(response.action, response.chatInfo.id, (state) => {
83
+ state.pinnedMessages.push(message.message_id);
84
+ });
85
+ }
86
+ }
87
+ async processResponse(response, ignoreQuote = false) {
88
+ const sentMessage = await this.sendApiRequest(response, ignoreQuote);
89
+ this.eventEmitter.emit(BotEventType.apiRequestSent, {
90
+ response,
91
+ telegramMethod: this.methodMap[response.kind]
92
+ });
93
+ if (sentMessage && 'content' in response) {
94
+ await this.pinIfShould(response, sentMessage);
95
+ for (const capture of response.captures) {
96
+ this.captureRegistrationCallback(capture, sentMessage.message_id, response.chatInfo, response.traceId);
97
+ }
98
+ }
99
+ }
100
+ async sendApiRequest(response, ignoreQuote) {
101
+ this.eventEmitter.emit(BotEventType.apiRequestSending, {
102
+ response,
103
+ telegramMethod: this.methodMap[response.kind]
104
+ });
105
+ switch (response.kind) {
106
+ case 'text':
107
+ return await this.telegram.sendMessage(response.chatInfo.id, response.content, {
108
+ reply_parameters: response.replyInfo
109
+ ? {
110
+ message_id: response.replyInfo.id,
111
+ quote: ignoreQuote
112
+ ? undefined
113
+ : response.replyInfo.quote
114
+ }
115
+ : undefined,
116
+ parse_mode: 'MarkdownV2',
117
+ link_preview_options: {
118
+ is_disabled: response.disableWebPreview
119
+ },
120
+ reply_markup: response.keyboard
121
+ ? {
122
+ inline_keyboard: response.keyboard
123
+ }
124
+ : undefined
125
+ });
126
+ case 'image':
127
+ return await this.telegram.sendPhoto(response.chatInfo.id, response.content,
128
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
129
+ response.replyInfo?.id
130
+ ? {
131
+ reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
132
+ }
133
+ : undefined);
134
+ case 'video':
135
+ return await this.telegram.sendVideo(response.chatInfo.id, response.content,
136
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
137
+ response.replyInfo?.id
138
+ ? {
139
+ reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
140
+ }
141
+ : undefined);
142
+ case 'react':
143
+ await this.telegram.setMessageReaction(response.chatInfo.id, response.messageId, [
144
+ {
145
+ type: 'emoji',
146
+ emoji: response.emoji
147
+ }
148
+ ]);
149
+ return null;
150
+ case 'unpin':
151
+ await this.telegram.unpinChatMessage(response.chatInfo.id, response.messageId);
152
+ await this.storage.updateStateFor(response.action, response.chatInfo.id, (state) => {
153
+ state.pinnedMessages = state.pinnedMessages.filter((x) => x != response.messageId);
154
+ });
155
+ return null;
156
+ case 'inlineQuery':
157
+ await this.telegram.answerInlineQuery(response.queryId, response.queryResults, { cache_time: 0 });
158
+ return null;
159
+ case 'delay':
160
+ return null;
161
+ }
162
+ }
163
+ }
@@ -0,0 +1,14 @@
1
+ import { BaseContextInternal } from '../entities/context/baseContext';
2
+ import { IActionState } from './actionState';
3
+ import { BotResponse } from './response';
4
+ export type ActionKey = string & {
5
+ __brand: 'actionKey';
6
+ };
7
+ export interface IActionWithState<TActionState extends IActionState> extends IAction {
8
+ readonly stateConstructor: () => TActionState;
9
+ }
10
+ export interface IAction {
11
+ readonly key: ActionKey;
12
+ exec(ctx: BaseContextInternal<IAction>): Promise<BotResponse[]>;
13
+ }
14
+ //# sourceMappingURL=action.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"action.d.ts","sourceRoot":"","sources":["../../types/action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAE1D,MAAM,WAAW,gBAAgB,CAAC,YAAY,SAAS,YAAY,CAC/D,SAAQ,OAAO;IACf,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACjD;AAED,MAAM,WAAW,OAAO;IACpB,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,IAAI,CAAC,GAAG,EAAE,mBAAmB,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CACnE"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export interface IActionState {
2
+ lastExecutedDate: number;
3
+ pinnedMessages: number[];
4
+ }
5
+ //# sourceMappingURL=actionState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actionState.d.ts","sourceRoot":"","sources":["../../types/actionState.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IACzB,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,EAAE,MAAM,EAAE,CAAC;CAC5B"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export type CachedValueAccessor = <TResult>(key: string) => Promise<TResult>;
2
+ //# sourceMappingURL=cachedValueAccessor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cachedValueAccessor.d.ts","sourceRoot":"","sources":["../../types/cachedValueAccessor.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ import { ReplyContext, ReplyContextInternal } from '../entities/context/replyContext';
2
+ import { IActionState } from './actionState';
3
+ import { CommandTrigger } from './commandTrigger';
4
+ import { IAction } from './action';
5
+ export interface ICaptureController {
6
+ captureReplies: <TParentActionState extends IActionState>(
7
+ /**
8
+ * Defines action trigger.
9
+ * If `string` or `string[]` is provided, will be triggered only on exact message match.
10
+ * If `RegExp` or `RegExp[]` is provided, will be triggered on successful match.
11
+ */
12
+ trigger: CommandTrigger[],
13
+ /** Callback that will be called on trigger */
14
+ handler: (replyContext: ReplyContext<TParentActionState>) => Promise<void>,
15
+ /** Abort controller to abort capturing manually */
16
+ abortController?: AbortController) => void;
17
+ }
18
+ export interface IReplyCapture {
19
+ trigger: CommandTrigger[];
20
+ handler: (replyContext: ReplyContextInternal<IActionState>) => Promise<void>;
21
+ abortController: AbortController;
22
+ action: IAction;
23
+ }
24
+ //# sourceMappingURL=capture.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../types/capture.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,YAAY,EACZ,oBAAoB,EACvB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC,MAAM,WAAW,kBAAkB;IAC/B,cAAc,EAAE,CAAC,kBAAkB,SAAS,YAAY;IACpD;;;;OAIG;IACH,OAAO,EAAE,cAAc,EAAE;IACzB,8CAA8C;IAC9C,OAAO,EAAE,CACL,YAAY,EAAE,YAAY,CAAC,kBAAkB,CAAC,KAC7C,OAAO,CAAC,IAAI,CAAC;IAClB,mDAAmD;IACnD,eAAe,CAAC,EAAE,eAAe,KAChC,IAAI,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,OAAO,EAAE,CACL,YAAY,EAAE,oBAAoB,CAAC,YAAY,CAAC,KAC/C,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,eAAe,EAAE,eAAe,CAAC;IACjC,MAAM,EAAE,OAAO,CAAC;CACnB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { MessageContext } from '../entities/context/messageContext';
2
+ import { IActionState } from './actionState';
3
+ export type CommandCondition<TActionState extends IActionState> = (
4
+ /** Context of action executed in chat, in response to a message. */
5
+ ctx: MessageContext<TActionState>,
6
+ /** State of an action being executed. */
7
+ state: TActionState) => boolean;
8
+ //# sourceMappingURL=commandCondition.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI;AAC9D,oEAAoE;AACpE,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;AACjC,yCAAyC;AACzC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export type CommandTrigger = string | RegExp;
2
+ //# sourceMappingURL=commandTrigger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandTrigger.d.ts","sourceRoot":"","sources":["../../types/commandTrigger.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,MAAM,CAAC"}
@@ -0,0 +1 @@
1
+ export {};