我是新手,我已经在Eclipse中编写了TelegramBot的代码。它工作正常,但是我想知道如何在不启动Java应用程序的情况下使其持续工作。我想我应该为此使用服务器。谢谢

编辑:添加了Java代码

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

public class MyFirstBot  extends TelegramLongPollingBot{

    @Override
    public String getBotUsername() {
        // TODO Auto-generated method stub
        return "TheBot Version0.01";
    }

    @Override
    public void onUpdateReceived(Update update) {
        // TODO Auto-generated method stub
         SendMessage message = new SendMessage()
                    .setChatId(update.getMessage().getChatId());
         if(update.hasMessage() && update.getMessage().isCommand()){
             try{
                 switch(update.getMessage().getText()){ //reads command
                 case("/hello"):
                    message.setText("Hi there!");
                    sendMessage(message); //says hello
                    break;
                 case("/date"):
                    message.setText(LocalDateTime.now().toString());
                    sendMessage(message); //tells time and date
                    break;
                 default:
                    message.setText("Invalid Value");
                    sendMessage(message);} //in case no such command exists
             }
             catch(TelegramApiException e){
                 e.printStackTrace();
             }
         }
    }

    @Override
    public String getBotToken() {
        // TODO Auto-generated method stub
        return "xxx";
    }

}

最佳答案

我建议将您的应用程序导出到.jar文件中,然后使用任务计划程序运行它(Windows)或进行cronjob来运行它(Linux)。如果您拥有Amazon Web Services帐户,则还可以使用Lambda函数。如果需要,您可以购买一台像树莓派这样的小型计算机,然后在该计算机上运行它,就像它是“服务器”一样。

10-07 13:33
查看更多