-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
235 lines (219 loc) · 7 KB
/
index.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Importing the modules. */
const { Telegraf } = require('telegraf')
const fs = require('fs')
const ytdl = require('ytdl-core')
const yaml = require('js-yaml')
const Path = require('path')
const translate = require('translate-google')
/* It's importing the modules, and it's creating the bot. */
const messages = yaml.load(fs.readFileSync('./messages.yml', 'utf8'))
const variables = yaml.load(fs.readFileSync('./variables.yml', 'utf8'))
const bot = new Telegraf(variables.token)
const ctx = bot.context
/**
* It checks if a file exists in the current directory.
* @param pathFile - The path to the file you want to check.
* @returns A boolean value.
*/
function fileExist(pathFile) {
const path = Path.join(__dirname, pathFile)
return fs.existsSync(path)
}
/**
* It downloads a video from YouTube, then checks if the video is less than 50MB, if it is, it sends it
* to the user, if it's not, it sends a link to the video.
* @param URL - The URL of the video you want to download.
*/
function downloadVideo(URL) {
let language = ctx.from?.language_code
ytdl.getBasicInfo(URL).then((info) => {
console.log(
'ID: ' +
ctx.from?.id +
' - Descargando el video - ' +
info.videoDetails.title,
)
ytdl(URL, {
format: 'mp4',
})
.pipe(
fs.createWriteStream(`./bot-youtube/${info.videoDetails.title}.mp4`),
)
.on('finish', () => {
let vidPath = `./bot-youtube/${info.videoDetails.title}.mp4`
let vidInfo = fs.statSync(vidPath)
let fileSizeMB = vidInfo.size / (1024 * 1024)
translate(messages.es.videoDownloadSuccess, {
from: 'es',
to: language,
})
.then((res) => {
ctx.reply(res)
})
.catch((err) => {
console.error(err)
ctx.reply(messages.error.errorLanguage)
})
if (fileSizeMB < 50 && fileSizeMB > 0) {
translate(messages.es.videoSend, {
from: 'es',
to: language,
})
.then((res) => {
ctx.reply(res)
})
.catch((err) => {
console.error(err)
ctx.reply(messages.error.errorLanguage)
})
console.log(`ID: ${ctx.from?.id} - Enviando video.`)
ctx.replyWithVideo({
source: fs.createReadStream(
`./bot-youtube/${info.videoDetails.title}.mp4`,
),
message: 'No olvides subscribirte a @MrNizzyApps',
})
} else if (fileSizeMB >= 50) {
translate(messages.es.limitTelegram, {
from: 'es',
to: language,
})
.then((res) => {
ctx.reply(
res +
`http://example.com/bot-youtube/${info.videoDetails.title}.mp4`,
)
})
.catch((err) => {
console.error(err)
ctx.reply(messages.error.errorLanguage)
})
} else {
translate(messages.es.videoNoFound, {
from: 'es',
to: language,
})
.then((res) => {
ctx.reply(res)
})
.catch((err) => {
console.error(err)
ctx.reply(messages.error.errorLanguage)
})
}
})
.on('error', () => {
translate(messages.es.errorDownload, {
from: 'es',
to: language,
})
.then((res) => {
ctx.reply(res)
})
.catch((err) => {
console.error(err)
ctx.reply(messages.error.errorLanguage)
})
})
})
}
/* It's a command that sends a message to the user. */
bot.command('start', (context) => {
let language = context.from?.language_code
translate(messages.es.start, { from: 'es', to: language })
.then((res) => {
context.reply(res)
})
.catch((err) => {
console.error(err)
context.reply(messages.error.errorLanguage)
})
})
/* It's a function that checks if the URL is valid, if it is, it checks if the video is already
downloaded, if it is, it sends it to the user, if it's not, it downloads it. */
bot.url((context) => {
let language = context.from?.language_code
let URL = context.message.text
if (ytdl.validateURL(URL)) {
translate(messages.es.videoProcess, { from: 'es', to: language })
.then((res) => {
context.reply(res)
})
.catch((err) => {
console.error(err)
context.reply(messages.error.errorLanguage)
})
console.log(`ID: ${context.from?.id} - Procesando video...`)
ytdl.getBasicInfo(URL).then((info) => {
if (fileExist(`./bot-youtube/${info.videoDetails.title}.mp4`)) {
let vidPath = `./bot-youtube/${info.videoDetails.title}.mp4`
let vidInfo = fs.statSync(vidPath)
let fileSizeMB = vidInfo.size / (1024 * 1024)
if (fileSizeMB < 50 && fileSizeMB > 0) {
translate(messages.es.videoSend, { from: 'es', to: language })
.then((res) => {
context.reply(res)
})
.catch((err) => {
console.error(err)
context.reply(messages.error.errorLanguage)
})
console.log(`ID: ${context.from?.id} - Enviando video.`)
context.replyWithVideo({
source: fs.createReadStream(
`./bot-youtube/${info.videoDetails.title}.mp4`,
),
message: 'No olvides subscribirte a @MrNizzyApps',
})
} else if (fileSizeMB >= 50) {
translate(messages.es.limitTelegram, { from: 'es', to: language })
.then((res) => {
context.reply(res)
})
.catch((err) => {
console.error(err)
context.reply(messages.error.errorLanguage)
})
} else {
translate(messages.es.videoNoFound, { from: 'es', to: language })
.then((res) => {
context.reply(res)
})
.catch((err) => {
console.error(err)
context.reply(messages.error.errorLanguage)
})
}
} else {
downloadVideo(URL)
}
})
} else {
console.log('URL incorrecta por usuario.')
translate(messages.es.incorrectURL, { from: 'es', to: language })
.then((res) => {
context.reply(res)
})
.catch((err) => {
console.error(err)
context.reply(messages.error.errorLanguage)
})
}
})
/* It's a command that sends a message to the user. */
bot.command('help', (context) => {
let language = context.from?.language_code
translate(messages.es.help, { from: 'es', to: language })
.then((res) => {
context.reply(res)
})
.catch((err) => {
console.error(err)
context.reply(messages.error.errorLanguage)
})
})
/* It's starting the bot. */
bot.launch()
/* It's a function that stops the bot when you press Ctrl + C. */
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))