-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathclient.js
More file actions
37 lines (29 loc) · 1007 Bytes
/
client.js
File metadata and controls
37 lines (29 loc) · 1007 Bytes
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
import TelegramClient from './telegram-client.js';
async function main() {
const client = new TelegramClient(
process.env.TELEGRAM_API_ID,
process.env.TELEGRAM_API_HASH,
process.env.TELEGRAM_PHONE_NUMBER
);
await client.initializeDialogCache();
const dialogs = await client.listDialogs(50);
console.log('Available chats:');
dialogs.forEach((chat, index) => {
console.log(`${index + 1}. ${chat.title} (ID: ${chat.id})`);
});
const firstChat = dialogs[0];
if (!firstChat) {
console.log('No chats available.');
return;
}
const { peerTitle, messages } = await client.getMessagesByChannelId(firstChat.id, 10);
console.log(`\nLatest messages from "${peerTitle}":`);
messages.forEach(msg => {
const date = msg.date ? new Date(msg.date * 1000).toISOString() : 'unknown-date';
console.log(`[${date}] ${msg.text || msg.message || ''}`);
});
await client.destroy();
}
main().catch(error => {
console.error('Error in client example:', error);
});