This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
203 lines (184 loc) · 5.23 KB
/
Copy pathindex.js
File metadata and controls
203 lines (184 loc) · 5.23 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
196
197
198
199
200
201
202
203
const { Telegraf } = require("telegraf");
const axios = require("axios").default;
const settings = require("./settings");
const rateLimit = require("telegraf-ratelimit");
const apiUrl = settings.portalUrl + "/skynet/skyfile";
const bot = new Telegraf(settings.telegramBotToken);
// config for telegraf ratelimit middleware
const limitConfig = {
window: settings.rateLimitTime,
limit: 1,
onLimitExceeded: (ctx) =>
ctx.reply(
`Rate limit exceeded. Max 1 upload per ${settings.rateLimitTime / 1000}s`
),
};
// takes a telegram file id, downloads the file and uploads it to skynet
async function uploadFile(fileId, filename, ctx) {
let reply;
try {
reply = await ctx.reply(`Downloading file...`, {
reply_to_message_id: ctx.message.message_id,
});
let url = await ctx.telegram.getFileLink(fileId); // get telegram file url
let response = await axios.get(url, { responseType: "stream" }); // download file
ctx.telegram.editMessageText(
ctx.chat.id,
reply.message_id,
null,
"Uploading to Skynet..."
);
// upload to skynet
axios
.post(apiUrl + "?filename=" + filename, response.data, {
maxContentLength: Infinity,
})
.then((resp) => {
if (resp.status !== 200) {
ctx.telegram.editMessageText(
ctx.chat.id,
reply.message_id,
null,
"Error while uploading file to skynet ☹️"
);
}
// update reply with skylink
ctx.telegram.editMessageText(
ctx.chat.id,
reply.message_id,
null,
`${settings.portalUrl}/${resp.data.skylink}`,
{ disable_web_page_preview: true }
);
})
.catch(console.error);
} catch (error) {
if (!reply) return;
ctx.telegram.editMessageText(
ctx.chat.id,
reply.message_id,
null,
"Error while uploading file to skynet ☹️"
);
// handle telegram 20mb donwload filesize limit
if (error.message === "400: Bad Request: file is too big") {
ctx.telegram.editMessageText(
ctx.chat.id,
reply.message_id,
null,
"Error: File too big.\nTelegram has a 20MB filesize limit for bots."
);
} else console.error(error);
}
}
// upload string as a textfile to skynet
async function uploadText(ctx) {
let reply;
try {
reply = await ctx.reply(`Uploading text...`, {
reply_to_message_id: ctx.message.message_id,
});
let filename = `text_${new Date().getTime()}.txt`;
axios
.post(apiUrl + "?filename=" + filename, ctx.message.text, {
maxContentLength: Infinity,
})
.then((resp) => {
ctx.telegram.editMessageText(
ctx.chat.id,
reply.message_id,
null,
`${settings.portalUrl}/${resp.data.skylink}`,
{ disable_web_page_preview: true }
);
});
} catch (error) {
if (!reply) return;
console.error(error);
ctx.telegram.editMessageText(
ctx.chat.id,
reply.message_id,
null,
"Error while uploading text to skynet ☹️"
);
}
}
// telegraf bot events
bot.start((ctx) =>
ctx.reply(settings.startMsg, { disable_web_page_preview: true })
);
bot.use(rateLimit(limitConfig));
bot.help((ctx) => ctx.reply(settings.helpMsg));
bot.command("source", (ctx) => {
ctx.reply("Source code: https://github.com/Delivator/skynet-telegram-bot");
});
bot.on("document", async (ctx) => {
uploadFile(ctx.message.document.file_id, ctx.message.document.file_name, ctx);
});
bot.on("photo", (ctx) => {
let file = ctx.message.photo.slice(-1).pop().file_id;
uploadFile(file, `telegram-photo_${ctx.message.date}.jpg`, ctx);
});
bot.on("voice", (ctx) => {
uploadFile(
ctx.message.voice.file_id,
`telegram-audio_${ctx.message.date}.ogg`,
ctx
);
});
bot.on("audio", (ctx) => {
uploadFile(
ctx.message.audio.file_id,
`telegram-audio_${ctx.message.date}.mp3`,
ctx
);
});
bot.on("video", (ctx) => {
uploadFile(
ctx.message.video.file_id,
`telegram-video_${ctx.message.date}.mp4`,
ctx
);
});
bot.on("video_note", (ctx) => {
uploadFile(
ctx.message.video_note.file_id,
`telegram-video_${ctx.message.date}.mp4`,
ctx
);
});
bot.on("sticker", (ctx) => {
const filename = ctx.message.sticker.is_animated
? `animated-telegram-sticker_${ctx.message.date}.tgs`
: `telegram-sticker_${ctx.message.date}.webp`;
uploadFile(ctx.message.sticker.file_id, filename, ctx);
});
bot.on("text", (ctx) => {
uploadText(ctx);
});
bot.catch((err, ctx) => {
console.log(`Telegraf encountered an error for ${ctx.updateType}`, err);
});
// start telegraf bot
bot.launch().then(async () => {
const commands = [
{
command: "help",
description: "Show help message",
},
{
command: "source",
description: "Show GitHub link",
},
];
// Check if the bot has commands set, if not apply the default commands above
if ((await bot.telegram.getMyCommands()).length < 1) {
console.log("No commands found, adding default");
await bot.telegram
.setMyCommands(commands)
.then(() => console.log("Commands set"))
.catch(console.error);
}
console.log("Bot ready!");
console.log(`Add me: https://t.me/${bot.options.username}`);
});