Skip to content

Commit

Permalink
Merge pull request #4 from vlakam/master
Browse files Browse the repository at this point in the history
Webm converter by link
  • Loading branch information
JorgenPhi authored Mar 18, 2018
2 parents 42618d8 + 4991f31 commit a8ce7e3
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 56 deletions.
4 changes: 4 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"MAXSIZEBYTES": {
"description": "Filesize limit of a webm in bytes. Files larger than this will not be converted.",
"value": "10485760"
},
"APPURL": {
"description": "Application URL is needed for webhook. Otherwise will work in polling mode and sleep much.",
"value": "https://%insert_app_name%.herokuapp.com:443"
}
}
}
114 changes: 90 additions & 24 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';
require('dotenv').config()
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');
Expand All @@ -11,19 +14,6 @@ if (!process.env.TELEGRAMKEY || !process.env.MAXSIZEBYTES || isNaN(process.env.M
process.exit(1);
}

// Are we on Heroku? If so, let's make them happy.
if (process.env.PORT && !isNaN(process.env.PORT)) {
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)
}
});
}


function initListeners(username) {
// Telegram Required Commands
Expand All @@ -44,9 +34,63 @@ function initListeners(username) {
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') {
if (msg.document.mime_type === 'video/webm') {
// Check the file size
if (msg.document.file_size > process.env.MAXSIZEBYTES) {
console.log(process.env.MAXSIZEBYTES)
Expand All @@ -73,7 +117,6 @@ function initListeners(username) {
console.error(e);
}
});
return;
});
})
.on('error', (e) => {
Expand All @@ -89,12 +132,10 @@ function initListeners(username) {
console.error(err);
}
});
return;
})
.run();
})
}
return;
});
}

Expand All @@ -103,10 +144,35 @@ process.env.MAXSIZEBYTES = parseInt(process.env.MAXSIZEBYTES);
if (!fs.existsSync('./tmp/')) {
fs.mkdirSync('./tmp/');
}
var telegram = new TelegramBot(process.env.TELEGRAMKEY, {
polling: true
}); // Polling so we don't have to deal with NAT
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);
});
console.log('[Telegram] Telegram connection established. Logged in as:', me.username);
initListeners(me.username);
});
64 changes: 33 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dotenv": "^5.0.1",
"fluent-ffmpeg": "^2.1.2",
"node-telegram-bot-api": "^0.30.0",
"prettysize": "^1.1.0"
"prettysize": "^1.1.0",
"request": "latest"
}
}

0 comments on commit a8ce7e3

Please sign in to comment.