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.
- package/README.md +9 -10
- package/bun.lock +630 -307
- package/dist/dtos/chatHistoryMessage.d.ts +13 -0
- package/dist/dtos/chatHistoryMessage.d.ts.map +1 -0
- package/dist/dtos/chatHistoryMessage.js +20 -0
- package/dist/dtos/chatInfo.d.ts +3 -3
- package/dist/dtos/chatInfo.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.d.ts +5 -4
- package/dist/dtos/incomingMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.js +1 -1
- package/dist/dtos/messageInfo.d.ts +4 -3
- package/dist/dtos/messageInfo.d.ts.map +1 -1
- package/dist/dtos/responses/imageMessage.d.ts +1 -1
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -1
- package/dist/dtos/responses/inlineQueryResponse.d.ts +1 -1
- package/dist/dtos/responses/inlineQueryResponse.d.ts.map +1 -1
- package/dist/dtos/responses/reaction.d.ts +1 -1
- package/dist/dtos/responses/reaction.d.ts.map +1 -1
- package/dist/dtos/responses/videoMessage.d.ts +1 -1
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -1
- package/dist/entities/botInstance.d.ts +1 -1
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +2 -2
- package/dist/entities/context/inlineQueryContext.d.ts +1 -1
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.d.ts +2 -2
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/replyContext.d.ts +2 -2
- package/dist/entities/context/replyContext.d.ts.map +1 -1
- package/dist/main.d.ts +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +2 -2
- package/dist/services/actionProcessingService.d.ts +2 -2
- package/dist/services/actionProcessingService.d.ts.map +1 -1
- package/dist/services/actionProcessingService.js +13 -11
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +2 -3
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +9 -8
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +2 -2
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +4 -4
- package/dist/services/jsonLogger.d.ts +1 -1
- package/dist/services/jsonLogger.d.ts.map +1 -1
- package/dist/services/jsonLogger.js +33 -22
- package/dist/services/telegramApi.d.ts +2 -2
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +19 -18
- package/dist/types/inputFile.d.ts +5 -0
- package/dist/types/inputFile.d.ts.map +1 -0
- package/dist/types/inputFile.js +2 -0
- package/dist/types/logger.d.ts +2 -2
- package/dist/types/logger.d.ts.map +1 -1
- package/dist/types/messageTypes.d.ts +0 -2
- package/dist/types/messageTypes.d.ts.map +1 -1
- package/dtos/chatHistoryMessage.ts +14 -0
- package/dtos/chatInfo.ts +2 -2
- package/dtos/incomingMessage.ts +9 -14
- package/dtos/messageInfo.ts +3 -5
- package/dtos/responses/imageMessage.ts +1 -1
- package/dtos/responses/inlineQueryResponse.ts +1 -1
- package/dtos/responses/reaction.ts +1 -1
- package/dtos/responses/videoMessage.ts +1 -1
- package/entities/botInstance.ts +2 -2
- package/entities/context/inlineQueryContext.ts +1 -1
- package/entities/context/messageContext.ts +2 -2
- package/entities/context/replyContext.ts +2 -2
- package/main.ts +2 -2
- package/package.json +3 -7
- package/services/actionProcessingService.ts +13 -13
- package/services/actionProcessors/commandActionProcessor.ts +27 -18
- package/services/actionProcessors/inlineQueryActionProcessor.ts +10 -10
- package/services/jsonLogger.ts +42 -27
- package/services/telegramApi.ts +30 -31
- package/types/inputFile.ts +4 -0
- package/types/logger.ts +2 -6
- 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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
14
|
-
return
|
|
15
|
-
if (typeof value
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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:
|
|
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":"
|
|
1
|
+
{"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAkB,MAAM,mBAAmB,CAAC;AAChE,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;AAC5C,OAAO,WAAwB,MAAM,uBAAuB,CAAC;AAG7D,eAAO,MAAM,4BAA4B,uBAAuB,CAAC;AAEjE,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,QAAQ,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,
|
|
61
|
+
async pinIfShould(response, message) {
|
|
61
62
|
if (response.shouldPin) {
|
|
62
|
-
await this.telegram.pinChatMessage(response.chatInfo.id,
|
|
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(
|
|
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
|
|
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
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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,
|
|
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 @@
|
|
|
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"}
|
package/dist/types/logger.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { TraceId } from './trace';
|
|
2
2
|
export interface IScopedLogger {
|
|
3
|
-
logObjectWithTraceId(data:
|
|
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:
|
|
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,
|
|
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,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;IAE1C,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,GACjB,aAAa,CAAC;IAEjB,oBAAoB,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO,GACd,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,CACZ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,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,
|
|
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 {
|
|
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:
|
|
10
|
+
readonly messageHistory: ChatHistoryMessage[]
|
|
11
11
|
) {}
|
|
12
12
|
}
|
package/dtos/incomingMessage.ts
CHANGED
|
@@ -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:
|
|
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:
|
|
39
|
+
ctxMessage: Message,
|
|
45
40
|
botName: string,
|
|
46
|
-
history:
|
|
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:
|
|
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
|
}
|
package/dtos/messageInfo.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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:
|
|
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 '
|
|
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;
|
package/entities/botInstance.ts
CHANGED
|
@@ -83,7 +83,7 @@ export class BotInstance {
|
|
|
83
83
|
);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
async stop(
|
|
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(
|
|
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!:
|
|
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!:
|
|
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(
|
|
76
|
+
async stopBots() {
|
|
77
77
|
for (const bot of this.bots) {
|
|
78
|
-
await bot.stop(
|
|
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.
|
|
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
|
-
"
|
|
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
|
|
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.
|
|
72
|
+
this.telegramBot = new TelegramBot(token, { polling: true });
|
|
73
73
|
const api = new TelegramApiService(
|
|
74
74
|
this.botName,
|
|
75
|
-
this.
|
|
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.
|
|
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) =>
|
|
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.
|
|
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.
|
|
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(
|
|
130
|
-
this.
|
|
129
|
+
async stop() {
|
|
130
|
+
await this.telegramBot.stopPolling();
|
|
131
131
|
}
|
|
132
132
|
}
|