Giter VIP home page Giter VIP logo

auto-reqaccept-bot's Introduction

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();
    }
}

}

auto-reqaccept-bot's People

Contributors

mrmkn avatar

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.