【问题标题】:How do make my bot send message without an update?如何让我的机器人在没有更新的情况下发送消息?
【发布时间】:2020-10-06 21:17:08
【问题描述】:

我正在 java bot 中创建一个电报机器人,但我遇到了问题,我看到机器人在没有用户更新的情况下发送文本\广告,我想知道我该怎么做。它只在发送消息时发送消息用户向它发送消息。我需要知道如何让我的机器人在没有 onUpdateReceived 的情况下发送一些消息。(对不起我的英语)

onUpdateReceived(Update update) 只在用户发送命令时发送消息

谢谢。

【问题讨论】:

    标签: java telegram-bot


    【解决方案1】:

    只需创建一个发送消息的实例。 例如。

    SendMessage message = new SendMessage (chatid,text)
    

    然后让你的机器人执行实例。

    SendMessage message = new SendMessage (chatid,text)
    bot.execute(message)
    

    显然 chatid 阵营应该是硬编码的。

    【讨论】:

      【解决方案2】:

      onUpdateReceived(Update) 只是当你的机器人获得更新时调用的一个方法,但它不是你的机器人可以调用execute(SendMessage) 的唯一地方。 您需要的是在您的机器人中编写一个方法,例如

      public void sendAds() {
          for (Integer chatId: usersYouWantToPing) {
              SendMessage ad = new SendMessage
                  .setChatId(chatId)
                  .setText(text);
              execute(ad);
          }
      }
      

      显然,由于您没有 User 发件人对象,因此您必须找到发送消息的标准(也许您希望将要 ping 的用户的 ID 存储在数据库中)。

      现在的问题是:如何触发这个方法?答案是:随心所欲。

      一种方法是安排一些cron 作业 定期执行sendAds()。为此,您可以在注册机器人后立即在 main 方法中定义它。使用Quartz lib 你可以写类似

          /* Instantiate the job that will call the bot function */
          JobDetail jobSendAd = JobBuilder.newJob(SendAd.class)
              .withIdentity("sendAd")
              .build();
      
          /* Define a trigger for the call */
          Trigger trigger = TriggerBuilder
              .newTrigger()
              .withIdentity("everyMorningAt8")
              .withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(8, 0))
              .build();
      
          /* Create a scheduler to manage triggers */
          Scheduler scheduler = new StdSchedulerFactory().getScheduler();
          scheduler.getContext().put("bot", bot);
          scheduler.start();
          scheduler.scheduleJob(jobSendAd, trigger);
      

      其中SendAdJob 接口的实现,它实际上调用了bot 方法,比如

      public class SendNotification implements Job {
          public void execute(JobExecutionContext jobExecutionContext) {
              schedulerContext = jobExecutionContext.getScheduler().getContext();
              YourBot bot = (YourBot) schedulerContext.get("bot");
              bot.sendNotification();
          }
      }
      

      有关更多详细信息,我建议您查看我提供此解决方案的telegram bot template

      【讨论】:

        【解决方案3】:

        这种棘手的方法对我有用。

        Telegram 机器人需要知道该机器人与之交谈的特定群组或个人聊天的 chatID

        首先你必须得到 Long 的 chatId。

        在电报中借助 BotFather 创建 /chatid 命令。

        并像这样在 java 中使用该命令:

        public class TelegramBot extends TelegramLongPollingBot {
            public Long chatId = null;
        
            public void onUpdateReceived(Update update) {
        
                String input = update.getMessage().getText();
                SendMessage output = new SendMessage();
        
                if (input.equals("/chatid")) {
                    chatId = update.getMessage().getChatId();
                    System.out.println("chatId = " + chatId);
                    output.setText("chatid is  = " + chatId);
                }
        
                output.setChatId(update.getMessage().getChatId());
                try {
                    execute(output);
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
        
            }
        
            public void sayImNotRobot() {
            SendMessage message = new SendMessage();
        
            message.enableMarkdown(true);
            message.setChatId((long) chatIdLike123456); //Write chatID manually here
            message.setText("Im not robot");
            try {
                execute(message);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        
        }
            
            public String getBotUsername() {
                return "your bot name here";
            }
        
            public String getBotToken() {
                return "your bot token here";
            }
        }
        

        在主类中使用该方法将 chatId 打印到控制台并将其(从控制台结果)复制粘贴到我在 TelegramBot 类中编写的 chatIdLike123456。

         public class Main {
                public static void main(String[] args) {
                    ApiContextInitializer.init();
                    TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
                    try {
                        telegramBotsApi.registerBot(new TelegramBot());
                        TelegramBot bot = new TelegramBot();
                        bot.sayImNotRobot(); //now, you can call this method whenever you want
         
          
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-25
          • 2018-06-20
          • 2019-11-25
          • 1970-01-01
          • 2022-01-23
          • 2017-09-25
          • 2021-12-15
          • 2022-01-03
          相关资源
          最近更新 更多