chz-telegram-bot 0.3.29 → 0.4.0

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 (76) hide show
  1. package/README.md +9 -10
  2. package/bun.lock +630 -307
  3. package/dist/dtos/chatHistoryMessage.d.ts +13 -0
  4. package/dist/dtos/chatHistoryMessage.d.ts.map +1 -0
  5. package/dist/dtos/chatHistoryMessage.js +20 -0
  6. package/dist/dtos/chatInfo.d.ts +3 -3
  7. package/dist/dtos/chatInfo.d.ts.map +1 -1
  8. package/dist/dtos/incomingMessage.d.ts +5 -4
  9. package/dist/dtos/incomingMessage.d.ts.map +1 -1
  10. package/dist/dtos/incomingMessage.js +1 -1
  11. package/dist/dtos/messageInfo.d.ts +4 -3
  12. package/dist/dtos/messageInfo.d.ts.map +1 -1
  13. package/dist/dtos/responses/imageMessage.d.ts +1 -1
  14. package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
  15. package/dist/dtos/responses/inlineQueryResponse.d.ts +1 -1
  16. package/dist/dtos/responses/inlineQueryResponse.d.ts.map +1 -1
  17. package/dist/dtos/responses/reaction.d.ts +1 -1
  18. package/dist/dtos/responses/reaction.d.ts.map +1 -1
  19. package/dist/dtos/responses/videoMessage.d.ts +1 -1
  20. package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
  21. package/dist/entities/botInstance.d.ts +1 -1
  22. package/dist/entities/botInstance.d.ts.map +1 -1
  23. package/dist/entities/botInstance.js +2 -2
  24. package/dist/entities/context/inlineQueryContext.d.ts +1 -1
  25. package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
  26. package/dist/entities/context/messageContext.d.ts +2 -2
  27. package/dist/entities/context/messageContext.d.ts.map +1 -1
  28. package/dist/entities/context/replyContext.d.ts +2 -2
  29. package/dist/entities/context/replyContext.d.ts.map +1 -1
  30. package/dist/main.d.ts +1 -1
  31. package/dist/main.d.ts.map +1 -1
  32. package/dist/main.js +2 -2
  33. package/dist/services/actionProcessingService.d.ts +2 -2
  34. package/dist/services/actionProcessingService.d.ts.map +1 -1
  35. package/dist/services/actionProcessingService.js +13 -11
  36. package/dist/services/actionProcessors/commandActionProcessor.d.ts +2 -3
  37. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
  38. package/dist/services/actionProcessors/commandActionProcessor.js +9 -8
  39. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +2 -2
  40. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
  41. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +4 -4
  42. package/dist/services/jsonLogger.d.ts +1 -1
  43. package/dist/services/jsonLogger.d.ts.map +1 -1
  44. package/dist/services/jsonLogger.js +33 -22
  45. package/dist/services/telegramApi.d.ts +2 -2
  46. package/dist/services/telegramApi.d.ts.map +1 -1
  47. package/dist/services/telegramApi.js +19 -18
  48. package/dist/types/inputFile.d.ts +5 -0
  49. package/dist/types/inputFile.d.ts.map +1 -0
  50. package/dist/types/inputFile.js +2 -0
  51. package/dist/types/logger.d.ts +2 -2
  52. package/dist/types/logger.d.ts.map +1 -1
  53. package/dist/types/messageTypes.d.ts +0 -2
  54. package/dist/types/messageTypes.d.ts.map +1 -1
  55. package/dtos/chatHistoryMessage.ts +14 -0
  56. package/dtos/chatInfo.ts +2 -2
  57. package/dtos/incomingMessage.ts +9 -14
  58. package/dtos/messageInfo.ts +3 -5
  59. package/dtos/responses/imageMessage.ts +1 -1
  60. package/dtos/responses/inlineQueryResponse.ts +1 -1
  61. package/dtos/responses/reaction.ts +1 -1
  62. package/dtos/responses/videoMessage.ts +1 -1
  63. package/entities/botInstance.ts +2 -2
  64. package/entities/context/inlineQueryContext.ts +1 -1
  65. package/entities/context/messageContext.ts +2 -2
  66. package/entities/context/replyContext.ts +2 -2
  67. package/main.ts +2 -2
  68. package/package.json +3 -7
  69. package/services/actionProcessingService.ts +13 -13
  70. package/services/actionProcessors/commandActionProcessor.ts +27 -18
  71. package/services/actionProcessors/inlineQueryActionProcessor.ts +10 -10
  72. package/services/jsonLogger.ts +42 -27
  73. package/services/telegramApi.ts +30 -31
  74. package/types/inputFile.ts +4 -0
  75. package/types/logger.ts +2 -6
  76. package/types/messageTypes.ts +0 -4
@@ -3,27 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.JsonLogger = void 0;
4
4
  class JsonLogger {
5
5
  serializeError(error) {
6
- const plainObject = {};
7
- Object.getOwnPropertyNames(error).forEach(function (key) {
8
- plainObject[key] = error[key];
9
- });
10
- return JSON.stringify(plainObject);
6
+ if (error instanceof Error) {
7
+ const plainObject = {
8
+ name: error.name,
9
+ message: error.message,
10
+ stack: error.stack
11
+ };
12
+ for (const [key, value] of Object.entries(error)) {
13
+ plainObject[key] = value;
14
+ }
15
+ return JSON.stringify(plainObject);
16
+ }
17
+ return JSON.stringify({ error });
11
18
  }
12
19
  getCircularReplacer() {
13
- const ancestors = [];
14
- return function (_, value) {
15
- if (typeof value !== 'object' || value === null) {
16
- return value;
17
- }
18
- // `this` is the object that value is contained in,
19
- // i.e., its direct parent.
20
- while (ancestors.length > 0 && ancestors.at(-1) !== this) {
21
- ancestors.pop();
20
+ const cache = new Set();
21
+ return (_, value) => {
22
+ if (typeof value === 'object' && value !== null) {
23
+ if (cache.has(value)) {
24
+ return;
25
+ }
26
+ cache.add(value);
22
27
  }
23
- if (ancestors.includes(value)) {
24
- return '[Circular]';
25
- }
26
- ancestors.push(value);
27
28
  return value;
28
29
  };
29
30
  }
@@ -41,10 +42,20 @@ class JsonLogger {
41
42
  };
42
43
  }
43
44
  logObjectWithTraceId(botName, traceId, chatName, data) {
44
- data.botName = botName;
45
- data.traceId = traceId;
46
- data.chatName = chatName;
47
- console.log(data);
45
+ const enrichedData = typeof data == 'object'
46
+ ? {
47
+ ...data,
48
+ botName,
49
+ traceId,
50
+ chatName
51
+ }
52
+ : {
53
+ botName,
54
+ traceId,
55
+ chatName,
56
+ data
57
+ };
58
+ console.log(enrichedData);
48
59
  }
49
60
  logWithTraceId(botName, traceId, chatName, text) {
50
61
  console.log(`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","text":"${text}"}`);
@@ -1,10 +1,10 @@
1
1
  import { IStorageClient } from '../types/storage';
2
2
  import { BotResponse } from '../types/response';
3
- import { Telegram } from 'telegraf/typings/telegram';
4
3
  import { ILogger } from '../types/logger';
5
4
  import { IReplyCapture } from '../types/capture';
6
5
  import { TraceId } from '../types/trace';
7
6
  import { ChatInfo } from '../dtos/chatInfo';
7
+ import TelegramBot from 'node-telegram-bot-api';
8
8
  export declare const TELEGRAM_ERROR_QUOTE_INVALID = "QUOTE_TEXT_INVALID";
9
9
  export declare class TelegramApiService {
10
10
  private readonly queue;
@@ -13,7 +13,7 @@ export declare class TelegramApiService {
13
13
  private readonly logger;
14
14
  private readonly captureRegistrationCallback;
15
15
  private readonly botName;
16
- constructor(botName: string, telegram: Telegram, storage: IStorageClient, logger: ILogger, captureRegistrationCallback: (capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo, traceId: TraceId) => void);
16
+ constructor(botName: string, telegram: TelegramBot, storage: IStorageClient, logger: ILogger, captureRegistrationCallback: (capture: IReplyCapture, parentMessageId: number, chatInfo: ChatInfo, traceId: TraceId) => void);
17
17
  enqueueBatchedResponses(responses: BotResponse[]): void;
18
18
  flushResponses(): void;
19
19
  private pinIfShould;
@@ -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,gBA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,CAAC,KAAK,CAAiC;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAc;IACvC,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,WAAW,EACrB,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;IAwDhD,cAAc;YAIA,WAAW;YAkBX,eAAe;CAoGhC"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TelegramApiService = exports.TELEGRAM_ERROR_QUOTE_INVALID = void 0;
4
4
  const responseProcessingQueue_1 = require("./responseProcessingQueue");
5
+ const fs_1 = require("fs");
5
6
  exports.TELEGRAM_ERROR_QUOTE_INVALID = 'QUOTE_TEXT_INVALID';
6
7
  class TelegramApiService {
7
8
  queue = new responseProcessingQueue_1.ResponseProcessingQueue();
@@ -57,11 +58,11 @@ class TelegramApiService {
57
58
  flushResponses() {
58
59
  void this.queue.flushReadyItems();
59
60
  }
60
- async pinIfShould(response, sentMessage) {
61
+ async pinIfShould(response, message) {
61
62
  if (response.shouldPin) {
62
- await this.telegram.pinChatMessage(response.chatInfo.id, sentMessage.message_id, { disable_notification: true });
63
+ await this.telegram.pinChatMessage(response.chatInfo.id, message.message_id, { disable_notification: true });
63
64
  await this.storage.updateStateFor(response.action, response.chatInfo.id, (state) => {
64
- state.pinnedMessages.push(sentMessage.message_id);
65
+ state.pinnedMessages.push(message.message_id);
65
66
  });
66
67
  }
67
68
  }
@@ -85,33 +86,33 @@ class TelegramApiService {
85
86
  });
86
87
  break;
87
88
  case 'image':
88
- sentMessage = await this.telegram.sendPhoto(response.chatInfo.id, response.content,
89
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
90
- response.replyInfo?.id
89
+ sentMessage = await this.telegram.sendPhoto(response.chatInfo.id, (0, fs_1.createReadStream)(response.content.source), response.replyInfo?.id
91
90
  ? {
92
- reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ reply_to_message_id: response.replyInfo.id
93
92
  }
94
93
  : undefined);
95
94
  break;
96
95
  case 'video':
97
- sentMessage = await this.telegram.sendVideo(response.chatInfo.id, response.content,
98
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
99
- response.replyInfo?.id
96
+ sentMessage = await this.telegram.sendVideo(response.chatInfo.id, (0, fs_1.createReadStream)(response.content.source), response.replyInfo?.id
100
97
  ? {
101
- reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
+ reply_to_message_id: response.replyInfo.id
102
99
  }
103
100
  : undefined);
104
101
  break;
105
102
  case 'react':
106
- await this.telegram.setMessageReaction(response.chatInfo.id, response.messageId, [
107
- {
108
- type: 'emoji',
109
- emoji: response.emoji
110
- }
111
- ], true);
103
+ await this.telegram.setMessageReaction(response.chatInfo.id, response.messageId, {
104
+ reaction: [
105
+ {
106
+ type: 'emoji',
107
+ emoji: response.emoji
108
+ }
109
+ ]
110
+ });
112
111
  return;
113
112
  case 'unpin':
114
- await this.telegram.unpinChatMessage(response.chatInfo.id, response.messageId);
113
+ await this.telegram.unpinChatMessage(response.chatInfo.id, {
114
+ message_id: response.messageId
115
+ });
115
116
  await this.storage.updateStateFor(response.action, response.chatInfo.id, (state) => {
116
117
  state.pinnedMessages = state.pinnedMessages.filter((x) => x != response.messageId);
117
118
  });
@@ -0,0 +1,5 @@
1
+ export interface InputFile {
2
+ source: string;
3
+ filename?: string;
4
+ }
5
+ //# sourceMappingURL=inputFile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inputFile.d.ts","sourceRoot":"","sources":["../../types/inputFile.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,12 +1,12 @@
1
1
  import { TraceId } from './trace';
2
2
  export interface IScopedLogger {
3
- logObjectWithTraceId(data: any): void;
3
+ logObjectWithTraceId(data: unknown): void;
4
4
  logWithTraceId(text: string): void;
5
5
  errorWithTraceId(errorObj: unknown, extraData?: unknown): void;
6
6
  }
7
7
  export interface ILogger {
8
8
  createScope(botName: string, traceId: TraceId, chatName: string): IScopedLogger;
9
- logObjectWithTraceId(botName: string, traceId: TraceId, chatName: string, data: any): void;
9
+ logObjectWithTraceId(botName: string, traceId: TraceId, chatName: string, data: unknown): void;
10
10
  logWithTraceId(botName: string, traceId: TraceId, chatName: string, text: string): void;
11
11
  errorWithTraceId(botName: string, traceId: TraceId, chatName: string, errorObj: unknown, extraData?: unknown): void;
12
12
  }
@@ -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,aAAa;IAC1B,oBAAoB,CAEhB,IAAI,EAAE,GAAG,GACV,IAAI,CAAC;IAER,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAEnC,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAClE;AAED,MAAM,WAAW,OAAO;IACpB,WAAW,CACP,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM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Q,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,OAAO,GACpB,IAAI,CAAC;CACX"}
@@ -1,4 +1,3 @@
1
- import { Message, Update } from 'telegraf/types';
2
1
  export declare const INTERNAL_MESSAGE_TYPE_PREFIX = "__msg:";
3
2
  export declare const MessageType: {
4
3
  readonly Any: "__msg:Any";
@@ -18,5 +17,4 @@ export declare const MessageType: {
18
17
  readonly Unknown: "__msg:Unknown";
19
18
  };
20
19
  export type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
21
- export type TelegrafContextMessage = Update.New & (Update.NonChannel & Message);
22
20
  //# sourceMappingURL=messageTypes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"messageTypes.d.ts","sourceRoot":"","sources":["../../types/messageTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAEjD,eAAO,MAAM,4BAA4B,WAAW,CAAC;AAErD,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;CAgBd,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;AAE9E,MAAM,MAAM,sBAAsB,GAAG,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC,CAAC"}
1
+ {"version":3,"file":"messageTypes.d.ts","sourceRoot":"","sources":["../../types/messageTypes.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,4BAA4B,WAAW,CAAC;AAErD,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;CAgBd,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { User } from 'node-telegram-bot-api';
2
+ import { MessageTypeValue } from '../types/messageTypes';
3
+ import { TraceId } from '../types/trace';
4
+
5
+ export class ChatHistoryMessage {
6
+ constructor(
7
+ readonly id: number,
8
+ readonly from: User | undefined,
9
+ readonly text: string,
10
+ readonly type: MessageTypeValue,
11
+ readonly traceId: TraceId,
12
+ readonly replyToId: number | undefined
13
+ ) {}
14
+ }
package/dtos/chatInfo.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IncomingMessage } from './incomingMessage';
1
+ import { ChatHistoryMessage } from './chatHistoryMessage';
2
2
 
3
3
  export class ChatInfo {
4
4
  constructor(
@@ -7,6 +7,6 @@ export class ChatInfo {
7
7
  /** Name of a chat that action is executed in. */
8
8
  readonly name: string,
9
9
  /** Last 100 messages in chat where action is executed */
10
- readonly messageHistory: IncomingMessage[]
10
+ readonly messageHistory: ChatHistoryMessage[]
11
11
  ) {}
12
12
  }
@@ -1,13 +1,10 @@
1
- import { Message, Update, User } from 'telegraf/types';
2
1
  import { randomInt } from 'crypto';
3
- import {
4
- MessageType,
5
- MessageTypeValue,
6
- TelegrafContextMessage
7
- } from '../types/messageTypes';
2
+ import { MessageType, MessageTypeValue } from '../types/messageTypes';
8
3
  import { ChatInfo } from './chatInfo';
9
4
  import { createTrace } from '../helpers/traceFactory';
10
5
  import { TraceId } from '../types/trace';
6
+ import { ChatHistoryMessage } from './chatHistoryMessage';
7
+ import { Message, User } from 'node-telegram-bot-api';
11
8
 
12
9
  export class IncomingMessage {
13
10
  readonly messageId: number;
@@ -18,11 +15,9 @@ export class IncomingMessage {
18
15
  readonly traceId: TraceId;
19
16
  readonly replyToMessageId: number | undefined;
20
17
 
21
- readonly updateObject: TelegrafContextMessage;
18
+ readonly updateObject: Message;
22
19
 
23
- private detectMessageType(
24
- message: Update.New & (Update.NonChannel & Message)
25
- ) {
20
+ private detectMessageType(message: Message) {
26
21
  if ('forward_origin' in message) return MessageType.Forward;
27
22
  if ('text' in message) return MessageType.Text;
28
23
  if ('video' in message) return MessageType.Video;
@@ -41,9 +36,9 @@ export class IncomingMessage {
41
36
  }
42
37
 
43
38
  constructor(
44
- ctxMessage: TelegrafContextMessage,
39
+ ctxMessage: Message,
45
40
  botName: string,
46
- history: IncomingMessage[]
41
+ history: ChatHistoryMessage[]
47
42
  ) {
48
43
  this.traceId = createTrace(
49
44
  this,
@@ -68,8 +63,8 @@ export class IncomingMessage {
68
63
  this.updateObject = ctxMessage;
69
64
  }
70
65
 
71
- private getMessageText(ctxMessage: TelegrafContextMessage) {
72
- if ('text' in ctxMessage) return ctxMessage.text;
66
+ private getMessageText(ctxMessage: Message) {
67
+ if ('text' in ctxMessage) return ctxMessage.text ?? '';
73
68
 
74
69
  return 'caption' in ctxMessage ? ctxMessage.caption ?? '' : '';
75
70
  }
@@ -1,7 +1,5 @@
1
- import {
2
- MessageTypeValue,
3
- TelegrafContextMessage
4
- } from '../types/messageTypes';
1
+ import { Message } from 'node-telegram-bot-api';
2
+ import { MessageTypeValue } from '../types/messageTypes';
5
3
 
6
4
  export class MessageInfo {
7
5
  constructor(
@@ -12,6 +10,6 @@ export class MessageInfo {
12
10
  /** Type of message being received */
13
11
  readonly type: MessageTypeValue,
14
12
  /** Message object recieved from Telegram */
15
- readonly telegramUpdateObject: TelegrafContextMessage
13
+ readonly telegramUpdateObject: Message
16
14
  ) {}
17
15
  }
@@ -1,4 +1,3 @@
1
- import { InputFile } from 'telegraf/types';
2
1
  import {
3
2
  BotResponseTypes,
4
3
  IReplyResponseWithContent
@@ -9,6 +8,7 @@ import { ChatInfo } from '../chatInfo';
9
8
  import { TraceId } from '../../types/trace';
10
9
  import { ReplyInfo } from '../replyInfo';
11
10
  import { IReplyCapture } from '../../types/capture';
11
+ import { InputFile } from '../../types/inputFile';
12
12
 
13
13
  export class ImageMessage implements IReplyResponseWithContent<InputFile> {
14
14
  readonly kind = BotResponseTypes.image;
@@ -1,4 +1,4 @@
1
- import { InlineQueryResult } from 'telegraf/types';
1
+ import { InlineQueryResult } from 'node-telegram-bot-api';
2
2
  import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
3
3
  import { BotResponseTypes } from '../../types/response';
4
4
  import { TraceId } from '../../types/trace';
@@ -1,8 +1,8 @@
1
- import { TelegramEmoji } from 'telegraf/types';
2
1
  import { BotResponseTypes, IChatResponse } from '../../types/response';
3
2
  import { IAction } from '../../types/action';
4
3
  import { ChatInfo } from '../chatInfo';
5
4
  import { TraceId } from '../../types/trace';
5
+ import { TelegramEmoji } from 'node-telegram-bot-api';
6
6
 
7
7
  export class Reaction implements IChatResponse {
8
8
  readonly kind = BotResponseTypes.react;
@@ -1,4 +1,3 @@
1
- import { InputFile } from 'telegraf/types';
2
1
  import {
3
2
  BotResponseTypes,
4
3
  IReplyResponseWithContent
@@ -9,6 +8,7 @@ import { ChatInfo } from '../chatInfo';
9
8
  import { TraceId } from '../../types/trace';
10
9
  import { ReplyInfo } from '../replyInfo';
11
10
  import { IReplyCapture } from '../../types/capture';
11
+ import { InputFile } from '../../types/inputFile';
12
12
 
13
13
  export class VideoMessage implements IReplyResponseWithContent<InputFile> {
14
14
  readonly kind = BotResponseTypes.video;
@@ -83,7 +83,7 @@ export class BotInstance {
83
83
  );
84
84
  }
85
85
 
86
- async stop(code: string) {
86
+ async stop() {
87
87
  this.logger.logWithTraceId(
88
88
  this.name,
89
89
  createTrace(this, this.name, 'Stop'),
@@ -93,6 +93,6 @@ export class BotInstance {
93
93
 
94
94
  this.scheduler.stopAll();
95
95
  await this.storage.close();
96
- this.actionProcessingService.stop(code);
96
+ await this.actionProcessingService.stop();
97
97
  }
98
98
  }
@@ -1,4 +1,3 @@
1
- import { InlineQueryResult } from 'telegraf/types';
2
1
  import { BotResponse } from '../../types/response';
3
2
  import { InlineQueryAction } from '../actions/inlineQueryAction';
4
3
  import { InlineQueryResponse } from '../../dtos/responses/inlineQueryResponse';
@@ -6,6 +5,7 @@ import {
6
5
  BaseContextInternal,
7
6
  BaseContextPropertiesToOmit
8
7
  } from './baseContext';
8
+ import { InlineQueryResult } from 'node-telegram-bot-api';
9
9
 
10
10
  export type InlineQueryContext = Omit<
11
11
  InlineQueryContextInternal,
@@ -1,5 +1,4 @@
1
1
  import { resolve } from 'path';
2
- import { TelegramEmoji, UserFromGetMe } from 'telegraf/types';
3
2
  import { IActionState } from '../../types/actionState';
4
3
  import { ImageMessage } from '../../dtos/responses/imageMessage';
5
4
  import { Reaction } from '../../dtos/responses/reaction';
@@ -16,6 +15,7 @@ import { Seconds } from '../../types/timeValues';
16
15
  import { BaseContextPropertiesToOmit } from './baseContext';
17
16
  import { MessageInfo } from '../../dtos/messageInfo';
18
17
  import { UserInfo } from '../../dtos/userInfo';
18
+ import { TelegramEmoji, User } from 'node-telegram-bot-api';
19
19
 
20
20
  export type MessageContext<TActionState extends IActionState> = Omit<
21
21
  MessageContextInternal<TActionState>,
@@ -37,7 +37,7 @@ export class MessageContextInternal<
37
37
  /** Indicates if cooldown should be started after action is executed. Set to `true` by default. */
38
38
  startCooldown: boolean = true;
39
39
  /** Bot info from Telegram */
40
- botInfo!: UserFromGetMe;
40
+ botInfo!: User;
41
41
  customCooldown: Seconds | undefined;
42
42
 
43
43
  private getQuotePart(quote: boolean | string) {
@@ -1,4 +1,3 @@
1
- import { TelegramEmoji, UserFromGetMe } from 'telegraf/types';
2
1
  import { ReplyInfo } from '../../dtos/replyInfo';
3
2
  import { ImageMessage } from '../../dtos/responses/imageMessage';
4
3
  import { Reaction } from '../../dtos/responses/reaction';
@@ -17,6 +16,7 @@ import {
17
16
  } from './baseContext';
18
17
  import { UserInfo } from '../../dtos/userInfo';
19
18
  import { MessageInfo } from '../../dtos/messageInfo';
19
+ import { TelegramEmoji, User } from 'node-telegram-bot-api';
20
20
 
21
21
  export type ReplyContext<TActionState extends IActionState> = Omit<
22
22
  ReplyContextInternal<TActionState>,
@@ -38,7 +38,7 @@ export class ReplyContextInternal<
38
38
  /** Information about the message that triggered this action */
39
39
  messageInfo!: MessageInfo;
40
40
  /** Bot info from Telegram */
41
- botInfo!: UserFromGetMe;
41
+ botInfo!: User;
42
42
 
43
43
  isInitialized = false;
44
44
 
package/main.ts CHANGED
@@ -73,9 +73,9 @@ class BotOrchestrator {
73
73
  /**
74
74
  * Terminates all scheduled tasks, closes storage connections and stops all bots.
75
75
  */
76
- async stopBots(reason: string) {
76
+ async stopBots() {
77
77
  for (const bot of this.bots) {
78
- await bot.stop(reason);
78
+ await bot.stop();
79
79
  }
80
80
  }
81
81
  }
package/package.json CHANGED
@@ -14,16 +14,17 @@
14
14
  "type": "git",
15
15
  "url": "https://github.com/AlexSolari/botFramework.git"
16
16
  },
17
- "version": "0.3.29",
17
+ "version": "0.4.0",
18
18
  "type": "module",
19
19
  "dependencies": {
20
20
  "async-sema": "^3.1.1",
21
21
  "moment": "^2.29.4",
22
- "telegraf": "^4.16.3"
22
+ "node-telegram-bot-api": "^0.66.0"
23
23
  },
24
24
  "main": "dist/index.js",
25
25
  "types": "dist/index.d.ts",
26
26
  "devDependencies": {
27
+ "@types/node-telegram-bot-api": "^0.64.10",
27
28
  "@eslint/js": "^9.29.0",
28
29
  "@types/markdown-escape": "^1.1.3",
29
30
  "@types/node": "^22.5.5",
@@ -35,10 +36,5 @@
35
36
  "scripts": {
36
37
  "build": "tsc",
37
38
  "lint": "npx eslint && tsc --noEmit"
38
- },
39
- "overrides": {
40
- "telegraf": {
41
- "node-fetch": "3.3.2"
42
- }
43
39
  }
44
40
  }
@@ -1,6 +1,5 @@
1
1
  import { hoursToSeconds } from '../helpers/timeConvertions';
2
2
  import { Seconds, Milliseconds, Hours } from '../types/timeValues';
3
- import { Telegraf } from 'telegraf';
4
3
  import { ILogger } from '../types/logger';
5
4
  import { IScheduler } from '../types/scheduler';
6
5
  import { IStorageClient } from '../types/storage';
@@ -13,6 +12,7 @@ import { buildHelpCommand } from '../builtin/helpAction';
13
12
  import { CommandActionProcessor } from './actionProcessors/commandActionProcessor';
14
13
  import { InlineQueryActionProcessor } from './actionProcessors/inlineQueryActionProcessor';
15
14
  import { ScheduledActionProcessor } from './actionProcessors/scheduledActionProcessor';
15
+ import TelegramBot from 'node-telegram-bot-api';
16
16
 
17
17
  export class ActionProcessingService {
18
18
  private readonly storage: IStorageClient;
@@ -24,7 +24,7 @@ export class ActionProcessingService {
24
24
 
25
25
  private readonly botName: string;
26
26
 
27
- private telegraf!: Telegraf;
27
+ private telegramBot!: TelegramBot;
28
28
 
29
29
  constructor(
30
30
  botName: string,
@@ -69,10 +69,10 @@ export class ActionProcessingService {
69
69
  scheduledPeriod?: Seconds,
70
70
  verboseLoggingForIncomingMessage?: boolean
71
71
  ) {
72
- this.telegraf = new Telegraf(token);
72
+ this.telegramBot = new TelegramBot(token, { polling: true });
73
73
  const api = new TelegramApiService(
74
74
  this.botName,
75
- this.telegraf.telegram,
75
+ this.telegramBot,
76
76
  this.storage,
77
77
  this.logger,
78
78
  (capture, id, chatInfo, traceId) => {
@@ -85,13 +85,15 @@ export class ActionProcessingService {
85
85
  }
86
86
  );
87
87
 
88
- const botInfo = await this.telegraf.telegram.getMe();
88
+ const botInfo = await this.telegramBot.getMe();
89
89
  const commandActions =
90
- actions.commands.length > 0
90
+ actions.commands.length > 0 && botInfo.username
91
91
  ? [
92
92
  buildHelpCommand(
93
93
  actions.commands
94
- .map((x) => x.readmeFactory(botInfo.username))
94
+ .map((x) =>
95
+ x.readmeFactory(botInfo.username as string)
96
+ )
95
97
  .filter((x) => !!x),
96
98
  botInfo.username
97
99
  ),
@@ -101,14 +103,14 @@ export class ActionProcessingService {
101
103
 
102
104
  this.commandProcessor.initialize(
103
105
  api,
104
- this.telegraf,
106
+ this.telegramBot,
105
107
  commandActions,
106
108
  verboseLoggingForIncomingMessage ?? false,
107
109
  botInfo
108
110
  );
109
111
  this.inlineQueryProcessor.initialize(
110
112
  api,
111
- this.telegraf,
113
+ this.telegramBot,
112
114
  actions.inlineQueries,
113
115
  300 as Milliseconds
114
116
  );
@@ -122,11 +124,9 @@ export class ActionProcessingService {
122
124
  ...actions.scheduled,
123
125
  ...commandActions
124
126
  ]);
125
-
126
- void this.telegraf.launch();
127
127
  }
128
128
 
129
- stop(code: string) {
130
- this.telegraf.stop(code);
129
+ async stop() {
130
+ await this.telegramBot.stopPolling();
131
131
  }
132
132
  }