chz-telegram-bot 0.1.19 → 0.1.20
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/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +11 -5
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.js +6 -11
- package/dist/services/responseProcessingQueue.d.ts +0 -1
- package/dist/services/responseProcessingQueue.d.ts.map +1 -1
- package/dist/services/responseProcessingQueue.js +1 -6
- package/dist/types/messageTypes.d.ts +1 -0
- package/dist/types/messageTypes.d.ts.map +1 -1
- package/dist/types/messageTypes.js +15 -14
- package/entities/botInstance.ts +21 -7
- package/eslint.config.js +0 -1
- package/package.json +1 -1
- package/services/jsonFileStorage.ts +7 -13
- package/services/responseProcessingQueue.ts +2 -8
- package/types/messageTypes.ts +15 -13
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuB,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAO5D,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGhD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuB,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MA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AE,cAAc,CAAC;YAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IA2BK,KAAK,CAAC,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE;YACL,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;YACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;YAC3C,aAAa,EAAE,iBAAiB,EAAE,CAAC;SACtC,CAAC;QACF,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,gCAAgC,CAAC,EAAE,OAAO,CAAC;QAC3C,QAAQ,CAAC,EAAE;YACP,aAAa,CAAC,EAAE,cAAc,CAAC;YAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IAsCD,OAAO,CAAC,6BAA6B;IA4CrC,OAAO,CAAC,+BAA+B;IA2EvC,OAAO,CAAC,2BAA2B;IA2C7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAaT,YAAY;YAuCZ,cAAc;CA2B/B"}
|
|
@@ -19,6 +19,7 @@ const traceFactory_1 = require("../helpers/traceFactory");
|
|
|
19
19
|
const incomingQuery_1 = require("../dtos/incomingQuery");
|
|
20
20
|
const inlineQueryContext_1 = require("./context/inlineQueryContext");
|
|
21
21
|
const helpAction_1 = require("../builtin/helpAction");
|
|
22
|
+
const messageTypes_1 = require("../types/messageTypes");
|
|
22
23
|
class BotInstance {
|
|
23
24
|
constructor(options) {
|
|
24
25
|
const actions = [
|
|
@@ -110,18 +111,23 @@ class BotInstance {
|
|
|
110
111
|
}
|
|
111
112
|
initializeMessageProcessing(verboseLoggingForIncomingMessage) {
|
|
112
113
|
if (this.commands.length > 0) {
|
|
114
|
+
const triggersToBeProcessed = new Set(this.commands
|
|
115
|
+
.flatMap((x) => x.triggers)
|
|
116
|
+
.map((x) => typeof x == 'string'
|
|
117
|
+
? x.startsWith(messageTypes_1.INTERNAL_MESSAGE_TYPE_PREFIX)
|
|
118
|
+
? x
|
|
119
|
+
: messageTypes_1.MessageType.Text
|
|
120
|
+
: messageTypes_1.MessageType.Text));
|
|
113
121
|
this.telegraf.on('message', async (ctx) => {
|
|
114
122
|
const msg = new incomingMessage_1.IncomingMessage(ctx.update.message, this.name);
|
|
115
|
-
const messageContent = msg.text || `<non-text message: ${msg.type}>`;
|
|
116
|
-
const messageFromName = msg.from?.first_name ?? 'Unknown';
|
|
117
|
-
const messageFromId = msg.from?.id ?? 'Unknown';
|
|
118
123
|
if (verboseLoggingForIncomingMessage) {
|
|
119
124
|
this.logger.logObjectWithTraceId(this.name, msg.traceId, msg.chatInfo.name, ctx.update.message);
|
|
120
125
|
}
|
|
121
126
|
else {
|
|
122
|
-
this.logger.logWithTraceId(this.name, msg.traceId, msg.chatInfo.name, `${
|
|
127
|
+
this.logger.logWithTraceId(this.name, msg.traceId, msg.chatInfo.name, `${msg.from?.first_name ?? 'Unknown'} (${msg.from?.id ?? 'Unknown'}): ${msg.text || `<non-text message: ${msg.type}>`}`);
|
|
123
128
|
}
|
|
124
|
-
|
|
129
|
+
if (triggersToBeProcessed.has(msg.type))
|
|
130
|
+
await this.processMessage(msg);
|
|
125
131
|
});
|
|
126
132
|
}
|
|
127
133
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonFileStorage.d.ts","sourceRoot":"","sources":["../../services/jsonFileStorage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"jsonFileStorage.d.ts","sourceRoot":"","sources":["../../services/jsonFileStorage.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,O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|
|
5
|
-
const path_1 = require("path");
|
|
6
5
|
const promises_1 = require("fs/promises");
|
|
7
6
|
const async_sema_1 = require("async-sema");
|
|
8
7
|
class JsonFileStorage {
|
|
@@ -35,10 +34,10 @@ class JsonFileStorage {
|
|
|
35
34
|
async loadInternal(key) {
|
|
36
35
|
if (!this.cache.has(key)) {
|
|
37
36
|
const targetPath = this.buidPathFromKey(key);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
const fileContent = await (0, promises_1.readFile)(targetPath, {
|
|
38
|
+
encoding: 'utf-8',
|
|
39
|
+
flag: 'a+'
|
|
40
|
+
});
|
|
42
41
|
if (fileContent) {
|
|
43
42
|
const data = JSON.parse(fileContent);
|
|
44
43
|
this.cache.set(key, data);
|
|
@@ -49,10 +48,6 @@ class JsonFileStorage {
|
|
|
49
48
|
async save(data, key) {
|
|
50
49
|
this.cache.set(key, data);
|
|
51
50
|
const targetPath = this.buidPathFromKey(key);
|
|
52
|
-
const folderName = (0, path_1.dirname)(targetPath);
|
|
53
|
-
if (!(0, fs_1.existsSync)(folderName)) {
|
|
54
|
-
await (0, promises_1.mkdir)(folderName, { recursive: true });
|
|
55
|
-
}
|
|
56
51
|
await (0, promises_1.writeFile)(targetPath, JSON.stringify(data), { flag: 'w+' });
|
|
57
52
|
}
|
|
58
53
|
buidPathFromKey(key) {
|
|
@@ -60,7 +55,7 @@ class JsonFileStorage {
|
|
|
60
55
|
}
|
|
61
56
|
async load(key) {
|
|
62
57
|
return await this.lock(key, async () => {
|
|
63
|
-
return this.loadInternal(key);
|
|
58
|
+
return await this.loadInternal(key);
|
|
64
59
|
});
|
|
65
60
|
}
|
|
66
61
|
async saveMetadata(actions, botName) {
|
|
@@ -76,7 +71,7 @@ class JsonFileStorage {
|
|
|
76
71
|
});
|
|
77
72
|
}
|
|
78
73
|
async saveActionExecutionResult(action, chatId, state) {
|
|
79
|
-
await this.lock(action.key, async () => {
|
|
74
|
+
return await this.lock(action.key, async () => {
|
|
80
75
|
const data = await this.loadInternal(action.key);
|
|
81
76
|
data[chatId] = state;
|
|
82
77
|
await this.save(data, action.key);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAIF,qBAAa,uBAAuB;IAChC,KAAK,EAAE,SAAS,EAAE,CAAM;IACxB,UAAU,UAAS;IAEnB,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;
|
|
1
|
+
{"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAIF,qBAAa,uBAAuB;IAChC,KAAK,EAAE,SAAS,EAAE,CAAM;IACxB,UAAU,UAAS;IAEnB,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;CAiBxB"}
|
|
@@ -26,10 +26,8 @@ class ResponseProcessingQueue {
|
|
|
26
26
|
return;
|
|
27
27
|
this.isFlushing = true;
|
|
28
28
|
while (this.items.length) {
|
|
29
|
-
if (Date.now() >= this.
|
|
29
|
+
if (Date.now() >= this.items[0].priority) {
|
|
30
30
|
const item = this.items.shift();
|
|
31
|
-
if (!item)
|
|
32
|
-
return;
|
|
33
31
|
await item.callback();
|
|
34
32
|
}
|
|
35
33
|
else {
|
|
@@ -38,8 +36,5 @@ class ResponseProcessingQueue {
|
|
|
38
36
|
}
|
|
39
37
|
this.isFlushing = false;
|
|
40
38
|
}
|
|
41
|
-
peek() {
|
|
42
|
-
return this.items[0];
|
|
43
|
-
}
|
|
44
39
|
}
|
|
45
40
|
exports.ResponseProcessingQueue = ResponseProcessingQueue;
|
|
@@ -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,WAAW;;;;;;;;;;;;;;CAcd,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,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAEjD,eAAO,MAAM,4BAA4B,WAAW,CAAC;AAErD,eAAO,MAAM,WAAW;;;;;;;;;;;;;;CAcd,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,18 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MessageType = void 0;
|
|
3
|
+
exports.MessageType = exports.INTERNAL_MESSAGE_TYPE_PREFIX = void 0;
|
|
4
|
+
exports.INTERNAL_MESSAGE_TYPE_PREFIX = `__msg:`;
|
|
4
5
|
exports.MessageType = {
|
|
5
|
-
Text:
|
|
6
|
-
Sticker:
|
|
7
|
-
Animation:
|
|
8
|
-
Document:
|
|
9
|
-
Voice:
|
|
10
|
-
Audio:
|
|
11
|
-
LeftChatMember:
|
|
12
|
-
NewChatMember:
|
|
13
|
-
Poll:
|
|
14
|
-
Location:
|
|
15
|
-
Photo:
|
|
16
|
-
Forward:
|
|
17
|
-
Unknown:
|
|
6
|
+
Text: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Text`,
|
|
7
|
+
Sticker: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Sticker`,
|
|
8
|
+
Animation: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Animation`,
|
|
9
|
+
Document: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Document`,
|
|
10
|
+
Voice: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Voice`,
|
|
11
|
+
Audio: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Audio`,
|
|
12
|
+
LeftChatMember: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}LeftChatMember`,
|
|
13
|
+
NewChatMember: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}NewChatMember`,
|
|
14
|
+
Poll: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Poll`,
|
|
15
|
+
Location: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Location`,
|
|
16
|
+
Photo: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Photo`,
|
|
17
|
+
Forward: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Forward`,
|
|
18
|
+
Unknown: `${exports.INTERNAL_MESSAGE_TYPE_PREFIX}Unknown`
|
|
18
19
|
};
|
package/entities/botInstance.ts
CHANGED
|
@@ -25,6 +25,10 @@ import { IncomingInlineQuery } from '../dtos/incomingQuery';
|
|
|
25
25
|
import { InlineQueryContext } from './context/inlineQueryContext';
|
|
26
26
|
import { buildHelpCommand } from '../builtin/helpAction';
|
|
27
27
|
import { UserFromGetMe } from 'telegraf/types';
|
|
28
|
+
import {
|
|
29
|
+
INTERNAL_MESSAGE_TYPE_PREFIX,
|
|
30
|
+
MessageType
|
|
31
|
+
} from '../types/messageTypes';
|
|
28
32
|
|
|
29
33
|
export class BotInstance {
|
|
30
34
|
private readonly api: TelegramApiService;
|
|
@@ -262,13 +266,20 @@ export class BotInstance {
|
|
|
262
266
|
verboseLoggingForIncomingMessage: boolean
|
|
263
267
|
) {
|
|
264
268
|
if (this.commands.length > 0) {
|
|
269
|
+
const triggersToBeProcessed = new Set(
|
|
270
|
+
this.commands
|
|
271
|
+
.flatMap((x) => x.triggers)
|
|
272
|
+
.map((x) =>
|
|
273
|
+
typeof x == 'string'
|
|
274
|
+
? x.startsWith(INTERNAL_MESSAGE_TYPE_PREFIX)
|
|
275
|
+
? x
|
|
276
|
+
: MessageType.Text
|
|
277
|
+
: MessageType.Text
|
|
278
|
+
)
|
|
279
|
+
);
|
|
280
|
+
|
|
265
281
|
this.telegraf.on('message', async (ctx) => {
|
|
266
282
|
const msg = new IncomingMessage(ctx.update.message, this.name);
|
|
267
|
-
const messageContent =
|
|
268
|
-
msg.text || `<non-text message: ${msg.type}>`;
|
|
269
|
-
|
|
270
|
-
const messageFromName = msg.from?.first_name ?? 'Unknown';
|
|
271
|
-
const messageFromId = msg.from?.id ?? 'Unknown';
|
|
272
283
|
|
|
273
284
|
if (verboseLoggingForIncomingMessage) {
|
|
274
285
|
this.logger.logObjectWithTraceId(
|
|
@@ -282,11 +293,14 @@ export class BotInstance {
|
|
|
282
293
|
this.name,
|
|
283
294
|
msg.traceId,
|
|
284
295
|
msg.chatInfo.name,
|
|
285
|
-
`${
|
|
296
|
+
`${msg.from?.first_name ?? 'Unknown'} (${
|
|
297
|
+
msg.from?.id ?? 'Unknown'
|
|
298
|
+
}): ${msg.text || `<non-text message: ${msg.type}>`}`
|
|
286
299
|
);
|
|
287
300
|
}
|
|
288
301
|
|
|
289
|
-
|
|
302
|
+
if (triggersToBeProcessed.has(msg.type))
|
|
303
|
+
await this.processMessage(msg);
|
|
290
304
|
});
|
|
291
305
|
}
|
|
292
306
|
}
|
package/eslint.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { existsSync, mkdirSync } from 'fs';
|
|
2
|
-
import {
|
|
3
|
-
import { mkdir, readFile, writeFile } from 'fs/promises';
|
|
2
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
4
3
|
import { Sema as Semaphore } from 'async-sema';
|
|
5
4
|
import { IStorageClient } from '../types/storage';
|
|
6
5
|
import { IActionState } from '../types/actionState';
|
|
@@ -51,11 +50,11 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
51
50
|
) {
|
|
52
51
|
if (!this.cache.has(key)) {
|
|
53
52
|
const targetPath = this.buidPathFromKey(key);
|
|
54
|
-
if (!existsSync(targetPath)) {
|
|
55
|
-
return {};
|
|
56
|
-
}
|
|
57
53
|
|
|
58
|
-
const fileContent = await readFile(targetPath,
|
|
54
|
+
const fileContent = await readFile(targetPath, {
|
|
55
|
+
encoding: 'utf-8',
|
|
56
|
+
flag: 'a+'
|
|
57
|
+
});
|
|
59
58
|
|
|
60
59
|
if (fileContent) {
|
|
61
60
|
const data = JSON.parse(fileContent);
|
|
@@ -74,11 +73,6 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
74
73
|
this.cache.set(key, data);
|
|
75
74
|
|
|
76
75
|
const targetPath = this.buidPathFromKey(key);
|
|
77
|
-
const folderName = dirname(targetPath);
|
|
78
|
-
|
|
79
|
-
if (!existsSync(folderName)) {
|
|
80
|
-
await mkdir(folderName, { recursive: true });
|
|
81
|
-
}
|
|
82
76
|
|
|
83
77
|
await writeFile(targetPath, JSON.stringify(data), { flag: 'w+' });
|
|
84
78
|
}
|
|
@@ -92,7 +86,7 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
92
86
|
|
|
93
87
|
async load<TActionState extends IActionState>(key: ActionKey) {
|
|
94
88
|
return await this.lock(key, async () => {
|
|
95
|
-
return this.loadInternal<TActionState>(key);
|
|
89
|
+
return await this.loadInternal<TActionState>(key);
|
|
96
90
|
});
|
|
97
91
|
}
|
|
98
92
|
|
|
@@ -128,7 +122,7 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
128
122
|
chatId: number,
|
|
129
123
|
state: TActionState
|
|
130
124
|
) {
|
|
131
|
-
await this.lock(action.key, async () => {
|
|
125
|
+
return await this.lock(action.key, async () => {
|
|
132
126
|
const data = await this.loadInternal<TActionState>(action.key);
|
|
133
127
|
|
|
134
128
|
data[chatId] = state;
|
|
@@ -37,10 +37,8 @@ export class ResponseProcessingQueue {
|
|
|
37
37
|
this.isFlushing = true;
|
|
38
38
|
|
|
39
39
|
while (this.items.length) {
|
|
40
|
-
if (Date.now() >= this.
|
|
41
|
-
const item = this.items.shift()
|
|
42
|
-
|
|
43
|
-
if (!item) return;
|
|
40
|
+
if (Date.now() >= this.items[0].priority) {
|
|
41
|
+
const item = this.items.shift()!;
|
|
44
42
|
|
|
45
43
|
await item.callback();
|
|
46
44
|
} else {
|
|
@@ -50,8 +48,4 @@ export class ResponseProcessingQueue {
|
|
|
50
48
|
|
|
51
49
|
this.isFlushing = false;
|
|
52
50
|
}
|
|
53
|
-
|
|
54
|
-
private peek() {
|
|
55
|
-
return this.items[0];
|
|
56
|
-
}
|
|
57
51
|
}
|
package/types/messageTypes.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { Message, Update } from 'telegraf/types';
|
|
2
2
|
|
|
3
|
+
export const INTERNAL_MESSAGE_TYPE_PREFIX = `__msg:`;
|
|
4
|
+
|
|
3
5
|
export const MessageType = {
|
|
4
|
-
Text:
|
|
5
|
-
Sticker:
|
|
6
|
-
Animation:
|
|
7
|
-
Document:
|
|
8
|
-
Voice:
|
|
9
|
-
Audio:
|
|
10
|
-
LeftChatMember:
|
|
11
|
-
NewChatMember:
|
|
12
|
-
Poll:
|
|
13
|
-
Location:
|
|
14
|
-
Photo:
|
|
15
|
-
Forward:
|
|
16
|
-
Unknown:
|
|
6
|
+
Text: `${INTERNAL_MESSAGE_TYPE_PREFIX}Text`,
|
|
7
|
+
Sticker: `${INTERNAL_MESSAGE_TYPE_PREFIX}Sticker`,
|
|
8
|
+
Animation: `${INTERNAL_MESSAGE_TYPE_PREFIX}Animation`,
|
|
9
|
+
Document: `${INTERNAL_MESSAGE_TYPE_PREFIX}Document`,
|
|
10
|
+
Voice: `${INTERNAL_MESSAGE_TYPE_PREFIX}Voice`,
|
|
11
|
+
Audio: `${INTERNAL_MESSAGE_TYPE_PREFIX}Audio`,
|
|
12
|
+
LeftChatMember: `${INTERNAL_MESSAGE_TYPE_PREFIX}LeftChatMember`,
|
|
13
|
+
NewChatMember: `${INTERNAL_MESSAGE_TYPE_PREFIX}NewChatMember`,
|
|
14
|
+
Poll: `${INTERNAL_MESSAGE_TYPE_PREFIX}Poll`,
|
|
15
|
+
Location: `${INTERNAL_MESSAGE_TYPE_PREFIX}Location`,
|
|
16
|
+
Photo: `${INTERNAL_MESSAGE_TYPE_PREFIX}Photo`,
|
|
17
|
+
Forward: `${INTERNAL_MESSAGE_TYPE_PREFIX}Forward`,
|
|
18
|
+
Unknown: `${INTERNAL_MESSAGE_TYPE_PREFIX}Unknown`
|
|
17
19
|
} as const;
|
|
18
20
|
|
|
19
21
|
export type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
|