skip to Main Content

I create a telegram bot for downloading youtube videos.
I use telegraf api (javascripts) and youtube-dl. As I know telegram bot can currently send video files of up to 50 MB in size, but I need to configure downloading more, e.g 1 gb. How I can do this, any ideas?

Code:

const { Telegraf } = require('telegraf');
const fs = require('fs');
const youtubedl = require('youtube-dl');

const bot = new Telegraf('mytoken');

var DOWN_URL = "https://www.youtube.com/watch?v=";
var TeleMaxData = 50;
var videosize;
let downloaded = 0

bot.command('/video', async (ctx) => {
    try {
        //let userID = ctx.from["id"];
        let videoURL = 'someYoutubeUrl';
        ctx.reply(`Youtube video URL: ${videoURL}`);

        var video = youtubedl(videoURL,
            ['--format=18'],
            { cwd: __dirname });

        video.on('info', function (info) {
            infor = info;
            ctx.reply('info', info)
            videosize = infor.size / 1000000;

            if (videosize < TeleMaxData) {
                ctx.reply('Download Started')
                video.pipe(fs.createWriteStream(`test.mp4`, { flags: 'a' }));

                video.on('end', async function () {
                    ctx.reply("Download completed");
                    try {
                        ctx.reply(`Download completed!nVideo gets Send! - This might take a few Seconds! n n Title: n ${infor.title}. It's ${videosize}mb big.`);
                        await ctx.replyWithVideo({
                            source: fs.createReadStream(`test.mp4`)
                        })
                    } catch (err) {
                        ctx.reply('Error: sendVideo' + err);
                    }
                })
            } else {
                ctx.reply(`The Video is ${videosize}mb. The maximum size for sending videos from Telegram is ${TeleMaxData}mb.`);
            }
        });
    } catch (err) {
        ctx.reply("ERROR" + err);
    }
})

2

Answers


  1. I know some article, where person makes some agent who will upload file and bot just resend this message. This article in Russian, but i can translate some steps.

    1. You should go to https://core.telegram.org and following the
      instructions sign up your app
    2. You should receive api_id and
      api_hash

    How it works?

    1. App working on server through BOT API make file for sending
    2. It invoke agent for downloading file on Telegram servers
    1. It receives file_id from agent
    2. Use this file

    Btw, this person write on python, there some photos of his code, hope you will understand this.
    Sorry for my English, it’s not perfect
    Link: https://habr.com/ru/post/348234/

    Login or Signup to reply.
  2. Using a Local Bot API Server you can send a large file up to 2GB.

    GitHub Source Code:

    https://github.com/tdlib/telegram-bot-api

    Official Documentation

    https://core.telegram.org/bots/api#using-a-local-bot-api-server

    You can build and install this to your server by following the instructions on this link https://tdlib.github.io/telegram-bot-api/build.html

    basic setup :

    1. Generate Telegram Applications id from https://my.telegram.org/apps
    2. Start the server ./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20
    3. Default address is http://127.0.0.1:8081/ and the port is 8081.
    4. All the official APIs will work with this setup. Just change the address to http://127.0.0.1:8081/bot<token>/METHOD_NAME reference: https://core.telegram.org/bots/api

    Example Code with axios:

    var axios = require('axios');
    var FormData = require('form-data');
    var fs = require('fs');
    var data = new FormData();
    data.append('chat_id', 'chat-id-here');
    data.append('video', fs.createReadStream('path-to-file'));
    data.append('supports_streaming', 'true');
    
    var config = {
      method: 'post',
    maxBodyLength: Infinity,
      url: 'http://localhost:8081/bot[bot-api-key-here]/sendVideo',
      headers: { 
        ...data.getHeaders()
      },
      data : data
    };
    
    axios(config)
    .then(function (response) {
      console.log(JSON.stringify(response.data));
    })
    .catch(function (error) {
      console.log(error);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search