-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtelegram-client.js
More file actions
1191 lines (1072 loc) · 34.8 KB
/
telegram-client.js
File metadata and controls
1191 lines (1072 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { TelegramClient as MtCuteClient } from '@mtcute/node';
import { InputMedia } from '@mtcute/core';
import EventEmitter from 'events';
import fs from 'fs';
import { stat } from 'fs/promises';
import os from 'os';
import path from 'path';
import readline from 'readline';
import { Readable } from 'stream';
import { nodeReadableToFuman } from '@fuman/node';
import { resolveStoreDir, resolveStorePaths } from './core/store.js';
const timeoutPatchKey = Symbol.for('tgcli.timeoutPatch');
if (!globalThis[timeoutPatchKey]) {
const originalSetTimeout = globalThis.setTimeout;
if (typeof originalSetTimeout === 'function') {
const wrapped = (handler, delay, ...args) => {
const safeDelay = Number.isFinite(delay) ? Math.max(0, delay) : 0;
return originalSetTimeout(handler, safeDelay, ...args);
};
globalThis.setTimeout = wrapped;
}
globalThis[timeoutPatchKey] = true;
}
const DEFAULT_STORE_DIR = resolveStoreDir();
const { sessionPath: DEFAULT_SESSION_PATH } = resolveStorePaths(DEFAULT_STORE_DIR);
const DEFAULT_DOWNLOAD_DIR = path.join(DEFAULT_STORE_DIR, 'downloads');
const MIME_EXTENSION_MAP = {
'image/jpeg': '.jpg',
'image/png': '.png',
'image/webp': '.webp',
'image/gif': '.gif',
'video/mp4': '.mp4',
'video/quicktime': '.mov',
'audio/mpeg': '.mp3',
'audio/mp4': '.m4a',
'audio/ogg': '.ogg',
'audio/opus': '.opus',
'audio/wav': '.wav',
'application/pdf': '.pdf',
'application/zip': '.zip',
};
const IS_TTY = typeof process === 'object' && Boolean(process.stdout?.isTTY);
const LOG_BASE_FORMAT = IS_TTY ? '%s [%s] [%s%s\x1B[0m] ' : '%s [%s] [%s] ';
const LOG_LEVEL_NAMES = IS_TTY
? [
'',
'\x1B[31mERR\x1B[0m',
'\x1B[33mWRN\x1B[0m',
'\x1B[34mINF\x1B[0m',
'\x1B[36mDBG\x1B[0m',
'\x1B[35mVRB\x1B[0m',
]
: ['', 'ERR', 'WRN', 'INF', 'DBG', 'VRB'];
const LOG_TAG_COLORS = [6, 2, 3, 4, 5, 1].map((i) => `\x1B[3${i};1m`);
const LOG_HANDLER = IS_TTY
? (color, level, tag, fmt, args) => {
console.log(
LOG_BASE_FORMAT + fmt,
new Date().toISOString(),
LOG_LEVEL_NAMES[level],
LOG_TAG_COLORS[color],
tag,
...args,
);
}
: (color, level, tag, fmt, args) => {
console.log(
LOG_BASE_FORMAT + fmt,
new Date().toISOString(),
LOG_LEVEL_NAMES[level],
tag,
...args,
);
};
async function normalizeUploadFile(file) {
if (typeof file === 'string') {
file = fs.createReadStream(file);
}
if (file instanceof fs.ReadStream) {
const filePath = file.path.toString();
const fileName = path.basename(filePath);
const fileSize = await stat(filePath).then((stats) => stats.size);
return {
file: nodeReadableToFuman(file),
fileName,
fileSize,
};
}
if (file instanceof Readable) {
return {
file: nodeReadableToFuman(file),
};
}
return null;
}
function createPlatform() {
return {
beforeExit: (callback) => {
if (typeof process === 'undefined') {
return () => {};
}
const handler = () => {
callback();
};
process.on('exit', handler);
return () => {
process.off('exit', handler);
};
},
log: LOG_HANDLER,
getDefaultLogLevel: () => {
const envLogLevel = Number.parseInt(process.env.MTCUTE_LOG_LEVEL ?? '', 10);
if (!Number.isNaN(envLogLevel)) {
return envLogLevel;
}
return null;
},
getDeviceModel: () => `Node.js/${process.version} (${os.type()} ${os.arch()})`,
normalizeFile: normalizeUploadFile,
};
}
function sanitizeString(value) {
return typeof value === 'string' ? value : '';
}
function coerceApiId(value) {
if (typeof value === 'number') {
return value;
}
if (typeof value === 'string') {
const parsed = parseInt(value, 10);
if (!Number.isNaN(parsed)) {
return parsed;
}
}
throw new Error('TELEGRAM_API_ID must be a number');
}
function normalizePeerType(peer) {
if (!peer) return 'chat';
if (peer.type === 'user' || peer.type === 'bot') return 'user';
if (peer.type === 'channel') return 'channel';
if (peer.type === 'chat' && peer.chatType && peer.chatType !== 'group') return 'channel';
return 'chat';
}
function isGroupPeer(peer) {
if (!peer) return false;
if (typeof peer.isGroup === 'boolean') {
return peer.isGroup;
}
if (peer.type === 'chat') {
return true;
}
if (peer.type === 'channel' && typeof peer.chatType === 'string') {
return peer.chatType !== 'channel';
}
return false;
}
function extractTopicId(message) {
if (!message) return null;
if (typeof message.replyToMessage?.threadId === 'number') {
return message.replyToMessage.threadId;
}
if (message.action?.type === 'topic_created' && typeof message.id === 'number') {
return message.id;
}
const raw = message.raw ?? message;
const replyTo = raw?.replyTo;
if (replyTo?.replyToTopId) {
return replyTo.replyToTopId;
}
return null;
}
function normalizeMediaText(value) {
if (typeof value === 'string') {
const trimmed = value.trim();
return trimmed ? trimmed : null;
}
if (typeof value === 'number' || typeof value === 'bigint') {
return String(value);
}
return null;
}
function normalizeMediaNumber(value) {
if (value === null || value === undefined) {
return null;
}
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
if (typeof value === 'string' && value.trim()) {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
}
return null;
}
function readMediaProperty(media, prop) {
if (!media) {
return null;
}
try {
return media[prop];
} catch (error) {
return null;
}
}
function buildWebpageExtras(media) {
if (!media || media.type !== 'webpage' || !media.preview) {
return null;
}
const preview = media.preview;
const previewData = {
url: preview.url ?? null,
displayUrl: preview.displayUrl ?? null,
siteName: preview.siteName ?? null,
title: preview.title ?? null,
description: preview.description ?? null,
author: preview.author ?? null,
previewType: preview.previewType ?? null,
};
const hasPreview = Object.values(previewData).some((value) => value);
const extras = {};
if (hasPreview) {
extras.preview = previewData;
}
if (typeof media.displaySize === 'string') {
extras.displaySize = media.displaySize;
}
if (typeof media.manual === 'boolean') {
extras.manual = media.manual;
}
return Object.keys(extras).length ? extras : null;
}
export function summarizeMedia(media) {
if (!media || typeof media !== 'object') {
return null;
}
const type = normalizeMediaText(media.type);
if (!type) {
return null;
}
const summary = {
type,
fileId: normalizeMediaText(readMediaProperty(media, 'fileId') ?? media.file_id),
uniqueFileId: normalizeMediaText(readMediaProperty(media, 'uniqueFileId') ?? media.unique_file_id),
fileName: normalizeMediaText(readMediaProperty(media, 'fileName') ?? media.file_name),
mimeType: normalizeMediaText(readMediaProperty(media, 'mimeType') ?? media.mime_type),
fileSize: normalizeMediaNumber(readMediaProperty(media, 'fileSize') ?? media.file_size),
width: normalizeMediaNumber(readMediaProperty(media, 'width') ?? media.width),
height: normalizeMediaNumber(readMediaProperty(media, 'height') ?? media.height),
duration: normalizeMediaNumber(readMediaProperty(media, 'duration') ?? media.duration),
extras: null,
};
if (!summary.mimeType && type === 'photo') {
summary.mimeType = 'image/jpeg';
}
if (media.extras && typeof media.extras === 'object') {
summary.extras = media.extras;
} else {
summary.extras = buildWebpageExtras(media);
}
if (type === 'webpage' && media.preview) {
const previewMedia = media.preview.document ?? media.preview.photo ?? null;
if (previewMedia) {
const previewSummary = summarizeMedia(previewMedia);
if (previewSummary) {
summary.fileId = summary.fileId ?? previewSummary.fileId;
summary.uniqueFileId = summary.uniqueFileId ?? previewSummary.uniqueFileId;
summary.fileName = summary.fileName ?? previewSummary.fileName;
summary.mimeType = summary.mimeType ?? previewSummary.mimeType;
summary.fileSize = summary.fileSize ?? previewSummary.fileSize;
summary.width = summary.width ?? previewSummary.width;
summary.height = summary.height ?? previewSummary.height;
summary.duration = summary.duration ?? previewSummary.duration;
}
}
}
return summary;
}
function extensionFromMime(mimeType) {
if (!mimeType || typeof mimeType !== 'string') {
return null;
}
return MIME_EXTENSION_MAP[mimeType.toLowerCase()] ?? null;
}
function buildDownloadFileName(summary, messageId) {
const rawName = normalizeMediaText(summary?.fileName);
if (rawName) {
return path.basename(rawName);
}
const ext = extensionFromMime(summary?.mimeType) ?? '';
const baseType = normalizeMediaText(summary?.type) ?? 'media';
return `${baseType}-${messageId}${ext}`;
}
function resolveDownloadPath(outputPath, { channelId, messageId, summary }) {
const fileName = buildDownloadFileName(summary, messageId);
if (!outputPath) {
return path.resolve(DEFAULT_DOWNLOAD_DIR, String(channelId), fileName);
}
const resolved = path.resolve(outputPath);
if (fs.existsSync(resolved)) {
const stat = fs.statSync(resolved);
if (stat.isDirectory()) {
return path.join(resolved, fileName);
}
} else if (/[\\/]$/.test(outputPath)) {
return path.join(resolved, fileName);
}
return resolved;
}
function resolveDownloadLocation(media) {
if (!media || typeof media !== 'object') {
return null;
}
if (media.type === 'webpage' && media.preview) {
return media.preview.document ?? media.preview.photo ?? null;
}
if (media.location && typeof media.location === 'object') {
return media;
}
return null;
}
export function normalizeChannelId(channelId) {
if (typeof channelId === 'number') {
return channelId;
}
if (typeof channelId === 'bigint') {
return Number(channelId);
}
if (typeof channelId === 'string') {
const trimmed = channelId.trim();
if (/^-?\d+$/.test(trimmed)) {
const numeric = Number(trimmed);
if (!Number.isNaN(numeric)) {
return numeric;
}
}
return trimmed;
}
throw new Error('Invalid channel ID provided');
}
class TelegramClient {
constructor(apiId, apiHash, phoneNumber, sessionPath = DEFAULT_SESSION_PATH, options = {}) {
this.apiId = coerceApiId(apiId);
this.apiHash = sanitizeString(apiHash);
this.phoneNumber = sanitizeString(phoneNumber);
this.sessionPath = path.resolve(sessionPath);
const dataDir = path.dirname(this.sessionPath);
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
this.updateEmitter = new EventEmitter();
this.updatesRunning = false;
this.rawUpdateHandler = null;
const userUpdates = options.updates ?? {};
const updatesConfig = {
...userUpdates,
catchUp: userUpdates.catchUp ?? true,
onChannelTooLong: (channelId, diff) => {
if (typeof userUpdates.onChannelTooLong === 'function') {
userUpdates.onChannelTooLong(channelId, diff);
}
this.updateEmitter.emit('channelTooLong', { channelId, diff });
},
};
this.client = new MtCuteClient({
apiId: this.apiId,
apiHash: this.apiHash,
storage: this.sessionPath,
platform: createPlatform(),
updates: updatesConfig,
});
}
_isUnauthorizedError(error) {
if (!error) return false;
const code = error.code || error.status || error.errorCode;
if (code === 401) {
return true;
}
const message = (error.errorMessage || error.message || '').toUpperCase();
return message.includes('AUTH_KEY') || message.includes('AUTHORIZATION') || message.includes('SESSION_PASSWORD_NEEDED');
}
async _isAuthorized() {
try {
await this.client.getMe();
return true;
} catch (error) {
if (this._isUnauthorizedError(error)) {
return false;
}
throw error;
}
}
async isAuthorized() {
return this._isAuthorized();
}
async getCurrentUser() {
try {
return await this.client.getMe();
} catch (error) {
if (this._isUnauthorizedError(error)) {
return null;
}
throw error;
}
}
async _askQuestion(prompt) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => {
rl.question(prompt, answer => {
rl.close();
resolve(answer.trim());
});
});
}
async _askHiddenQuestion(prompt) {
if (!process.stdin.isTTY) {
return this._askQuestion(prompt);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
});
rl.stdoutMuted = false;
const writeOutput = rl._writeToOutput.bind(rl);
rl._writeToOutput = (stringToWrite) => {
if (!rl.stdoutMuted) {
writeOutput(stringToWrite);
}
};
return new Promise(resolve => {
rl.question(prompt, answer => {
rl.output.write('\n');
rl.close();
resolve(answer.trim());
});
rl.stdoutMuted = true;
});
}
async login() {
try {
if (await this._isAuthorized()) {
console.log('Existing session is valid.');
return true;
}
if (!this.phoneNumber) {
throw new Error('TELEGRAM_PHONE_NUMBER is not configured.');
}
await this.client.start({
phone: this.phoneNumber,
code: async () => await this._askQuestion('Enter the code you received: '),
password: async () => {
const value = await this._askHiddenQuestion('Enter your 2FA password (leave empty if not enabled): ');
return value.length ? value : undefined;
},
});
console.log('Logged in successfully!');
return true;
} catch (error) {
console.error('Error during login:', error);
return false;
}
}
async ensureLogin() {
if (!(await this._isAuthorized())) {
throw new Error('Not logged in to Telegram. Please restart the server.');
}
return true;
}
async initializeDialogCache() {
console.log('Initializing dialog list...');
const loginSuccess = await this.login();
if (!loginSuccess) {
throw new Error('Failed to login to Telegram. Cannot proceed.');
}
await this.startUpdates();
console.log('Dialogs ready.');
return true;
}
async listDialogs(limit = 50) {
await this.ensureLogin();
const effectiveLimit = limit && limit > 0 ? limit : Infinity;
const results = [];
for await (const dialog of this.client.iterDialogs({})) {
const peer = dialog.peer;
if (!peer) continue;
const id = peer.id.toString();
const username = 'username' in peer ? peer.username ?? null : null;
const chatType = typeof peer.chatType === 'string' ? peer.chatType : null;
const isForum = typeof peer.isForum === 'boolean' ? peer.isForum : null;
const isGroup = typeof peer.isGroup === 'boolean' ? peer.isGroup : null;
results.push({
id,
type: normalizePeerType(peer),
title: peer.displayName || 'Unknown',
username,
chatType,
isForum,
isGroup,
});
if (results.length >= effectiveLimit) {
break;
}
}
return results;
}
async searchPeers(query, limit = 50) {
await this.ensureLogin();
const result = await this.client.call({
_: 'contacts.search',
q: query,
limit,
});
const results = [];
for (const chat of (result.chats || [])) {
results.push({
id: chat.id.toString(),
type: normalizePeerType(chat),
title: chat.title || chat.firstName || 'Unknown',
username: chat.username ?? null,
chatType: chat._ ?? null,
isForum: chat.forum ?? null,
isGroup: null,
});
}
for (const user of (result.users || [])) {
results.push({
id: user.id.toString(),
type: 'user',
title: [user.firstName, user.lastName].filter(Boolean).join(' ') || 'Unknown',
username: user.username ?? null,
chatType: 'user',
isForum: null,
isGroup: null,
});
}
return results;
}
async searchDialogs(keyword, limit = 100, { local = false } = {}) {
const query = sanitizeString(keyword).trim().toLowerCase();
if (!query) {
return [];
}
if (!local) {
return this.searchPeers(query, limit);
}
await this.ensureLogin();
const results = [];
for await (const dialog of this.client.iterDialogs({})) {
const peer = dialog.peer;
if (!peer) continue;
const title = (peer.displayName || '').toLowerCase();
const username = ('username' in peer && peer.username ? peer.username : '').toLowerCase();
if (title.includes(query) || username.includes(query)) {
const chatType = typeof peer.chatType === 'string' ? peer.chatType : null;
const isForum = typeof peer.isForum === 'boolean' ? peer.isForum : null;
const isGroup = typeof peer.isGroup === 'boolean' ? peer.isGroup : null;
results.push({
id: peer.id.toString(),
type: normalizePeerType(peer),
title: peer.displayName || 'Unknown',
username: 'username' in peer ? peer.username ?? null : null,
chatType,
isForum,
isGroup,
});
}
if (results.length >= limit) {
break;
}
}
return results;
}
async getMessagesByChannelId(channelId, limit = 100, options = {}) {
await this.ensureLogin();
const {
minId = 0,
maxId = 0,
reverse = false,
} = options;
const peerRef = normalizeChannelId(channelId);
const peer = await this.client.resolvePeer(peerRef);
const effectiveLimit = limit && limit > 0 ? limit : 100;
const messages = [];
const iterOptions = {
limit: effectiveLimit,
chunkSize: Math.min(effectiveLimit, 100),
reverse,
};
if (minId) {
iterOptions.minId = minId;
}
if (maxId) {
iterOptions.maxId = maxId;
}
for await (const message of this.client.iterHistory(peer, iterOptions)) {
messages.push(this._serializeMessage(message, peer));
if (messages.length >= effectiveLimit) {
break;
}
}
return {
peerTitle: peer?.displayName || 'Unknown',
peerId: peer?.id?.toString?.() ?? String(channelId),
peerType: normalizePeerType(peer),
messages,
};
}
async getMessageById(channelId, messageId) {
await this.ensureLogin();
const peerRef = normalizeChannelId(channelId);
const [message] = await this.client.getMessages(peerRef, Number(messageId));
if (!message) {
return null;
}
const peer = message.chat ?? await this.client.resolvePeer(peerRef);
return this._serializeMessage(message, peer);
}
async getMessageContext(channelId, messageId, options = {}) {
await this.ensureLogin();
const safeBefore = Number.isFinite(options.before) && options.before >= 0
? Number(options.before)
: 20;
const safeAfter = Number.isFinite(options.after) && options.after >= 0
? Number(options.after)
: 20;
const total = safeBefore + safeAfter + 1;
const peerRef = normalizeChannelId(channelId);
const [message] = await this.client.getMessages(peerRef, Number(messageId));
if (!message) {
return { target: null, before: [], after: [] };
}
let dateSeconds = 0;
if (message.date instanceof Date) {
dateSeconds = Math.floor(message.date.getTime() / 1000);
} else if (typeof message.date === 'number') {
dateSeconds = Math.floor(message.date);
}
const history = await this.client.getHistory(peerRef, {
offset: {
id: message.id,
date: dateSeconds,
},
addOffset: -safeAfter,
limit: total,
});
const peer = message.chat ?? await this.client.resolvePeer(peerRef);
const target = this._serializeMessage(message, peer);
const before = [];
const after = [];
for (const entry of history) {
const serialized = this._serializeMessage(entry, peer);
if (serialized.id < target.id) {
before.push(serialized);
} else if (serialized.id > target.id) {
after.push(serialized);
}
}
before.sort((a, b) => a.id - b.id);
after.sort((a, b) => a.id - b.id);
return {
target,
before: before.slice(-safeBefore),
after: after.slice(0, safeAfter),
};
}
async searchChannelMessages(channelId, options = {}) {
await this.ensureLogin();
const query = typeof options.query === 'string' ? options.query : '';
const limit = options.limit && options.limit > 0 ? Number(options.limit) : 50;
const threadId = typeof options.topicId === 'number' ? options.topicId : undefined;
const peerRef = normalizeChannelId(channelId);
const peer = await this.client.resolvePeer(peerRef);
const results = await this.client.searchMessages({
chatId: peerRef,
threadId,
limit,
query,
});
const messages = results.map((message) => this._serializeMessage(message, peer));
return {
peerTitle: peer?.displayName || 'Unknown',
peerId: peer?.id?.toString?.() ?? String(channelId),
peerType: normalizePeerType(peer),
total: results.total ?? messages.length,
next: results.next ?? null,
messages,
};
}
async sendTextMessage(channelId, text, options = {}) {
await this.ensureLogin();
const messageText = typeof text === 'string' ? text : String(text ?? '');
if (!messageText.trim()) {
throw new Error('Message text cannot be empty.');
}
const replyTo = Number.isFinite(options.replyToMessageId)
? options.replyToMessageId
: (Number.isFinite(options.topicId) ? options.topicId : undefined);
const params = replyTo ? { replyTo } : undefined;
const peerRef = normalizeChannelId(channelId);
const sent = await this.client.sendText(peerRef, messageText, params);
return { messageId: sent.id };
}
async sendFileMessage(channelId, filePath, options = {}) {
await this.ensureLogin();
if (!filePath || typeof filePath !== 'string') {
throw new Error('filePath must be a string.');
}
const resolved = path.resolve(filePath);
if (!fs.existsSync(resolved)) {
throw new Error(`File not found: ${resolved}`);
}
const uploadPath = `file:${resolved}`;
const caption = typeof options.caption === 'string' && options.caption.trim()
? options.caption
: undefined;
const fileName = typeof options.filename === 'string' && options.filename.trim()
? options.filename.trim()
: undefined;
const replyTo = Number.isFinite(options.topicId) ? options.topicId : undefined;
const params = replyTo ? { replyTo } : undefined;
const media = InputMedia.auto(uploadPath, {
caption,
fileName,
});
const peerRef = normalizeChannelId(channelId);
const sent = await this.client.sendMedia(peerRef, media, params);
return { messageId: sent.id };
}
async downloadMessageMedia(channelId, messageId, options = {}) {
await this.ensureLogin();
const peerRef = normalizeChannelId(channelId);
const [message] = await this.client.getMessages(peerRef, Number(messageId));
if (!message) {
throw new Error('Message not found.');
}
const location = resolveDownloadLocation(message.media);
if (!location) {
throw new Error('Message has no downloadable media.');
}
const summary = summarizeMedia(message.media);
const targetPath = resolveDownloadPath(options.outputPath, {
channelId,
messageId,
summary,
});
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
await this.client.downloadToFile(targetPath, location);
const stats = fs.statSync(targetPath);
return {
path: targetPath,
bytes: stats.size,
mimeType: summary?.mimeType ?? null,
downloadedAt: new Date().toISOString(),
};
}
async listContacts() {
await this.ensureLogin();
return this.client.getContacts();
}
async getUserProfile(userId) {
await this.ensureLogin();
return this.client.getUser(userId);
}
async listGroups(options = {}) {
await this.ensureLogin();
const query = typeof options.query === 'string' ? options.query.trim().toLowerCase() : '';
const limit = options.limit && options.limit > 0 ? Number(options.limit) : 100;
const results = [];
for await (const dialog of this.client.iterDialogs({})) {
const peer = dialog.peer;
if (!peer || !isGroupPeer(peer)) {
continue;
}
const title = peer.displayName || 'Unknown';
const username = 'username' in peer ? peer.username ?? null : null;
const haystack = `${title} ${username ?? ''}`.toLowerCase();
if (query && !haystack.includes(query)) {
continue;
}
results.push({
id: peer.id.toString(),
title,
username,
chatType: typeof peer.chatType === 'string' ? peer.chatType : null,
isForum: typeof peer.isForum === 'boolean' ? peer.isForum : null,
membersCount: peer.membersCount ?? null,
});
if (results.length >= limit) {
break;
}
}
return results;
}
async getGroupInfo(channelId) {
await this.ensureLogin();
const peerRef = normalizeChannelId(channelId);
const chat = await this.client.getChat(peerRef);
const full = await this.client.getFullChat(peerRef).catch(() => null);
return {
id: chat.id?.toString?.() ?? String(channelId),
title: chat.displayName || chat.title || 'Unknown',
username: chat.username ?? null,
chatType: typeof chat.chatType === 'string' ? chat.chatType : null,
isForum: typeof chat.isForum === 'boolean' ? chat.isForum : null,
isMember: typeof chat.isMember === 'boolean' ? chat.isMember : null,
isAdmin: typeof chat.isAdmin === 'boolean' ? chat.isAdmin : null,
isCreator: typeof chat.isCreator === 'boolean' ? chat.isCreator : null,
membersCount: full?.membersCount ?? chat.membersCount ?? null,
about: full?.bio ?? null,
};
}
async renameGroup(channelId, title) {
await this.ensureLogin();
const value = typeof title === 'string' ? title.trim() : '';
if (!value) {
throw new Error('Group title must be a non-empty string.');
}
const peerRef = normalizeChannelId(channelId);
await this.client.setChatTitle(peerRef, value);
return true;
}
async addGroupMembers(channelId, userIds) {
await this.ensureLogin();
const users = Array.isArray(userIds) ? userIds : [userIds];
if (!users.length) {
throw new Error('userIds must include at least one entry.');
}
const peerRef = normalizeChannelId(channelId);
const failed = await this.client.addChatMembers(peerRef, users);
return failed.map((entry) => ({
userId: entry.userId?.toString?.() ?? null,
error: entry.error ?? null,
}));
}
async removeGroupMembers(channelId, userIds) {
await this.ensureLogin();
const users = Array.isArray(userIds) ? userIds : [userIds];
if (!users.length) {
throw new Error('userIds must include at least one entry.');
}
const peerRef = normalizeChannelId(channelId);
const removed = [];
const failed = [];
for (const userId of users) {
try {
await this.client.kickChatMember({ chatId: peerRef, userId });
removed.push(String(userId));
} catch (error) {
failed.push({ userId: String(userId), error: error?.message ?? String(error) });
}
}
return { removed, failed };
}
async getGroupInviteLink(channelId) {
await this.ensureLogin();
const peerRef = normalizeChannelId(channelId);
return this.client.getPrimaryInviteLink(peerRef);
}
async revokeGroupInviteLink(channelId, link) {
await this.ensureLogin();
const peerRef = normalizeChannelId(channelId);
return this.client.revokeInviteLink(peerRef, link);
}
async joinGroup(invite) {
await this.ensureLogin();
return this.client.joinChat(invite);
}
async leaveGroup(channelId) {
await this.ensureLogin();
const peerRef = normalizeChannelId(channelId);
await this.client.leaveChat(peerRef);
return true;
}
async getPeerMetadata(channelId, peerType) {
await this.ensureLogin();
const peerRef = normalizeChannelId(channelId);
const buildUserMetadata = async () => {
const user = await this.client.getFullUser(peerRef);
return {
peerTitle: user.displayName || 'Unknown',
username: user.username ?? null,
peerType: normalizePeerType(user),
chatType: null,
isForum: null,
about: user.bio || null,
};
};
if (peerType === 'user') {
return buildUserMetadata();
}
let peerTitle = null;
let username = null;
let resolvedType = peerType ?? null;
let chatType = null;
let isForum = null;