skip to Main Content

I’m building my first telegram bot. It send one message every 5 seconds to the user.

While it sends it to one user it cannot receive update from other chat.

public void foo(msg, Update update){
    msg.setChatId(update.getMessage().getChatId());
    for (int i = 1; i < links.size(); i++){
        msg.setText(links.get(i));
        execute(msg);
    }
    Thread.sleep(wait * 1000);
}

How can I use Thread? I’ve tried creating multiple thread here

public static void bot(){

    ApiContextInitializer.init();
    TelegramBotsApi  telegramBotsApi = new TelegramBotsApi();
    try {
        telegramBotsApi.registerBot(new myBot());
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }

But he tries to create multiple bots and fails. Same if this is the runnable function:

How can I do it? I’m Stuck. I cannot create this function in different thread

public void onUpdateReceived(Update update) {

    leggi(new SendMessage(), update.getMessage().getText(),  update);

    //.setChatId(update.getMessage().getChatId())


public void  leggi(SendMessage msg, String command, Update update){ 
    if(command.equals("test") {
        foo( msg, update);
    }

Here the full code… https://github.com/siamoInPochi/Ilsottomarinobot/tree/prova/src/main/java/Ilsottomarinobot

2

Answers


  1. If you spawn a thread for every bot user who wants to receive messages, you will quickly be out of computer’s resources in case of high number of users. So I think threads is not a good idea for your task.

    In my mind more natural approach is the following:

    • Find a library with an HTTP server.
    • Switch from GetUpdates to webhooks.
    • Schedule send-message-to-user-every-5-seconds tasks to server’s event loop.
    • Send messages every 5 seconds asynchronously.
    Login or Signup to reply.
  2. You can make it with this library https://github.com/pengrad/java-telegram-bot-api

    <dependency>
      <groupId>com.github.pengrad</groupId>
      <artifactId>java-telegram-bot-api</artifactId>
      <version>4.2.0</version>
    </dependency>
    
    1. Subscribe to new updates via bot.setUpdatesListener
    2. Send messages via bot.execute(new SendMessage(chatId, link), callback)

    Full working example:

    static String[] links = {"1", "2", "3"};
    
    static Callback emptyCallback = new Callback() {
        @Override
        public void onResponse(BaseRequest request, BaseResponse response) {
        }
    
        @Override
        public void onFailure(BaseRequest request, IOException e) {
            e.printStackTrace();
        }
    };
    
    static void foo(TelegramBot bot, Update update) {
        Message message = update.message();
        if (message == null) return;
    
        Long chatId = message.chat().id();
        for (String link : links) {
            bot.execute(new SendMessage(chatId, link), emptyCallback);
        }
    }
    
    public static void main(String[] args) {
        TelegramBot bot = new TelegramBot(TOKEN);
    
        bot.setUpdatesListener(updates -> {
            for (Update update : updates) {
                foo(bot, update);
            }
            return UpdatesListener.CONFIRMED_UPDATES_ALL;
        });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search