Giter Club home page Giter Club logo

import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;

public class YouTubeDownloaderBot extends TelegramLongPollingBot {

private static final String YOUR_BOT_TOKEN = "YOUR_BOT_TOKEN";

@Override
public String getBotToken() {
    return YOUR_BOT_TOKEN;
}

@Override
public void onUpdateReceived(Update update) {
    if (update.hasMessage() && update.getMessage().hasText()) {
        String messageText = update.getMessage().getText();
        long chatId = update.getMessage().getChatId();

        if (messageText.startsWith("/download")) {
            String videoId = messageText.substring("/download".length());
            String command = String.format("youtube-dl -o \"%%(title)s.%%(ext)s\" https://www.youtube.com/watch?v=%s", videoId);

            try {
                Process process = Runtime.getRuntime().exec(command);
                int exitCode = process.waitFor();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                StringBuilder output = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }

                String response = (exitCode == 0) ? "Download successful!" : "Download failed.";

                SendMessage message = new SendMessage()
                        .setChatId(chatId)
                        .setText(response + "\n\n" + output.toString());
                execute(message);
            } catch (IOException | InterruptedException | TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }
}

@Override
public String getBotUsername() {
    return "YourBotUsername";
}

public static void main(String[] args) {
    TelegramBotsApi botsApi = new TelegramBotsApi();

    try {
        botsApi.registerBot(new YouTubeDownloaderBot());
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }
}

}

MN TG 's Projects

auto-reqaccept-bot icon auto-reqaccept-bot

This Is A Telegram Bot For Automatically Accept Chat Join Request With Broadcast & Users Database Feature

dq-the-file-donor icon dq-the-file-donor

DQ-The-File-Donor is a simple and powerful Aᴜᴛᴏ Fɪʟᴛᴇʀ Bᴏᴛ repository which supports Koyeb, Heroku, Okteto, Railway, Mogenius and VPS deployment.

file-sharing-bot icon file-sharing-bot

Telegram Bot to store Posts and Documents and it can Access by Special Links.

kodi icon kodi

Kodi is an award-winning free and open source home theater/media center software and entertainment hub for digital media. With its beautiful interface and powerful skinning engine, it's available for Android, BSD, Linux, macOS, iOS, tvOS and Windows.

newfilestore icon newfilestore

Miss Rozy is an advance permanent file store bot by @LazyDeveloperr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.