Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
111 lines
3.5 KiB
JavaScript
111 lines
3.5 KiB
JavaScript
const { Api, TelegramClient } = require('telegram');
|
||
const { StringSession } = require('telegram/sessions');
|
||
const input = require('input');
|
||
|
||
// 测试账号信息
|
||
const apiId = 21853698;
|
||
const apiHash = "66c079a5fd7b4f1bb656f6021ded2c66";
|
||
const stringSession = new StringSession(''); // 空session,需要登录
|
||
|
||
(async () => {
|
||
console.log('正在创建客户端...');
|
||
const client = new TelegramClient(stringSession, apiId, apiHash, {
|
||
connectionRetries: 5,
|
||
});
|
||
|
||
console.log('正在连接...');
|
||
await client.connect();
|
||
|
||
if (!await client.isUserAuthorized()) {
|
||
console.log('需要登录,请输入手机号码:');
|
||
const phoneNumber = await input.text('手机号码: ');
|
||
|
||
await client.sendCode(
|
||
{
|
||
apiId: apiId,
|
||
apiHash: apiHash,
|
||
},
|
||
phoneNumber
|
||
);
|
||
|
||
const code = await input.text('请输入验证码: ');
|
||
|
||
try {
|
||
await client.signIn(
|
||
{
|
||
apiId: apiId,
|
||
apiHash: apiHash,
|
||
},
|
||
phoneNumber,
|
||
code
|
||
);
|
||
} catch (error) {
|
||
if (error.message.includes('SESSION_PASSWORD_NEEDED')) {
|
||
const password = await input.text('请输入两步验证密码: ');
|
||
await client.signIn(
|
||
{
|
||
apiId: apiId,
|
||
apiHash: apiHash,
|
||
},
|
||
phoneNumber,
|
||
password
|
||
);
|
||
}
|
||
}
|
||
|
||
console.log('登录成功!');
|
||
console.log('Session string:', client.session.save());
|
||
}
|
||
|
||
console.log('获取对话列表...');
|
||
|
||
try {
|
||
const result = await client.invoke(
|
||
new Api.messages.GetDialogs({
|
||
offsetDate: 0,
|
||
offsetId: 0,
|
||
offsetPeer: new Api.InputPeerEmpty(),
|
||
limit: 10,
|
||
hash: 0,
|
||
})
|
||
);
|
||
|
||
console.log('\n对话列表:');
|
||
result.dialogs.forEach((dialog, index) => {
|
||
const peer = dialog.peer;
|
||
let title = 'Unknown';
|
||
let type = 'Unknown';
|
||
|
||
// 根据peer类型获取标题
|
||
if (peer instanceof Api.PeerUser) {
|
||
const user = result.users.find(u => u.id.equals(peer.userId));
|
||
if (user) {
|
||
title = user.firstName || user.username || 'User';
|
||
type = 'User';
|
||
}
|
||
} else if (peer instanceof Api.PeerChat) {
|
||
const chat = result.chats.find(c => c.id.equals(peer.chatId));
|
||
if (chat) {
|
||
title = chat.title;
|
||
type = 'Group';
|
||
}
|
||
} else if (peer instanceof Api.PeerChannel) {
|
||
const channel = result.chats.find(c => c.id.equals(peer.channelId));
|
||
if (channel) {
|
||
title = channel.title;
|
||
type = channel.broadcast ? 'Channel' : 'Supergroup';
|
||
}
|
||
}
|
||
|
||
console.log(`${index + 1}. [${type}] ${title}`);
|
||
});
|
||
|
||
console.log('\n测试成功!getDialogs方法工作正常。');
|
||
|
||
} catch (error) {
|
||
console.error('获取对话列表失败:', error);
|
||
}
|
||
|
||
await client.disconnect();
|
||
process.exit(0);
|
||
})(); |