-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (131 loc) · 4.65 KB
/
server.js
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
require('dotenv').config();
const { Telegraf } = require('telegraf');
const { Client, Intents, MessageAttachment } = require('discord.js');
const axios = require('axios');
const express = require('express');
const moment = require('moment');
// Token Bot Telegram dan Discord
const TELEGRAM_TOKEN = process.env.TELEGRAM_TOKEN;
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
const DISCORD_CHANNEL_ID = process.env.DISCORD_CHANNEL_ID;
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL; // Discord webhook URL
const BRIDGE_MODE = process.env.BRIDGE_MODE || 'client'; // 'client' or 'webhook'
const bot = new Telegraf(TELEGRAM_TOKEN);
const discordClient = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const app = express();
const PORT = process.env.PORT || 3000;
let fileTransferData = []; // Array to store file transfer information
discordClient.once('ready', () => {
console.log(`Discord bot logged in as ${discordClient.user.tag}`);
});
// Function to download file as buffer
async function downloadFileToBuffer(url) {
const response = await axios.get(url, { responseType: 'arraybuffer' });
return Buffer.from(response.data, 'binary');
}
// Function to send message using Discord client
async function sendWithClient(content, buffer, fileName) {
const discordChannel = await discordClient.channels.fetch(DISCORD_CHANNEL_ID);
await discordChannel.send({ content, files: buffer ? [new MessageAttachment(buffer, fileName)] : [] });
}
// Function to send message using Discord webhook
async function sendWithWebhook(content, buffer, fileName) {
const form = new FormData();
form.append('content', content);
if (buffer && fileName) {
form.append('file', buffer, fileName);
}
await axios.post(DISCORD_WEBHOOK_URL, form, {
headers: form.getHeaders()
});
}
// Telegram bot - forward messages to Discord
bot.on('channel_post', async (ctx) => {
const message = ctx.channelPost;
console.log("Menerima pesan dari channel announcement:", message);
try {
let fileName = "";
let caption = message.caption ? message.caption : "";
let buffer = null;
if (message.text) {
// Send text message
if (BRIDGE_MODE === 'client') {
await sendWithClient(message.text, null, null);
} else {
await sendWithWebhook(message.text, null, null);
}
} else if (message.photo) {
// Send photo
const fileId = message.photo[message.photo.length - 1].file_id;
const fileUrl = await bot.telegram.getFileLink(fileId);
buffer = await downloadFileToBuffer(fileUrl);
fileName = 'photo.jpg';
if (BRIDGE_MODE === 'client') {
await sendWithClient(caption, buffer, fileName);
} else {
await sendWithWebhook(caption, buffer, fileName);
}
} else if (message.video) {
// Send video
const fileId = message.video.file_id;
const fileUrl = await bot.telegram.getFileLink(fileId);
buffer = await downloadFileToBuffer(fileUrl);
fileName = 'video.mp4';
if (BRIDGE_MODE === 'client') {
await sendWithClient(caption, buffer, fileName);
} else {
await sendWithWebhook(caption, buffer, fileName);
}
} else if (message.document) {
// Send document
const fileId = message.document.file_id;
const fileUrl = await bot.telegram.getFileLink(fileId);
buffer = await downloadFileToBuffer(fileUrl);
fileName = message.document.file_name;
if (BRIDGE_MODE === 'client') {
await sendWithClient(caption, buffer, fileName);
} else {
await sendWithWebhook(caption, buffer, fileName);
}
} else {
console.log("Pesan tipe lain diterima dari Telegram dan tidak di-forward.");
return;
}
// Log file transfer information
fileTransferData.push({
fileName,
timestamp: moment().format('YYYY-MM-DD HH:mm:ss'),
caption
});
} catch (error) {
console.error("Error saat mem-forward pesan ke Discord:", error);
}
});
// Express endpoint to display file transfer information
app.get('/transfers', (req, res) => {
res.send(`
<h1>File Transfers</h1>
<table border="1">
<tr>
<th>File Name</th>
<th>Timestamp</th>
<th>Caption</th>
</tr>
${fileTransferData.map(data => `
<tr>
<td>${data.fileName}</td>
<td>${data.timestamp}</td>
<td>${data.caption}</td>
</tr>
`).join('')}
</table>
`);
});
// Start Telegram bot, Discord client, and Express server
bot.launch();
if (BRIDGE_MODE === 'client') {
discordClient.login(DISCORD_TOKEN);
}
app.listen(PORT, () => {
console.log(`Express server running on http://localhost:${PORT}`);
});