diff --git a/app.json b/app.json index f152fef..72a6d14 100644 --- a/app.json +++ b/app.json @@ -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" } } } diff --git a/bot.js b/bot.js index 6ccd840..91142c7 100644 --- a/bot.js +++ b/bot.js @@ -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'); @@ -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 @@ -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) @@ -73,7 +117,6 @@ function initListeners(username) { console.error(e); } }); - return; }); }) .on('error', (e) => { @@ -89,12 +132,10 @@ function initListeners(username) { console.error(err); } }); - return; }) .run(); }) } - return; }); } @@ -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); +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index e739120..ff77a93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -136,14 +136,6 @@ "assert-plus": "1.0.0" } }, - "debug": { - "version": "2.6.9", - "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", - "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", - "requires": { - "ms": "2.0.0" - } - }, "define-properties": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.2.tgz", @@ -164,9 +156,9 @@ "integrity": "sha1-V4O04cRZ8G+lyif5kfPQbnoxA1k=" }, "dotenv": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-4.0.0.tgz", - "integrity": "sha1-hk7xN5rO1Vzm+V3r7NzhefegzR0=" + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-5.0.1.tgz", + "integrity": "sha512-4As8uPrjfwb7VXC+WnLCbXK7y+Ueb2B3zgNCePYfhxS1PYeaO1YTeplffTEcbfLhvFNGLAz90VvJs9yomG7bow==" }, "ecc-jsbn": { "version": "0.1.1", @@ -207,11 +199,6 @@ "is-symbol": "1.0.1" } }, - "eventemitter3": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-2.0.3.tgz", - "integrity": "sha1-teEHm1n7XhuidxwKmTvgYKWMmbo=" - }, "extend": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", @@ -436,21 +423,45 @@ "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, "node-telegram-bot-api": { - "version": "0.29.0", - "resolved": "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-0.29.0.tgz", - "integrity": "sha512-WZBp5ccr0zMLRNtZ3avrevGpj05mrN59lDmxgpltls0bl3W/wqyNalNGKatTZEJ1cY0pctgd9FqKyCzWr36HcQ==", + "version": "0.30.0", + "resolved": "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-0.30.0.tgz", + "integrity": "sha512-+EeM+fe3Xt81KIPqN3L6s6eK+FK4QaqyDcwCwkY/jqsleERXwwjGlVbf4lJCOZ0uJuF5PfqTmvVNtua7AZfBXg==", "requires": { "array.prototype.findindex": "2.0.2", "bl": "1.2.1", "bluebird": "3.5.1", - "debug": "2.6.9", + "debug": "3.1.0", "depd": "1.1.1", - "eventemitter3": "2.0.3", + "eventemitter3": "3.0.1", "file-type": "3.9.0", "mime": "1.6.0", - "pump": "1.0.3", + "pump": "2.0.1", "request": "2.83.0", "request-promise": "4.2.2" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "eventemitter3": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.0.1.tgz", + "integrity": "sha512-QOCPu979MMWX9XNlfRZoin+Wm+bK1SP7vv3NGUniYwuSJK/+cPA10blMaeRgzg31RvoSFk6FsCDVa4vNryBTGA==" + }, + "pump": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/pump/-/pump-2.0.1.tgz", + "integrity": "sha512-ruPMNRkN3MHP1cWJc9OWr+T/xDP0jhXYCLfJcBuX54hhfIBnaQmAUMfDcG4DM5UMWByBbJY69QSphm3jtDKIkA==", + "requires": { + "end-of-stream": "1.4.0", + "once": "1.4.0" + } + } } }, "oauth-sign": { @@ -486,15 +497,6 @@ "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", "integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=" }, - "pump": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/pump/-/pump-1.0.3.tgz", - "integrity": "sha512-8k0JupWme55+9tCVE+FS5ULT3K6AbgqrGa58lTT49RpyfwwcGedHqaC5LlQNdEAumn/wFsu6aPwkuPMioy8kqw==", - "requires": { - "end-of-stream": "1.4.0", - "once": "1.4.0" - } - }, "punycode": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", diff --git a/package.json b/package.json index 4ccf1b3..9ec3723 100644 --- a/package.json +++ b/package.json @@ -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" } }