Files
telegram-management-system/backend/telegram-web-server.js
你的用户名 237c7802e5
Some checks failed
Deploy / deploy (push) Has been cancelled
Initial commit: Telegram Management System
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>
2025-11-04 15:37:50 +08:00

137 lines
4.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const PORT = 8890;
// 解析JSON请求体
app.use(bodyParser.json());
// 存储session信息
const sessions = {};
// 保存session信息
app.post('/api/session', (req, res) => {
const { accountId, sessionData } = req.body;
sessions[accountId] = sessionData;
res.json({ success: true });
});
// 获取session信息
app.get('/api/session/:accountId', (req, res) => {
const { accountId } = req.params;
const sessionData = sessions[accountId];
if (sessionData) {
res.json({ success: true, data: sessionData });
} else {
res.json({ success: false, message: 'Session not found' });
}
});
// 提供静态HTML页面
app.get('/web/:accountId', (req, res) => {
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Telegram Web</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
#telegram-frame {
width: 100%;
height: 100%;
border: none;
}
.loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-family: Arial, sans-serif;
}
.loading-spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto 20px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div id="loading" class="loading">
<div class="loading-spinner"></div>
<div>正在加载 Telegram Web...</div>
</div>
<iframe id="telegram-frame" style="display: none;"></iframe>
<script>
const accountId = '${req.params.accountId}';
const frame = document.getElementById('telegram-frame');
const loading = document.getElementById('loading');
// 加载Telegram Web
frame.src = 'https://web.telegram.org/k/';
frame.onload = async function() {
// 等待Telegram Web加载完成
setTimeout(async () => {
try {
// 获取session数据
const response = await fetch('/api/session/' + accountId);
const result = await response.json();
if (result.success && result.data) {
// 尝试注入session注意由于安全限制可能无法直接操作iframe内容
console.log('Session data ready for account:', accountId);
// 可以通过postMessage尝试通信
frame.contentWindow.postMessage({
type: 'telegram-session',
data: result.data
}, 'https://web.telegram.org');
}
} catch (error) {
console.error('Failed to load session:', error);
}
// 显示iframe
loading.style.display = 'none';
frame.style.display = 'block';
}, 2000);
};
</script>
</body>
</html>
`);
});
// 代理Telegram Web请求可选
app.use('/telegram', createProxyMiddleware({
target: 'https://web.telegram.org',
changeOrigin: true,
pathRewrite: {
'^/telegram': ''
}
}));
app.listen(PORT, () => {
console.log(`Telegram Web Server running on http://localhost:${PORT}`);
console.log(`访问 http://localhost:${PORT}/web/{accountId} 来使用Telegram Web`);
});