Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
195 lines (180 loc) · 6.19 KB
/
Copy pathserver.js
File metadata and controls
195 lines (180 loc) · 6.19 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
/*
_ _ _ _ _
__| (_)___ ___ ___ _ __ __| | | |_ ___| | ___ __ _ _ __ __ _ _ __ ___
/ _` | / __|/ __/ _ \| '__/ _` |_____| __/ _ \ |/ _ \/ _` | '__/ _` | '_ ` _ \
| (_| | \__ \ (_| (_) | | | (_| |_____| || __/ | __/ (_| | | | (_| | | | | | |
\__,_|_|___/\___\___/|_| \__,_| \__\___|_|\___|\__, |_| \__,_|_| |_| |_|
|___/
_ _ _
| |__ _ __(_) __| | __ _ ___
| '_ \| '__| |/ _` |/ _` |/ _ \
| |_) | | | | (_| | (_| | __/
|_.__/|_| |_|\__,_|\__, |\___|
|___/
*/
// clear console before start -- useful for debugging, disabled from ver 1.0.1
// console.clear()
// support for .env file
require('dotenv').config()
// import env variables
var telegramToken = process.env.TELEGRAM_BOT_TOKEN;
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
var telegramChatId = process.env.TELEGRAM_CHAT_ID;
var discordChannelId = process.env.DISCORD_CHANNEL_ID;
const PORT = process.env.PORT || 3000;
const DYNO_URL = process.env.DYNO_URL || "https://google.com";
const fetch = require("node-fetch");
const wakeUpDyno = (url, interval = 25, callback) => {
const milliseconds = interval * 60000;
setTimeout(() => {
try {
console.log("setTimeout called.");
// HTTP GET request to the dyno's url
fetch(url).then(() => console.log(`Fetching ${url}.`));
} catch (err) {
// catch fetch errors
console.log(`Error fetching ${url}: ${err.message}
Will try again in ${interval} minutes...`);
} finally {
try {
callback(); // execute callback, if passed
} catch (e) {
// catch callback error
callback ? console.log("Callback failed: ", e.message) : null;
} finally {
// do it all again
return wakeUpDyno(url, interval, callback);
}
}
}, milliseconds);
};
// import modules
const Discord = require("discord.js");
const client = new Discord.Client();
const Telegram = require("telegraf/telegram");
const polka = require("polka");
const { Telegraf } = require("telegraf");
polka()
.get("/", (req, res) => {
res.end("Hello world!");
})
.listen(PORT, (err) => {
if (err) {
throw err;
}
console.log("> Running on localhost:" + PORT);
if (process.env._ && process.env._.indexOf("heroku") !== -1) {
console.log("> Starting wakeUpDyno");
wakeUpDyno(DYNO_URL);
} else {
console.log("> Not running on heroku");
}
});
const webhookClient = new Discord.WebhookClient(
process.env.webhook_id,
process.env.webhook_token
);
// initializes the telegram bot and starts listening for updates (new messages)
const bot = new Telegraf(telegramToken);
client.once("ready", () => {
console.log("Discord bot ready!");
});
// initializes discord bot
client.login(DISCORD_TOKEN);
// if the discord bot receives a message
client.on("message", (message) => {
if (
// 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.
message.channel.id == discordChannelId &&
message.author.bot === false
) {
const mentionedUsernames = [];
for (const mention of message.mentions.users) {
mentionedUsernames.push("@" + mention[1].username);
}
var attachmentUrls = [];
for (const 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, "");
bot.telegram.sendMessage(
telegramChatId,
message.author.username +
": " +
finalMessageContent +
" " +
attachmentUrls.join(" ") +
mentionedUsernames.join(" ")
);
}
});
var photoUrl = "";
bot.on("message", function (message) {
const messageUpdate = message.update;
const updateMsg = message.update.message;
var filePath = "";
if (updateMsg.chat.id == telegramChatId && updateMsg.from.is_bot === false) {
// this part gets the user profile photos as the variable names suggest
const getProfilePic = new Promise(function (resolve, reject) {
var profilePhotos = bot.telegram.getUserProfilePhotos(updateMsg.from.id);
profilePhotos.then(function (data) {
// if user has a profile photo
// console.log(data)
if (data.total_count > 0) {
var file = bot.telegram.getFileLink(data.photos[0][0].file_id);
file.then(function (result) {
resolve(result);
});
} else {
resolve("https://telegram.org/img/t_logo.png");
}
});
});
getProfilePic.then(function (profileUrl) {
// if the message contains media
if (updateMsg.document || updateMsg.photo || updateMsg.sticker) {
if (updateMsg.document) {
bot.telegram
.getFileLink(updateMsg.document.file_id)
.then(function (photoUrl) {
webhookClient.send(updateMsg.caption, {
username: updateMsg.from.first_name,
avatarURL: profileUrl,
files: [photoUrl],
});
});
}
if (updateMsg.sticker) {
bot.telegram
.getFileLink(updateMsg.sticker.file_id)
.then(function (photoUrl) {
webhookClient.send(updateMsg.caption, {
username: updateMsg.from.first_name,
avatarURL: profileUrl,
files: [photoUrl],
});
});
}
if (updateMsg.photo) {
bot.telegram
.getFileLink(updateMsg.photo[0].file_id)
.then(function (photoUrl) {
webhookClient.send(updateMsg.caption, {
username: updateMsg.from.first_name,
avatarURL: profileUrl,
files: [photoUrl],
});
});
}
} else {
webhookClient.send(updateMsg.text, {
username: updateMsg.from.first_name,
avatarURL: profileUrl,
});
}
});
}
});
bot.launch();