chz-telegram-bot 0.3.30 → 0.5.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 +1 -7
- package/dist/dtos/chatHistoryMessage.d.ts +3 -3
- package/dist/dtos/chatHistoryMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.d.ts +5 -5
- package/dist/dtos/incomingMessage.d.ts.map +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 +3 -3
- 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 +3 -3
- 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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- 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 +11 -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 +7 -7
- 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 +4 -4
- package/dist/types/externalAliases.d.ts +11 -0
- package/dist/types/externalAliases.d.ts.map +1 -0
- package/dist/types/externalAliases.js +2 -0
- 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 +2 -2
- package/dtos/incomingMessage.ts +7 -13
- package/dtos/messageInfo.ts +3 -5
- package/dtos/responses/imageMessage.ts +1 -1
- package/dtos/responses/inlineQueryResponse.ts +3 -3
- 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 +3 -3
- package/entities/context/messageContext.ts +2 -2
- package/entities/context/replyContext.ts +2 -2
- package/index.ts +1 -0
- package/main.ts +2 -2
- package/package.json +37 -44
- package/services/actionProcessingService.ts +13 -12
- package/services/actionProcessors/commandActionProcessor.ts +15 -16
- package/services/actionProcessors/inlineQueryActionProcessor.ts +10 -10
- package/services/jsonLogger.ts +42 -27
- package/services/telegramApi.ts +15 -13
- package/types/externalAliases.ts +17 -0
- package/types/inputFile.ts +4 -0
- package/types/logger.ts +2 -6
- package/types/messageTypes.ts +0 -4
package/package.json
CHANGED
|
@@ -1,44 +1,37 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "chz-telegram-bot",
|
|
3
|
-
"description": "Opinionated TypeScript framework that provides a structured approach to building Telegram bots.",
|
|
4
|
-
"author": {
|
|
5
|
-
"name": "Alex Halanin",
|
|
6
|
-
"url": "https://github.com/AlexSolari"
|
|
7
|
-
},
|
|
8
|
-
"license": "MIT",
|
|
9
|
-
"keywords": [
|
|
10
|
-
"telegram",
|
|
11
|
-
"telegram bot"
|
|
12
|
-
],
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "https://github.com/AlexSolari/botFramework.git"
|
|
16
|
-
},
|
|
17
|
-
"version": "0.
|
|
18
|
-
"type": "module",
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"async-sema": "^3.1.1",
|
|
21
|
-
"moment": "^2.29.4",
|
|
22
|
-
"telegraf": "^4.16.3"
|
|
23
|
-
},
|
|
24
|
-
"main": "dist/index.js",
|
|
25
|
-
"types": "dist/index.d.ts",
|
|
26
|
-
"devDependencies": {
|
|
27
|
-
"@eslint/js": "^9.29.0",
|
|
28
|
-
"@types/
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
"overrides": {
|
|
40
|
-
"telegraf": {
|
|
41
|
-
"node-fetch": "3.3.2"
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "chz-telegram-bot",
|
|
3
|
+
"description": "Opinionated TypeScript framework that provides a structured approach to building Telegram bots.",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Alex Halanin",
|
|
6
|
+
"url": "https://github.com/AlexSolari"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"telegram",
|
|
11
|
+
"telegram bot"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/AlexSolari/botFramework.git"
|
|
16
|
+
},
|
|
17
|
+
"version": "0.5.0",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"async-sema": "^3.1.1",
|
|
21
|
+
"moment": "^2.29.4",
|
|
22
|
+
"telegraf": "^4.16.3"
|
|
23
|
+
},
|
|
24
|
+
"main": "dist/index.js",
|
|
25
|
+
"types": "dist/index.d.ts",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@eslint/js": "^9.29.0",
|
|
28
|
+
"@types/node": "^22.5.5",
|
|
29
|
+
"eslint": "^9.29.0",
|
|
30
|
+
"typescript": "^5.9.0-beta",
|
|
31
|
+
"typescript-eslint": "^8.34.1"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc",
|
|
35
|
+
"lint": "npx eslint && tsc --noEmit"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -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,8 @@ 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 '../types/externalAliases';
|
|
16
|
+
import { Telegraf } from 'telegraf';
|
|
16
17
|
|
|
17
18
|
export class ActionProcessingService {
|
|
18
19
|
private readonly storage: IStorageClient;
|
|
@@ -24,7 +25,7 @@ export class ActionProcessingService {
|
|
|
24
25
|
|
|
25
26
|
private readonly botName: string;
|
|
26
27
|
|
|
27
|
-
private
|
|
28
|
+
private telegramBot!: TelegramBot;
|
|
28
29
|
|
|
29
30
|
constructor(
|
|
30
31
|
botName: string,
|
|
@@ -69,10 +70,10 @@ export class ActionProcessingService {
|
|
|
69
70
|
scheduledPeriod?: Seconds,
|
|
70
71
|
verboseLoggingForIncomingMessage?: boolean
|
|
71
72
|
) {
|
|
72
|
-
this.
|
|
73
|
+
this.telegramBot = new Telegraf(token);
|
|
73
74
|
const api = new TelegramApiService(
|
|
74
75
|
this.botName,
|
|
75
|
-
this.
|
|
76
|
+
this.telegramBot.telegram,
|
|
76
77
|
this.storage,
|
|
77
78
|
this.logger,
|
|
78
79
|
(capture, id, chatInfo, traceId) => {
|
|
@@ -85,9 +86,9 @@ export class ActionProcessingService {
|
|
|
85
86
|
}
|
|
86
87
|
);
|
|
87
88
|
|
|
88
|
-
const botInfo = await this.
|
|
89
|
+
const botInfo = await this.telegramBot.telegram.getMe();
|
|
89
90
|
const commandActions =
|
|
90
|
-
actions.commands.length > 0
|
|
91
|
+
actions.commands.length > 0 && botInfo.username
|
|
91
92
|
? [
|
|
92
93
|
buildHelpCommand(
|
|
93
94
|
actions.commands
|
|
@@ -101,14 +102,14 @@ export class ActionProcessingService {
|
|
|
101
102
|
|
|
102
103
|
this.commandProcessor.initialize(
|
|
103
104
|
api,
|
|
104
|
-
this.
|
|
105
|
+
this.telegramBot,
|
|
105
106
|
commandActions,
|
|
106
107
|
verboseLoggingForIncomingMessage ?? false,
|
|
107
108
|
botInfo
|
|
108
109
|
);
|
|
109
110
|
this.inlineQueryProcessor.initialize(
|
|
110
111
|
api,
|
|
111
|
-
this.
|
|
112
|
+
this.telegramBot,
|
|
112
113
|
actions.inlineQueries,
|
|
113
114
|
300 as Milliseconds
|
|
114
115
|
);
|
|
@@ -118,15 +119,15 @@ export class ActionProcessingService {
|
|
|
118
119
|
scheduledPeriod ?? hoursToSeconds(1 as Hours)
|
|
119
120
|
);
|
|
120
121
|
|
|
122
|
+
void this.telegramBot.launch();
|
|
123
|
+
|
|
121
124
|
void this.storage.saveMetadata([
|
|
122
125
|
...actions.scheduled,
|
|
123
126
|
...commandActions
|
|
124
127
|
]);
|
|
125
|
-
|
|
126
|
-
void this.telegraf.launch();
|
|
127
128
|
}
|
|
128
129
|
|
|
129
|
-
stop(
|
|
130
|
-
this.
|
|
130
|
+
stop() {
|
|
131
|
+
this.telegramBot.stop();
|
|
131
132
|
}
|
|
132
133
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Telegraf } from 'telegraf';
|
|
2
1
|
import { IncomingMessage } from '../../dtos/incomingMessage';
|
|
3
2
|
import { CommandAction } from '../../entities/actions/commandAction';
|
|
4
3
|
import { ReplyCaptureAction } from '../../entities/actions/replyCaptureAction';
|
|
@@ -15,18 +14,18 @@ import {
|
|
|
15
14
|
} from '../../types/messageTypes';
|
|
16
15
|
import { typeSafeObjectFromEntries } from '../../helpers/objectFromEntries';
|
|
17
16
|
import { BaseActionProcessor } from './baseProcessor';
|
|
18
|
-
import { UserFromGetMe } from 'telegraf/types';
|
|
19
17
|
import { getOrSetIfNotExists } from '../../helpers/mapUtils';
|
|
20
18
|
import { MessageInfo } from '../../dtos/messageInfo';
|
|
21
19
|
import { UserInfo } from '../../dtos/userInfo';
|
|
22
20
|
import { ChatHistoryMessage } from '../../dtos/chatHistoryMessage';
|
|
21
|
+
import { BotInfo, TelegramBot } from '../../types/externalAliases';
|
|
23
22
|
|
|
24
23
|
const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
|
|
25
24
|
|
|
26
25
|
export class CommandActionProcessor extends BaseActionProcessor {
|
|
27
26
|
private readonly replyCaptures: ReplyCaptureAction<IActionState>[] = [];
|
|
28
27
|
private readonly chatHistory = new Map<number, ChatHistoryMessage[]>();
|
|
29
|
-
private botInfo!:
|
|
28
|
+
private botInfo!: BotInfo;
|
|
30
29
|
|
|
31
30
|
private commands = typeSafeObjectFromEntries(
|
|
32
31
|
Object.values(MessageType).map((x) => [
|
|
@@ -37,10 +36,10 @@ export class CommandActionProcessor extends BaseActionProcessor {
|
|
|
37
36
|
|
|
38
37
|
initialize(
|
|
39
38
|
api: TelegramApiService,
|
|
40
|
-
|
|
39
|
+
telegram: TelegramBot,
|
|
41
40
|
commands: CommandAction<IActionState>[],
|
|
42
41
|
verboseLoggingForIncomingMessage: boolean,
|
|
43
|
-
botInfo:
|
|
42
|
+
botInfo: BotInfo
|
|
44
43
|
) {
|
|
45
44
|
this.botInfo = botInfo;
|
|
46
45
|
this.initializeDependencies(api);
|
|
@@ -71,30 +70,30 @@ export class CommandActionProcessor extends BaseActionProcessor {
|
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
if (commands.length > 0) {
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
73
|
+
telegram.on('message', ({ message }) => {
|
|
74
|
+
const internalMessage = new IncomingMessage(
|
|
75
|
+
message,
|
|
77
76
|
this.botName,
|
|
78
|
-
getOrSetIfNotExists(this.chatHistory,
|
|
77
|
+
getOrSetIfNotExists(this.chatHistory, message.chat.id, [])
|
|
79
78
|
);
|
|
80
79
|
|
|
81
80
|
const logger = this.logger.createScope(
|
|
82
81
|
this.botName,
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
internalMessage.traceId,
|
|
83
|
+
internalMessage.chatInfo.name
|
|
85
84
|
);
|
|
86
85
|
|
|
87
86
|
if (verboseLoggingForIncomingMessage) {
|
|
88
|
-
logger.logObjectWithTraceId(
|
|
87
|
+
logger.logObjectWithTraceId(message);
|
|
89
88
|
} else {
|
|
90
89
|
logger.logWithTraceId(
|
|
91
|
-
`${
|
|
92
|
-
|
|
93
|
-
}): ${
|
|
90
|
+
`${internalMessage.from?.first_name ?? 'Unknown'} (${
|
|
91
|
+
internalMessage.from?.id ?? 'Unknown'
|
|
92
|
+
}): ${internalMessage.text || internalMessage.type}`
|
|
94
93
|
);
|
|
95
94
|
}
|
|
96
95
|
|
|
97
|
-
void this.processMessage(
|
|
96
|
+
void this.processMessage(internalMessage);
|
|
98
97
|
});
|
|
99
98
|
}
|
|
100
99
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Telegraf } from 'telegraf';
|
|
2
1
|
import { IncomingInlineQuery } from '../../dtos/incomingQuery';
|
|
3
2
|
import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
|
|
4
3
|
import { InlineQueryContextInternal } from '../../entities/context/inlineQueryContext';
|
|
5
4
|
import { createTrace } from '../../helpers/traceFactory';
|
|
5
|
+
import { TelegramBot } from '../../types/externalAliases';
|
|
6
6
|
import { Milliseconds } from '../../types/timeValues';
|
|
7
7
|
import { TraceId } from '../../types/trace';
|
|
8
8
|
import { TelegramApiService } from '../telegramApi';
|
|
@@ -13,7 +13,7 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
|
13
13
|
|
|
14
14
|
initialize(
|
|
15
15
|
api: TelegramApiService,
|
|
16
|
-
|
|
16
|
+
telegram: TelegramBot,
|
|
17
17
|
inlineQueries: InlineQueryAction[],
|
|
18
18
|
period: Milliseconds
|
|
19
19
|
) {
|
|
@@ -25,12 +25,12 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
|
25
25
|
const queriesInProcessing = new Map<number, IncomingInlineQuery>();
|
|
26
26
|
|
|
27
27
|
if (this.inlineQueries.length > 0) {
|
|
28
|
-
|
|
28
|
+
telegram.on('inline_query', ({ inlineQuery }) => {
|
|
29
29
|
const query = new IncomingInlineQuery(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
createTrace('InlineQuery', this.botName,
|
|
30
|
+
inlineQuery.id,
|
|
31
|
+
inlineQuery.query,
|
|
32
|
+
inlineQuery.from.id,
|
|
33
|
+
createTrace('InlineQuery', this.botName, inlineQuery.id)
|
|
34
34
|
);
|
|
35
35
|
|
|
36
36
|
const logger = this.logger.createScope(
|
|
@@ -40,9 +40,9 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
|
40
40
|
);
|
|
41
41
|
|
|
42
42
|
logger.logWithTraceId(
|
|
43
|
-
`${
|
|
44
|
-
|
|
45
|
-
}): Query for ${
|
|
43
|
+
`${inlineQuery.from.username ?? 'Unknown'} (${
|
|
44
|
+
inlineQuery.from.id
|
|
45
|
+
}): Query for ${inlineQuery.query}`
|
|
46
46
|
);
|
|
47
47
|
|
|
48
48
|
const queryBeingProcessed = queriesInProcessing.get(
|
package/services/jsonLogger.ts
CHANGED
|
@@ -1,39 +1,42 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
1
|
import { ILogger, IScopedLogger } from '../types/logger';
|
|
4
2
|
import { TraceId } from '../types/trace';
|
|
5
3
|
|
|
6
4
|
export class JsonLogger implements ILogger {
|
|
7
|
-
private serializeError(error:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
private serializeError(error: unknown): string {
|
|
6
|
+
if (error instanceof Error) {
|
|
7
|
+
const plainObject: Record<string, unknown> = {
|
|
8
|
+
name: error.name,
|
|
9
|
+
message: error.message,
|
|
10
|
+
stack: error.stack
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
for (const [key, value] of Object.entries(error)) {
|
|
14
|
+
plainObject[key] = value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return JSON.stringify(plainObject);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return JSON.stringify({ error });
|
|
13
21
|
}
|
|
14
22
|
|
|
15
23
|
private getCircularReplacer() {
|
|
16
|
-
const
|
|
17
|
-
return
|
|
18
|
-
if (typeof value
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
ancestors.pop();
|
|
25
|
-
}
|
|
26
|
-
if (ancestors.includes(value)) {
|
|
27
|
-
return '[Circular]';
|
|
24
|
+
const cache = new Set();
|
|
25
|
+
return <T>(_: string, value: T) => {
|
|
26
|
+
if (typeof value === 'object' && value !== null) {
|
|
27
|
+
if (cache.has(value)) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
cache.add(value);
|
|
28
32
|
}
|
|
29
|
-
ancestors.push(value);
|
|
30
33
|
return value;
|
|
31
34
|
};
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
createScope(botName: string, traceId: TraceId, chatName: string) {
|
|
35
38
|
return {
|
|
36
|
-
logObjectWithTraceId: (data:
|
|
39
|
+
logObjectWithTraceId: (data: unknown) => {
|
|
37
40
|
this.logObjectWithTraceId(botName, traceId, chatName, data);
|
|
38
41
|
},
|
|
39
42
|
logWithTraceId: (text: string) => {
|
|
@@ -55,12 +58,24 @@ export class JsonLogger implements ILogger {
|
|
|
55
58
|
botName: string,
|
|
56
59
|
traceId: TraceId,
|
|
57
60
|
chatName: string,
|
|
58
|
-
data:
|
|
61
|
+
data: unknown
|
|
59
62
|
) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
const enrichedData =
|
|
64
|
+
typeof data == 'object'
|
|
65
|
+
? {
|
|
66
|
+
...data,
|
|
67
|
+
botName,
|
|
68
|
+
traceId,
|
|
69
|
+
chatName
|
|
70
|
+
}
|
|
71
|
+
: {
|
|
72
|
+
botName,
|
|
73
|
+
traceId,
|
|
74
|
+
chatName,
|
|
75
|
+
data
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
console.log(enrichedData);
|
|
64
79
|
}
|
|
65
80
|
|
|
66
81
|
logWithTraceId(
|
package/services/telegramApi.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { Message } from 'telegraf/types';
|
|
2
1
|
import { IStorageClient } from '../types/storage';
|
|
3
2
|
import { BotResponse, IReplyResponse } from '../types/response';
|
|
4
|
-
import { Telegram } from 'telegraf/typings/telegram';
|
|
5
3
|
import { ILogger } from '../types/logger';
|
|
6
4
|
import { QueueItem, ResponseProcessingQueue } from './responseProcessingQueue';
|
|
7
5
|
import { IReplyCapture } from '../types/capture';
|
|
@@ -9,13 +7,13 @@ import { IActionWithState } from '../types/action';
|
|
|
9
7
|
import { IActionState } from '../types/actionState';
|
|
10
8
|
import { TraceId } from '../types/trace';
|
|
11
9
|
import { ChatInfo } from '../dtos/chatInfo';
|
|
12
|
-
import {
|
|
10
|
+
import { TelegramApiClient, TelegramMessage } from '../types/externalAliases';
|
|
13
11
|
|
|
14
12
|
export const TELEGRAM_ERROR_QUOTE_INVALID = 'QUOTE_TEXT_INVALID';
|
|
15
13
|
|
|
16
14
|
export class TelegramApiService {
|
|
17
15
|
private readonly queue = new ResponseProcessingQueue();
|
|
18
|
-
private readonly telegram:
|
|
16
|
+
private readonly telegram: TelegramApiClient;
|
|
19
17
|
private readonly storage: IStorageClient;
|
|
20
18
|
private readonly logger: ILogger;
|
|
21
19
|
private readonly captureRegistrationCallback: (
|
|
@@ -29,7 +27,7 @@ export class TelegramApiService {
|
|
|
29
27
|
|
|
30
28
|
constructor(
|
|
31
29
|
botName: string,
|
|
32
|
-
telegram:
|
|
30
|
+
telegram: TelegramApiClient,
|
|
33
31
|
storage: IStorageClient,
|
|
34
32
|
logger: ILogger,
|
|
35
33
|
captureRegistrationCallback: (
|
|
@@ -67,8 +65,10 @@ export class TelegramApiService {
|
|
|
67
65
|
try {
|
|
68
66
|
await this.processResponse(response);
|
|
69
67
|
} catch (error) {
|
|
70
|
-
if ('message' in (error as
|
|
71
|
-
const telegramResponse = error as
|
|
68
|
+
if ('message' in (error as { message?: string })) {
|
|
69
|
+
const telegramResponse = error as {
|
|
70
|
+
message: string;
|
|
71
|
+
};
|
|
72
72
|
|
|
73
73
|
if (
|
|
74
74
|
telegramResponse.message.includes(
|
|
@@ -104,11 +104,14 @@ export class TelegramApiService {
|
|
|
104
104
|
void this.queue.flushReadyItems();
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
private async pinIfShould(
|
|
107
|
+
private async pinIfShould(
|
|
108
|
+
response: IReplyResponse,
|
|
109
|
+
message: TelegramMessage
|
|
110
|
+
) {
|
|
108
111
|
if (response.shouldPin) {
|
|
109
112
|
await this.telegram.pinChatMessage(
|
|
110
113
|
response.chatInfo.id,
|
|
111
|
-
|
|
114
|
+
message.message_id,
|
|
112
115
|
{ disable_notification: true }
|
|
113
116
|
);
|
|
114
117
|
|
|
@@ -116,14 +119,14 @@ export class TelegramApiService {
|
|
|
116
119
|
response.action as IActionWithState<IActionState>,
|
|
117
120
|
response.chatInfo.id,
|
|
118
121
|
(state) => {
|
|
119
|
-
state.pinnedMessages.push(
|
|
122
|
+
state.pinnedMessages.push(message.message_id);
|
|
120
123
|
}
|
|
121
124
|
);
|
|
122
125
|
}
|
|
123
126
|
}
|
|
124
127
|
|
|
125
128
|
private async processResponse(response: BotResponse, ignoreQuote = false) {
|
|
126
|
-
let sentMessage:
|
|
129
|
+
let sentMessage: TelegramMessage | null = null;
|
|
127
130
|
|
|
128
131
|
switch (response.kind) {
|
|
129
132
|
case 'text':
|
|
@@ -179,8 +182,7 @@ export class TelegramApiService {
|
|
|
179
182
|
type: 'emoji',
|
|
180
183
|
emoji: response.emoji
|
|
181
184
|
}
|
|
182
|
-
]
|
|
183
|
-
true
|
|
185
|
+
]
|
|
184
186
|
);
|
|
185
187
|
|
|
186
188
|
return;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
User,
|
|
3
|
+
Message,
|
|
4
|
+
InlineQueryResult,
|
|
5
|
+
TelegramEmoji as Emoji,
|
|
6
|
+
UserFromGetMe
|
|
7
|
+
} from 'telegraf/typings/core/types/typegram';
|
|
8
|
+
import { Telegraf } from 'telegraf';
|
|
9
|
+
import Telegram from 'telegraf/typings/telegram';
|
|
10
|
+
|
|
11
|
+
export type TelegramUser = User;
|
|
12
|
+
export type TelegramMessage = Message;
|
|
13
|
+
export type TelegramInlineQueryResult = InlineQueryResult;
|
|
14
|
+
export type TelegramEmoji = Emoji;
|
|
15
|
+
export type TelegramApiClient = Telegram;
|
|
16
|
+
export type BotInfo = UserFromGetMe;
|
|
17
|
+
export type TelegramBot = Telegraf;
|
package/types/logger.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { TraceId } from './trace';
|
|
2
2
|
|
|
3
3
|
export interface IScopedLogger {
|
|
4
|
-
logObjectWithTraceId(
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
-
data: any
|
|
7
|
-
): void;
|
|
4
|
+
logObjectWithTraceId(data: unknown): void;
|
|
8
5
|
|
|
9
6
|
logWithTraceId(text: string): void;
|
|
10
7
|
|
|
@@ -22,8 +19,7 @@ export interface ILogger {
|
|
|
22
19
|
botName: string,
|
|
23
20
|
traceId: TraceId,
|
|
24
21
|
chatName: string,
|
|
25
|
-
|
|
26
|
-
data: any
|
|
22
|
+
data: unknown
|
|
27
23
|
): void;
|
|
28
24
|
|
|
29
25
|
logWithTraceId(
|
package/types/messageTypes.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Message, Update } from 'telegraf/types';
|
|
2
|
-
|
|
3
1
|
export const INTERNAL_MESSAGE_TYPE_PREFIX = `__msg:`;
|
|
4
2
|
|
|
5
3
|
export const MessageType = {
|
|
@@ -21,5 +19,3 @@ export const MessageType = {
|
|
|
21
19
|
} as const;
|
|
22
20
|
|
|
23
21
|
export type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
|
|
24
|
-
|
|
25
|
-
export type TelegrafContextMessage = Update.New & (Update.NonChannel & Message);
|