chz-telegram-bot 0.3.1 → 0.3.2

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 (55) hide show
  1. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  2. package/dist/entities/actions/commandAction.js +1 -1
  3. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
  4. package/dist/entities/actions/inlineQueryAction.js +1 -1
  5. package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
  6. package/dist/entities/actions/replyCaptureAction.js +1 -1
  7. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  8. package/dist/entities/actions/scheduledAction.js +1 -1
  9. package/dist/entities/context/chatContext.d.ts +10 -4
  10. package/dist/entities/context/chatContext.d.ts.map +1 -1
  11. package/dist/entities/context/chatContext.js +18 -6
  12. package/dist/entities/context/inlineQueryContext.d.ts +4 -4
  13. package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
  14. package/dist/entities/context/inlineQueryContext.js +1 -2
  15. package/dist/entities/context/messageContext.d.ts +1 -8
  16. package/dist/entities/context/messageContext.d.ts.map +1 -1
  17. package/dist/entities/context/messageContext.js +5 -20
  18. package/dist/entities/context/replyContext.d.ts +4 -4
  19. package/dist/entities/context/replyContext.d.ts.map +1 -1
  20. package/dist/entities/context/replyContext.js +1 -2
  21. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
  22. package/dist/services/actionProcessors/commandActionProcessor.js +12 -8
  23. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
  24. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +7 -5
  25. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
  26. package/dist/services/actionProcessors/scheduledActionProcessor.js +3 -2
  27. package/dist/services/jsonLogger.d.ts +3 -2
  28. package/dist/services/jsonLogger.d.ts.map +1 -1
  29. package/dist/services/jsonLogger.js +8 -4
  30. package/dist/services/telegramApi.d.ts +1 -0
  31. package/dist/services/telegramApi.d.ts.map +1 -1
  32. package/dist/services/telegramApi.js +23 -6
  33. package/dist/types/capture.d.ts +11 -1
  34. package/dist/types/capture.d.ts.map +1 -1
  35. package/dist/types/commandCondition.d.ts +5 -1
  36. package/dist/types/commandCondition.d.ts.map +1 -1
  37. package/dist/types/logger.d.ts +6 -0
  38. package/dist/types/logger.d.ts.map +1 -1
  39. package/entities/actions/commandAction.ts +0 -3
  40. package/entities/actions/inlineQueryAction.ts +1 -6
  41. package/entities/actions/replyCaptureAction.ts +0 -3
  42. package/entities/actions/scheduledAction.ts +0 -3
  43. package/entities/context/chatContext.ts +25 -12
  44. package/entities/context/inlineQueryContext.ts +4 -9
  45. package/entities/context/messageContext.ts +5 -34
  46. package/entities/context/replyContext.ts +4 -9
  47. package/package.json +1 -1
  48. package/services/actionProcessors/commandActionProcessor.ts +28 -32
  49. package/services/actionProcessors/inlineQueryActionProcessor.ts +10 -18
  50. package/services/actionProcessors/scheduledActionProcessor.ts +8 -12
  51. package/services/jsonLogger.ts +20 -4
  52. package/services/telegramApi.ts +40 -11
  53. package/types/capture.ts +7 -0
  54. package/types/commandCondition.ts +2 -0
  55. package/types/logger.ts +20 -0
@@ -5,6 +5,7 @@ import { ILogger } from '../types/logger';
5
5
  import { IReplyCapture } from '../types/capture';
6
6
  import { TraceId } from '../types/trace';
7
7
  import { ChatInfo } from '../dtos/chatInfo';
8
+ export declare const TELEGRAM_ERROR_QUOTE_INVALID = "QUOTE_TEXT_INVALID";
8
9
  export declare class TelegramApiService {
9
10
  private readonly queue;
10
11
  private readonly telegram;
@@ -1 +1 @@
1
- {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAkB,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGjD,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiC;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IACjC,OAAO,CAAC,QAAQ,CAAC,2BAA2B,CAKlC;IAEV,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAG7B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,OAAO,EACf,2BAA2B,EAAE,CACzB,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO,KACf,IAAI;IASb,uBAAuB,CAAC,SAAS,EAAE,WAAW,EAAE;IA8BhD,cAAc;YAIA,WAAW;YAkBX,eAAe;CAkGhC"}
1
+ {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAkB,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGjD,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAG5C,eAAO,MAAM,4BAA4B,uBAAuB,CAAC;AAEjE,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiC;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IACjC,OAAO,CAAC,QAAQ,CAAC,2BAA2B,CAKlC;IAEV,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAG7B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,OAAO,EACf,2BAA2B,EAAE,CACzB,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO,KACf,IAAI;IASb,uBAAuB,CAAC,SAAS,EAAE,WAAW,EAAE;IAsDhD,cAAc;YAIA,WAAW;YAkBX,eAAe;CAoGhC"}
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TelegramApiService = void 0;
3
+ exports.TelegramApiService = exports.TELEGRAM_ERROR_QUOTE_INVALID = void 0;
4
4
  const responseProcessingQueue_1 = require("./responseProcessingQueue");
5
+ exports.TELEGRAM_ERROR_QUOTE_INVALID = 'QUOTE_TEXT_INVALID';
5
6
  class TelegramApiService {
6
7
  constructor(botName, telegram, storage, logger, captureRegistrationCallback) {
7
8
  this.queue = new responseProcessingQueue_1.ResponseProcessingQueue();
@@ -20,13 +21,27 @@ class TelegramApiService {
20
21
  }
21
22
  const queueItem = {
22
23
  callback: async () => {
24
+ const scopedLogger = this.logger.createScope(this.botName, response.traceId, 'chatInfo' in response
25
+ ? response.chatInfo.name
26
+ : 'Unknown');
23
27
  try {
24
28
  await this.processResponse(response);
25
29
  }
26
30
  catch (error) {
27
- this.logger.errorWithTraceId(this.botName, response.traceId, 'chatInfo' in response
28
- ? response.chatInfo.name
29
- : 'Unknown', error, response);
31
+ if ('message' in error) {
32
+ const telegramResponse = error;
33
+ if (telegramResponse.message.includes(exports.TELEGRAM_ERROR_QUOTE_INVALID)) {
34
+ scopedLogger.logWithTraceId('Quote error recieved, retrying without quote');
35
+ try {
36
+ await this.processResponse(response, true);
37
+ }
38
+ catch (error) {
39
+ scopedLogger.errorWithTraceId(error, response);
40
+ }
41
+ return;
42
+ }
43
+ }
44
+ scopedLogger.errorWithTraceId(error, response);
30
45
  }
31
46
  },
32
47
  priority: response.createdAt + offset
@@ -45,7 +60,7 @@ class TelegramApiService {
45
60
  });
46
61
  }
47
62
  }
48
- async processResponse(response) {
63
+ async processResponse(response, ignoreQuote = false) {
49
64
  let sentMessage = null;
50
65
  switch (response.kind) {
51
66
  case 'text':
@@ -53,7 +68,9 @@ class TelegramApiService {
53
68
  reply_parameters: response.replyInfo
54
69
  ? {
55
70
  message_id: response.replyInfo.id,
56
- quote: response.replyInfo.quote
71
+ quote: ignoreQuote
72
+ ? undefined
73
+ : response.replyInfo.quote
57
74
  }
58
75
  : undefined,
59
76
  parse_mode: 'MarkdownV2',
@@ -3,7 +3,17 @@ import { IActionState } from './actionState';
3
3
  import { CommandTrigger } from './commandTrigger';
4
4
  import { IActionWithState } from './action';
5
5
  export interface ICaptureController {
6
- captureReplies: <TParentActionState extends IActionState>(trigger: CommandTrigger[], handler: (replyContext: ReplyContext<TParentActionState>) => Promise<void>, abortController: AbortController) => void;
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;
7
17
  }
8
18
  export interface IReplyCapture {
9
19
  trigger: CommandTrigger[];
@@ -1 +1 @@
1
- {"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../types/capture.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,MAAM,WAAW,kBAAkB;IAC/B,cAAc,EAAE,CAAC,kBAAkB,SAAS,YAAY,EACpD,OAAO,EAAE,cAAc,EAAE,EACzB,OAAO,EAAE,CACL,YAAY,EAAE,YAAY,CAAC,kBAAkB,CAAC,KAC7C,OAAO,CAAC,IAAI,CAAC,EAClB,eAAe,EAAE,eAAe,KAC/B,IAAI,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,OAAO,EAAE,CAAC,YAAY,EAAE,YAAY,CAAC,YAAY,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACrE,eAAe,EAAE,eAAe,CAAC;IACjC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;CAC1C"}
1
+ {"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../types/capture.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,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,EAAE,eAAe,KAC/B,IAAI,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,OAAO,EAAE,CAAC,YAAY,EAAE,YAAY,CAAC,YAAY,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACrE,eAAe,EAAE,eAAe,CAAC;IACjC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;CAC1C"}
@@ -1,4 +1,8 @@
1
1
  import { MessageContext } from '../entities/context/messageContext';
2
2
  import { IActionState } from './actionState';
3
- export type CommandCondition<TActionState extends IActionState> = (ctx: MessageContext<TActionState>, state: TActionState) => boolean;
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;
4
8
  //# sourceMappingURL=commandCondition.d.ts.map
@@ -1 +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,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,EACjC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC"}
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"}
@@ -1,5 +1,11 @@
1
1
  import { TraceId } from './trace';
2
+ export interface IScopedLogger {
3
+ logObjectWithTraceId(data: any): void;
4
+ logWithTraceId(text: string): void;
5
+ errorWithTraceId<TData>(errorObj: unknown, extraData?: TData | undefined): void;
6
+ }
2
7
  export interface ILogger {
8
+ createScope(botName: string, traceId: TraceId, chatName: string): IScopedLogger;
3
9
  logObjectWithTraceId(botName: string, traceId: TraceId, chatName: string, data: any): void;
4
10
  logWithTraceId(botName: string, traceId: TraceId, chatName: string, text: string): void;
5
11
  errorWithTraceId<TData>(botName: string, traceId: TraceId, chatName: string, errorObj: unknown, extraData?: TData | undefined): void;
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../types/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,MAAM,WAAW,OAAO;IACpB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,GAAG,GACV,IAAI,CAAC;IAER,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACb,IAAI,CAAC;IAER,gBAAgB,CAAC,KAAK,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS,GAC9B,IAAI,CAAC;CACX"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../types/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,MAAM,WAAW,aAAa;IAC1B,oBAAoB,CAEhB,IAAI,EAAE,GAAG,GACV,IAAI,CAAC;IAER,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAEnC,gBAAgB,CAAC,KAAK,EAClB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS,GAC9B,IAAI,CAAC;CACX;AAED,MAAM,WAAW,OAAO;IACpB,WAAW,CACP,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,GACjB,aAAa,CAAC;IAEjB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,GAAG,GACV,IAAI,CAAC;IAER,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACb,IAAI,CAAC;IAER,gBAAgB,CAAC,KAAK,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS,GAC9B,IAAI,CAAC;CACX"}
@@ -76,9 +76,6 @@ export class CommandAction<TActionState extends IActionState>
76
76
  if (!shouldExecute) return Noop.NoResponse;
77
77
 
78
78
  ctx.logger.logWithTraceId(
79
- ctx.botName,
80
- ctx.traceId,
81
- ctx.chatInfo.name,
82
79
  ` - Executing [${this.name}] in ${ctx.chatInfo.id}`
83
80
  );
84
81
  ctx.matchResults = matchResults;
@@ -53,12 +53,7 @@ export class InlineQueryAction implements IAction {
53
53
 
54
54
  ctx.matchResults = matchResults;
55
55
 
56
- ctx.logger.logWithTraceId(
57
- ctx.botName,
58
- ctx.traceId,
59
- 'Unknown',
60
- ` - Executing [${this.name}]`
61
- );
56
+ ctx.logger.logWithTraceId(` - Executing [${this.name}]`);
62
57
 
63
58
  await this.handler(ctx);
64
59
 
@@ -51,9 +51,6 @@ export class ReplyCaptureAction<TParentActionState extends IActionState>
51
51
  if (!shouldExecute) return Noop.NoResponse;
52
52
 
53
53
  ctx.logger.logWithTraceId(
54
- ctx.botName,
55
- ctx.traceId,
56
- ctx.chatInfo.name,
57
54
  ` - Executing [${this.key}] in ${ctx.chatInfo.id}`
58
55
  );
59
56
  ctx.matchResults = matchResults;
@@ -63,9 +63,6 @@ export class ScheduledAction<TActionState extends IActionState>
63
63
  if (!isAllowedToTrigger) return Noop.NoResponse;
64
64
 
65
65
  ctx.logger.logWithTraceId(
66
- ctx.botName,
67
- ctx.traceId,
68
- ctx.chatInfo.name,
69
66
  ` - Executing [${this.name}] in ${ctx.chatInfo.id}`
70
67
  );
71
68
 
@@ -14,7 +14,7 @@ import { BotResponse, IReplyResponse } from '../../types/response';
14
14
  import { Milliseconds } from '../../types/timeValues';
15
15
  import { DelayResponse } from '../../dtos/responses/delay';
16
16
  import { ChatInfo } from '../../dtos/chatInfo';
17
- import { ILogger } from '../../types/logger';
17
+ import { IScopedLogger } from '../../types/logger';
18
18
  import { IScheduler } from '../../types/scheduler';
19
19
  import { TraceId } from '../../types/trace';
20
20
  import { ICaptureController } from '../../types/capture';
@@ -30,10 +30,10 @@ export class ChatContext<TActionState extends IActionState> {
30
30
  /** Storage client instance for the bot executing this action. */
31
31
  readonly storage: IStorageClient;
32
32
  /** Logger instance for the bot executing this action */
33
- readonly logger: ILogger;
34
33
  /** Scheduler instance for the bot executing this action */
35
34
  readonly scheduler: IScheduler;
36
35
 
36
+ logger!: IScopedLogger;
37
37
  /** Trace id of a action execution. */
38
38
  traceId!: TraceId;
39
39
  /** Name of a bot that executes this action. */
@@ -45,17 +45,12 @@ export class ChatContext<TActionState extends IActionState> {
45
45
 
46
46
  isInitialized = false;
47
47
 
48
- constructor(
49
- storage: IStorageClient,
50
- logger: ILogger,
51
- scheduler: IScheduler
52
- ) {
48
+ constructor(storage: IStorageClient, scheduler: IScheduler) {
53
49
  this.storage = storage;
54
- this.logger = logger;
55
50
  this.scheduler = scheduler;
56
51
  }
57
52
 
58
- protected createCaptureController_TEMP(
53
+ protected createCaptureController(
59
54
  response: IReplyResponse
60
55
  ): ICaptureController {
61
56
  return {
@@ -76,6 +71,24 @@ export class ChatContext<TActionState extends IActionState> {
76
71
  };
77
72
  }
78
73
 
74
+ /**
75
+ * Loads state of another action for current chat.
76
+ * @param action Action to load state of.
77
+ * @template TAnotherActionState - Type of a state that is used by another action.
78
+ */
79
+ async loadStateOf<TAnotherActionState extends IActionState>(
80
+ action: IActionWithState<TAnotherActionState>
81
+ ) {
82
+ const allStates = await this.storage.load(action.key);
83
+ const stateForChat = allStates[this.chatInfo.id];
84
+
85
+ if (!stateForChat) {
86
+ return Object.freeze(action.stateConstructor());
87
+ }
88
+
89
+ return Object.freeze(stateForChat as TAnotherActionState);
90
+ }
91
+
79
92
  /**
80
93
  * Collection of actions that send something to chat as a standalone message.
81
94
  */
@@ -98,7 +111,7 @@ export class ChatContext<TActionState extends IActionState> {
98
111
 
99
112
  this.responses.push(response);
100
113
 
101
- return this.createCaptureController_TEMP(response);
114
+ return this.createCaptureController(response);
102
115
  },
103
116
 
104
117
  /**
@@ -119,7 +132,7 @@ export class ChatContext<TActionState extends IActionState> {
119
132
 
120
133
  this.responses.push(response);
121
134
 
122
- return this.createCaptureController_TEMP(response);
135
+ return this.createCaptureController(response);
123
136
  },
124
137
 
125
138
  /**
@@ -140,7 +153,7 @@ export class ChatContext<TActionState extends IActionState> {
140
153
 
141
154
  this.responses.push(response);
142
155
 
143
- return this.createCaptureController_TEMP(response);
156
+ return this.createCaptureController(response);
144
157
  }
145
158
  };
146
159
 
@@ -1,5 +1,5 @@
1
1
  import { InlineQueryResult } from 'telegraf/types';
2
- import { ILogger } from '../../types/logger';
2
+ import { IScopedLogger } from '../../types/logger';
3
3
  import { BotResponse } from '../../types/response';
4
4
  import { IScheduler } from '../../types/scheduler';
5
5
  import { IStorageClient } from '../../types/storage';
@@ -13,11 +13,11 @@ export class InlineQueryContext {
13
13
 
14
14
  /** Storage client instance for the bot executing this action. */
15
15
  readonly storage: IStorageClient;
16
- /** Logger instance for the bot executing this action */
17
- readonly logger: ILogger;
18
16
  /** Scheduler instance for the bot executing this action */
19
17
  readonly scheduler: IScheduler;
20
18
 
19
+ /** Logger instance for the bot executing this action */
20
+ logger!: IScopedLogger;
21
21
  /** Trace id of a action execution. */
22
22
  traceId!: TraceId;
23
23
  /** Name of a bot that executes this action. */
@@ -48,13 +48,8 @@ export class InlineQueryContext {
48
48
 
49
49
  isInitialized = false;
50
50
 
51
- constructor(
52
- storage: IStorageClient,
53
- logger: ILogger,
54
- scheduler: IScheduler
55
- ) {
51
+ constructor(storage: IStorageClient, scheduler: IScheduler) {
56
52
  this.storage = storage;
57
- this.logger = logger;
58
53
  this.scheduler = scheduler;
59
54
  }
60
55
 
@@ -6,18 +6,15 @@ import { ImageMessage } from '../../dtos/responses/imageMessage';
6
6
  import { Reaction } from '../../dtos/responses/reaction';
7
7
  import { TextMessage } from '../../dtos/responses/textMessage';
8
8
  import { VideoMessage } from '../../dtos/responses/videoMessage';
9
- import { ActionStateBase } from '../states/actionStateBase';
10
9
  import { ChatContext } from './chatContext';
11
10
  import {
12
11
  MessageSendingOptions,
13
12
  TextMessageSendingOptions
14
13
  } from '../../types/messageSendingOptions';
15
- import { ActionKey } from '../../types/action';
16
14
  import {
17
15
  MessageTypeValue,
18
16
  TelegrafContextMessage
19
17
  } from '../../types/messageTypes';
20
- import { ILogger } from '../../types/logger';
21
18
  import { IScheduler } from '../../types/scheduler';
22
19
  import { ReplyInfo } from '../../dtos/replyInfo';
23
20
 
@@ -44,12 +41,8 @@ export class MessageContext<
44
41
  /** Message object recieved from Telegram */
45
42
  messageUpdateObject!: TelegrafContextMessage;
46
43
 
47
- constructor(
48
- storage: IStorageClient,
49
- logger: ILogger,
50
- scheduler: IScheduler
51
- ) {
52
- super(storage, logger, scheduler);
44
+ constructor(storage: IStorageClient, scheduler: IScheduler) {
45
+ super(storage, scheduler);
53
46
  }
54
47
 
55
48
  private replyWithText(
@@ -73,7 +66,7 @@ export class MessageContext<
73
66
 
74
67
  this.responses.push(response);
75
68
 
76
- return this.createCaptureController_TEMP(response);
69
+ return this.createCaptureController(response);
77
70
  }
78
71
 
79
72
  private replyWithImage(
@@ -97,7 +90,7 @@ export class MessageContext<
97
90
 
98
91
  this.responses.push(response);
99
92
 
100
- return this.createCaptureController_TEMP(response);
93
+ return this.createCaptureController(response);
101
94
  }
102
95
 
103
96
  private replyWithVideo(
@@ -121,29 +114,7 @@ export class MessageContext<
121
114
 
122
115
  this.responses.push(response);
123
116
 
124
- return this.createCaptureController_TEMP(response);
125
- }
126
-
127
- /**
128
- * Loads state of another action. Changes to the loaded state will no affect actual state of other action.
129
- * @param commandName Name of an action to load state of.
130
- * @template TAnotherActionState - Type of a state that is used by another action.
131
- */
132
- async loadStateOf<TAnotherActionState extends IActionState>(
133
- commandName: string
134
- ): Promise<TAnotherActionState> {
135
- const storageKey = `command:${commandName.replace(
136
- '.',
137
- '-'
138
- )}` as ActionKey;
139
- const allStates = await this.storage.load(storageKey);
140
- const stateForChat = allStates[this.chatInfo.id];
141
-
142
- if (!stateForChat) {
143
- return new ActionStateBase() as TAnotherActionState;
144
- }
145
-
146
- return stateForChat as TAnotherActionState;
117
+ return this.createCaptureController(response);
147
118
  }
148
119
 
149
120
  /**
@@ -6,7 +6,7 @@ import { Reaction } from '../../dtos/responses/reaction';
6
6
  import { TextMessage } from '../../dtos/responses/textMessage';
7
7
  import { VideoMessage } from '../../dtos/responses/videoMessage';
8
8
  import { IActionState } from '../../types/actionState';
9
- import { ILogger } from '../../types/logger';
9
+ import { IScopedLogger } from '../../types/logger';
10
10
  import {
11
11
  TextMessageSendingOptions,
12
12
  MessageSendingOptions
@@ -27,8 +27,6 @@ export class ReplyContext<TParentActionState extends IActionState> {
27
27
 
28
28
  /** Storage client instance for the bot executing this action. */
29
29
  readonly storage: IStorageClient;
30
- /** Logger instance for the bot executing this action */
31
- readonly logger: ILogger;
32
30
  /** Scheduler instance for the bot executing this action */
33
31
  readonly scheduler: IScheduler;
34
32
 
@@ -36,6 +34,8 @@ export class ReplyContext<TParentActionState extends IActionState> {
36
34
  traceId!: TraceId;
37
35
  /** Name of a bot that executes this action. */
38
36
  botName!: string;
37
+ /** Logger instance for the bot executing this action */
38
+ logger!: IScopedLogger;
39
39
 
40
40
  /** Ordered collection of responses to be processed */
41
41
  responses: BotResponse[] = [];
@@ -60,13 +60,8 @@ export class ReplyContext<TParentActionState extends IActionState> {
60
60
 
61
61
  isInitialized = false;
62
62
 
63
- constructor(
64
- storage: IStorageClient,
65
- logger: ILogger,
66
- scheduler: IScheduler
67
- ) {
63
+ constructor(storage: IStorageClient, scheduler: IScheduler) {
68
64
  this.storage = storage;
69
- this.logger = logger;
70
65
  this.scheduler = scheduler;
71
66
  }
72
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -75,18 +75,16 @@ export class CommandActionProcessor {
75
75
  this.botName
76
76
  );
77
77
 
78
+ const logger = this.logger.createScope(
79
+ this.botName,
80
+ msg.traceId,
81
+ msg.chatInfo.name
82
+ );
83
+
78
84
  if (verboseLoggingForIncomingMessage) {
79
- this.logger.logObjectWithTraceId(
80
- this.botName,
81
- msg.traceId,
82
- msg.chatInfo.name,
83
- ctx.update.message
84
- );
85
+ logger.logObjectWithTraceId(ctx.update.message);
85
86
  } else {
86
- this.logger.logWithTraceId(
87
- this.botName,
88
- msg.traceId,
89
- msg.chatInfo.name,
87
+ logger.logWithTraceId(
90
88
  `${msg.from?.first_name ?? 'Unknown'} (${
91
89
  msg.from?.id ?? 'Unknown'
92
90
  }): ${msg.text || `<non-text message: ${msg.type}>`}`
@@ -118,10 +116,13 @@ export class CommandActionProcessor {
118
116
  capture.abortController
119
117
  );
120
118
 
121
- this.logger.logWithTraceId(
119
+ const logger = this.logger.createScope(
122
120
  this.botName,
123
121
  traceId,
124
- chatInfo.name,
122
+ chatInfo.name
123
+ );
124
+
125
+ logger.logWithTraceId(
125
126
  `Starting capturing replies to message ${parentMessageId} with action ${replyAction.key}`
126
127
  );
127
128
 
@@ -131,10 +132,7 @@ export class CommandActionProcessor {
131
132
  const index = this.replyCaptures.indexOf(replyAction);
132
133
  this.replyCaptures.splice(index, 1);
133
134
 
134
- this.logger.logWithTraceId(
135
- this.botName,
136
- traceId,
137
- chatInfo.name,
135
+ logger.logWithTraceId(
138
136
  `Stopping capturing replies to message ${parentMessageId} with action ${replyAction.key}`
139
137
  );
140
138
  });
@@ -143,7 +141,6 @@ export class CommandActionProcessor {
143
141
  private async processMessage(msg: IncomingMessage) {
144
142
  const ctx = new MessageContext<IActionState>(
145
143
  this.storage,
146
- this.logger,
147
144
  this.scheduler
148
145
  );
149
146
 
@@ -155,19 +152,12 @@ export class CommandActionProcessor {
155
152
  this.api.enqueueBatchedResponses(responses);
156
153
  ctx.isInitialized = false;
157
154
  } catch (error) {
158
- this.logger.errorWithTraceId(
159
- ctx.botName,
160
- ctx.traceId,
161
- ctx.chatInfo.name,
162
- error,
163
- ctx
164
- );
155
+ ctx.logger.errorWithTraceId(error, ctx);
165
156
  }
166
157
  }
167
158
 
168
159
  const replyCtx = new ReplyContext<IActionState>(
169
160
  this.storage,
170
- this.logger,
171
161
  this.scheduler
172
162
  );
173
163
 
@@ -179,13 +169,7 @@ export class CommandActionProcessor {
179
169
  this.api.enqueueBatchedResponses(responses);
180
170
  replyCtx.isInitialized = false;
181
171
  } catch (error) {
182
- this.logger.errorWithTraceId(
183
- replyCtx.botName,
184
- replyCtx.traceId,
185
- replyCtx.chatInfo.name,
186
- error,
187
- replyCtx
188
- );
172
+ replyCtx.logger.errorWithTraceId(error, replyCtx);
189
173
  }
190
174
  }
191
175
 
@@ -213,6 +197,12 @@ export class CommandActionProcessor {
213
197
 
214
198
  ctx.isInitialized = true;
215
199
  ctx.matchResults = [];
200
+
201
+ ctx.logger = this.logger.createScope(
202
+ this.botName,
203
+ message.traceId,
204
+ message.chatInfo.name
205
+ );
216
206
  }
217
207
 
218
208
  private initializeMessageContext<TActionState extends IActionState>(
@@ -238,5 +228,11 @@ export class CommandActionProcessor {
238
228
  ctx.action = action;
239
229
  ctx.chatInfo = message.chatInfo;
240
230
  ctx.traceId = message.traceId;
231
+
232
+ ctx.logger = this.logger.createScope(
233
+ this.botName,
234
+ message.traceId,
235
+ message.chatInfo.name
236
+ );
241
237
  }
242
238
  }
@@ -57,10 +57,13 @@ export class InlineQueryActionProcessor {
57
57
  createTrace('InlineQuery', this.botName, ctx.inlineQuery.id)
58
58
  );
59
59
 
60
- this.logger.logWithTraceId(
60
+ const logger = this.logger.createScope(
61
61
  this.botName,
62
62
  query.traceId,
63
- 'Query',
63
+ 'Query'
64
+ );
65
+
66
+ logger.logWithTraceId(
64
67
  `${ctx.inlineQuery.from.username} (${ctx.inlineQuery.from.id}): Query for ${ctx.inlineQuery.query}`
65
68
  );
66
69
 
@@ -68,10 +71,7 @@ export class InlineQueryActionProcessor {
68
71
  query.userId
69
72
  );
70
73
  if (queryBeingProcessed) {
71
- this.logger.logWithTraceId(
72
- this.botName,
73
- query.traceId,
74
- 'Query',
74
+ logger.logWithTraceId(
75
75
  `Aborting query ${queryBeingProcessed.queryId} (${queryBeingProcessed.query}): new query recieved from ${query.userId}`
76
76
  );
77
77
 
@@ -91,7 +91,6 @@ export class InlineQueryActionProcessor {
91
91
  async () => {
92
92
  const ctx = new InlineQueryContext(
93
93
  this.storage,
94
- this.logger,
95
94
  this.scheduler
96
95
  );
97
96
 
@@ -124,20 +123,11 @@ export class InlineQueryActionProcessor {
124
123
  const error = err as Error;
125
124
 
126
125
  if (error.name == 'AbortError') {
127
- this.logger.logWithTraceId(
128
- this.botName,
129
- inlineQuery.traceId,
130
- 'Query',
126
+ ctx.logger.logWithTraceId(
131
127
  `Aborting query ${inlineQuery.queryId} (${inlineQuery.query}) successful.`
132
128
  );
133
129
  } else {
134
- this.logger.errorWithTraceId(
135
- ctx.botName,
136
- ctx.traceId,
137
- 'Unknown',
138
- error,
139
- ctx
140
- );
130
+ ctx.logger.errorWithTraceId(error, ctx);
141
131
  }
142
132
  }
143
133
  }
@@ -172,5 +162,7 @@ export class InlineQueryActionProcessor {
172
162
  ctx.isInitialized = true;
173
163
  ctx.queryResults = [];
174
164
  ctx.matchResults = [];
165
+
166
+ ctx.logger = this.logger.createScope(this.botName, traceId, 'Unknown');
175
167
  }
176
168
  }