-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserver.js
176 lines (148 loc) · 4.77 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
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
#!/usr/bin/env node
import { discordClient, discordWebhookClient } from './backends/discord.js';
import { telegram, telegramGetFileURL, telegramGetProfilePic } from './backends/telegram.js';
import { enable_heroku } from './utils/heroku.js';
if ("HEROKU_DYNO_URL" in process.env) {
enable_heroku();
}
// import env variables
const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID;
const DISCORD_CHANNEL_ID = process.env.DISCORD_CHANNEL_ID;
const DISCORD_FORWARD_BOT = (process.env.DISCORD_FORWARD_BOT === 'true')
console.log("Telegram chat id: " + TELEGRAM_CHAT_ID);
console.log("Discord channel id: " + DISCORD_CHANNEL_ID);
// Discord -> Telegram handler
discordClient.on("message", message => {
// the program currently check if the message's from a bot to check for duplicates.
// This isn't the best method but it's good enough.
// A webhook counts as a bot in the discord api, don't ask me why.
// Ignore messages from bots if DISCORD_FORWARD_BOT is 'false'
if (message.channel.id !== DISCORD_CHANNEL_ID || (message.author.bot && !DISCORD_FORWARD_BOT)) {
return;
}
let mentioned_usernames = []
for (let mention of message.mentions.users) {
mentioned_usernames.push("@" + mention[1].username);
}
var attachmentUrls = []
for (let attachment of message.attachments) {
attachmentUrls.push(attachment[1].url);
}
// attachmentUrls is empty when there are no attachments so we can be just lazy
var finalMessageContent = message.content.replace(/<@.*>/gi, '');
// convert bold text for telegram markdown
finalMessageContent = finalMessageContent.replace(/\*\*/g, '*');
var text = `*\[DISCORD\] ${message.author.username} (${message.author.username}#${message.author.discriminator}):*\n`;
text += finalMessageContent
text += ` ${attachmentUrls.join(' ')}`;
text += mentioned_usernames.join(" ");
try {
telegram.sendMessage(TELEGRAM_CHAT_ID, text, {parse_mode: 'markdown'});
}
catch(err) {
console.log(err.message);
return;
}
});
// Telegram -> Discord handler
telegram.on("message", async (message) => {
// console.log(message)
if (message.chat.id != TELEGRAM_CHAT_ID) {
return;
}
// Ignore messages from bots
if (message.from.is_bot) {
return;
}
var username = `[TELEGRAM] ${message.from.first_name}`;
if (message.from.last_name) {
username += ` ${message.from.last_name}`;
}
if (message.from.username) {
username += ` (@${message.from.username})`;
}
let profileUrl = await telegramGetProfilePic(message);
var text;
var fileId;
if (!message.document && !message.photo && !message.sticker) {
if (!message.text) {
return;
}
text = message.text;
// convert bold, italic & hyperlink Telegram text for Discord markdown
if (message.entities) {
text = convert_text_telegram_to_discord(text, message.entities);
}
} else {
text = message.caption;
// convert bold, italic & hyperlink Telegram text for Discord markdown
if (message.caption_entities) {
text = convert_text_telegram_to_discord(text, message.caption_entities);
}
if (message.document) {
fileId = message.document.file_id;
} else if (message.sticker) {
fileId = message.sticker.file_id;
} else if (message.photo) {
// pick the last/largest picture in the list
fileId = message.photo[message.photo.length - 1].file_id;
}
}
if (text) {
text = text.replace(/@everyone/g, "[EVERYONE]").replace(/@here/g, "[HERE]");
}
try {
var fileUrl = "";
if (fileId) {
var file = await telegram.getFile(fileId);
fileUrl = telegramGetFileURL(file.file_path);
if (fileUrl != "") {
discordWebhookClient.send(text, {
username: username, avatarURL: profileUrl, files: [fileUrl]
});
}
}
if (!fileId || fileUrl == "") {
await discordWebhookClient.send(text, {
username: username, avatarURL: profileUrl
});
}
}
catch(err) {
console.log(err.message);
return;
}
});
function convert_text_telegram_to_discord(text, entities) {
var convert;
var start_format;
var end_format;
var section_offset = 0
var section_end;
var section_start;
entities.forEach(({type, offset, length, url}) => {
convert = true;
if (type == 'bold') {
start_format = '\*\*';
end_format = '\*\*';
} else if(type == 'italic') {
start_format = '\_';
end_format = '\_';
} else if(type == 'text_link') {
start_format = '\*\*';
end_format = '\*\* (<' + url + '>)';
} else {
// Don't convert other entities
convert = false;
}
if (convert) {
section_start = offset + section_offset;
section_end = offset + length + section_offset;
// First add end_format, so it won't mess up the string indexes for start_format
text = text.slice(0, section_end) + end_format + text.slice(section_end);
text = text.slice(0, section_start) + start_format + text.slice(section_start);
section_offset += start_format.length + end_format.length;
}
});
return text
}