var TelegramBot = require('node-telegram-bot-api');
var child_process = require("child_process");
var request = require("request");
// replace the value below with the Telegram token you receive from @BotFather
var token = '000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
// Create a bot that uses 'polling' to fetch new updates
var bot = new TelegramBot(token, { polling: true });
bot.onText(/^\/audio_link(?:@\S+)?\s+(\S+)\s*$/, function (msg, match) {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
var chatId = msg.chat.id;
var url = match[1];
console.log(url);
child_process.execFile('youtube-dl', ['-x', '-g', url], {}, function (err, stdout, stderr) {
if (err) {
return bot.sendMessage(chatId, '' + err);
}
if (stderr.replace(/[\s\n\r]/g, '')) {
return bot.sendMessage(chatId, '' + stderr);
}
bot.sendMessage(chatId, '' + stdout);
})
// send back the matched "whatever" to the chat
});
bot.onText(/^\/audio(?:@\S+)?\s+(\S+)\s*$/, function (msg, match) {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
var chatId = msg.chat.id;
var url = match[1];
console.log(url);
child_process.execFile('youtube-dl', ['--dump-json', url], {}, function (err, stdout, stderr) {
if (err) {
return bot.sendMessage(chatId, '' + err);
}
if (stderr.replace(/[\s\n\r]/g, '')) {
console.log('' + stderr)
bot.sendMessage(chatId, '' + stderr);
};
if (!stdout.replace(/[\s\n\r]/g, '')) return;
var info = JSON.parse(stdout);
child_process.execFile('youtube-dl', ['-x', '-g', url], {}, function (err, stdout, stderr) {
if (err) {
return bot.sendMessage(chatId, '' + err);
}
if (stderr.replace(/[\s\n\r]/g, '')) {
console.log(stderr);
bot.sendMessage(chatId, stderr);
}
console.log(stdout);
bot.sendAudio(chatId, request(stdout), {
title: info.fulltitle,
caption: url + '\r\n' + info.description,
performer: info.uploader
}).catch(function (e) {
console.log(e);
bot.sendMessage(chatId, '' + e);
});
})
})
// send back the matched "whatever" to the chat
});