【问题标题】:Handle multiple user at the same time with TelegramLongPollingBot and thread使用 TelegramLongPollingBot 和线程同时处理多个用户
【发布时间】:2019-05-10 18:09:45
【问题描述】:

我正在构建我的第一个电报机器人。它每 5 秒向用户发送一条消息。

当它发送给一个用户时,它无法接收来自其他聊天的更新。

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

如何使用线程?我试过在这里创建多个线程

public static void bot(){

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

但他试图创建多个机器人并失败了。如果这是可运行函数,则相同:

我该怎么做?我卡住了。我不能在不同的线程中创建这个函数

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

这里有完整的代码...https://github.com/siamoInPochi/Ilsottomarinobot/tree/prova/src/main/java/Ilsottomarinobot

【问题讨论】:

    标签: java telegram-bot


    【解决方案1】:

    如果您为每个想要接收消息的机器人用户生成一个线程,那么在用户数量众多的情况下,您将很快耗尽计算机资源。所以我认为线程对你的任务来说不是一个好主意。

    在我看来更自然的方法如下:

    • 查找带有 HTTP 服务器的库。
    • GetUpdates 切换到 webhook。
    • send-message-to-user-every-5-seconds 任务安排到服务器的事件循环中。
    • 每 5 秒异步发送一次消息。

    【讨论】:

      【解决方案2】:

      你可以用这个库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. 通过bot.setUpdatesListener订阅新的更新
      2. 通过bot.execute(new SendMessage(chatId, link), callback)发送消息

      完整的工作示例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多