Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以从Spring启动应用程序启动telegram bot吗?_Java_Spring Boot_Telegram Bot - Fatal编程技术网

Java 我可以从Spring启动应用程序启动telegram bot吗?

Java 我可以从Spring启动应用程序启动telegram bot吗?,java,spring-boot,telegram-bot,Java,Spring Boot,Telegram Bot,我尝试从这里创建一个机器人 它作为一个简单的应用程序工作,但当我尝试添加Spring Boot时,它不起作用。我想这是因为SpringBoot启动了Tomcat,电报机器人试图发送/接收一些http 我没有收到任何错误(bot作为@Component bean启动) 甚至可以将这种机器人程序与Spring Boot应用程序或至少是web应用程序连接起来吗?您可以尝试从同一个库中使用telegrambots Spring Boot starter 您的主要配置应该如下所示: @SpringBoo

我尝试从这里创建一个机器人

它作为一个简单的应用程序工作,但当我尝试添加Spring Boot时,它不起作用。我想这是因为SpringBoot启动了Tomcat,电报机器人试图发送/接收一些http

我没有收到任何错误(bot作为@Component bean启动)


甚至可以将这种机器人程序与Spring Boot应用程序或至少是web应用程序连接起来吗?

您可以尝试从同一个库中使用
telegrambots Spring Boot starter

您的主要配置应该如下所示:

@SpringBootApplication
public class YourApplicationMainClass {

    public static void main(String[] args) {
        ApiContextInitializer.init();

        SpringApplication.run(YourApplicationMainClass.class, args);
    }
}
和您的机器人的等级:

// Standard Spring component annotation
@Component
public class YourBotName extends TelegramLongPollingBot {
    //Bot body.
}

你可以在这里找到更多的信息,

正如@Bobby所说,你可以试试

telegrambots spring启动程序项目

此外,您还可以添加新的电报机器人扩展依赖项,使您能够管理命令机器人

因此,代码将是

@Component
public class Foo extends TelegramLongPollingCommandBot {

        @Override
        public void processNonCommandUpdate(Update update) {
也可以用这种方式管理命令

@Component
public class FooCommand extends DefaultBotCommand {

      @Override
      public void execute(AbsSender absSender, User user, Chat chat, Integer messageId, String[] arguments) {
您可以在SpringBoot类主目录中注册TelegramLongPollingCommandBot,如下所示:


[

在此结构中在何处注册bot?
 @SpringBootApplication 
 public class TelegramBotConfiguration { 
      public static void main(String[] args)      {       
         ApiContextInitializer.init();
         TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
         try {
           session = telegramBotsApi.registerBot(new Foo());

         } catch (TelegramApiException e) {
             log.error(e);
         }
         SpringApplication.run(TelegramBotConfiguration.class, args);
      }
 }