-
Notifications
You must be signed in to change notification settings - Fork 8
/
bot.js
178 lines (166 loc) · 5.97 KB
/
bot.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
'use strict';
require('dotenv').config();
const path = require('path');
const url = require('url');
const request = require('request');
const TelegramBot = require('node-telegram-bot-api');
const prettysize = require('prettysize');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
// Check if we have a valid API key
if (!process.env.TELEGRAMKEY || !process.env.MAXSIZEBYTES || isNaN(process.env.MAXSIZEBYTES)) {
console.error('Telegram API key was not, MaxSize was not set, or .env file is missing');
process.exit(1);
}
function initListeners(username) {
// Telegram Required Commands
// We need to escape our username for regex use
// Credit bobince & nhahtdh of stackoverflow
// https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
username = username.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
// These commands are required to have responses by the Telegram API
telegram.onText(RegExp('/start(?:@' + username + ')?$', 'i'), function(msg, match) {
console.log('[webm2mp4] New private chat started with', msg.from);
telegram.sendMessage(msg.chat.id, 'Hello! Upload an WebM for me to convert it to a MP4.');
});
telegram.onText(RegExp('/help(?:@' + username + ')?$', 'i'), function(msg, match) {
console.log('[webm2mp4] Help command used by', msg.from);
telegram.sendMessage(msg.chat.id, 'Hello! Upload an WebM for me to convert it to a MP4. I can also be added to group chats to automatically convert WebMs.');
});
telegram.onText(new RegExp('(https?:\\/\\/[^\\s]+.webm)'), function (msg, match) {
let filename;
let r = request(match[0]).on('response', function (res) {
let contentDisp = res.headers['content-disposition'];
if (contentDisp && /^attachment/i.test(contentDisp)) {
filename = contentDisp.toLowerCase()
.split('filename=')[1]
.split(';')[0]
.replace(/"/g, '');
} else {
filename = path.basename(url.parse(match[0]).path);
}
console.log(filename);
r.pipe(fs.createWriteStream(path.join(__dirname, filename)));
});
r.on('end', function () {
ffmpeg(filename)
.output(filename + '.mp4')
.outputOptions('-strict -2') // Needed since axc is "experimental"
.on('end', () => {
// Cleanup
fs.unlink(filename, (e) => {
if (e) {
console.error(e);
}
});
console.log('[webm2mp4] File', filename, 'converted - Uploading...');
telegram.sendVideo(msg.chat.id, filename + '.mp4').then(function() {
fs.unlink(filename + '.mp4', (e) => {
if (e) {
console.error(e);
}
});
});
})
.on('error', (e) => {
console.error(e);
// Cleanup
fs.unlink(filename, (err) => {
if (err) {
console.error(err);
}
});
fs.unlink(filename + '.mp4', (err) => {
if (err) {
console.error(err);
}
});
})
.run();
});
});
// The real meat of the bot
telegram.on('document', (msg) => {
if (msg.document.mime_type === 'video/webm') {
// Check the file size
if (msg.document.file_size > process.env.MAXSIZEBYTES) {
console.log(process.env.MAXSIZEBYTES)
console.log('[webm2mp4] ', msg.from, ' uploaded a file that was too big.');
telegram.sendMessage(msg.chat.id, 'This file is too large for me to convert. It must be less than ' + prettysize(process.env.MAXSIZEBYTES) + '.');
return;
}
// Download it
telegram.downloadFile(msg.document.file_id, './tmp/').then(function(filename) {
ffmpeg(filename)
.output(filename + '.mp4')
.outputOptions('-strict -2') // Needed since axc is "experimental"
.on('end', () => {
// Cleanup
fs.unlink(filename, (e) => {
if (e) {
console.error(e);
}
});
console.log('[webm2mp4] File', msg.document.file_name, 'converted - Uploading...');
telegram.sendVideo(msg.chat.id, filename + '.mp4').then(function() {
fs.unlink(filename + '.mp4', (e) => {
if (e) {
console.error(e);
}
});
});
})
.on('error', (e) => {
console.error(e);
// Cleanup
fs.unlink(filename, (err) => {
if (err) {
console.error(err);
}
});
fs.unlink(filename + '.mp4', (err) => {
if (err) {
console.error(err);
}
});
})
.run();
})
}
});
}
// Init
process.env.MAXSIZEBYTES = parseInt(process.env.MAXSIZEBYTES);
if (!fs.existsSync('./tmp/')) {
fs.mkdirSync('./tmp/');
}
let telegram = null;
// Are we on Heroku?
if (process.env.PORT && !isNaN(process.env.PORT)) {
if (process.env.APPURL) {
telegram = new TelegramBot(process.env.TELEGRAMKEY, {
webHook: {
port: process.env.PORT
}
});
telegram.setWebHook(`${process.env.APPURL}/bot${process.env.TELEGRAMKEY}`);
} else {
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Alive!')
});
server.listen(parseInt(process.env.PORT), (e) => {
if (e) {
return console.error(e)
}
});
}
} else {
telegram = new TelegramBot(process.env.TELEGRAMKEY, {
polling: true
}); // Polling so we don't have to deal with NAT
}
telegram.getMe().then(function(me) {
console.log('[Telegram] Telegram connection established. Logged in as:', me.username);
initListeners(me.username);
});