简体   繁体   English

我可以从 Spring Boot 应用程序启动电报机器人吗?

[英]Can I launch a telegram bot from a Spring Boot application?

I try to create a bot from here https://github.com/rubenlagus/TelegramBots我尝试从这里创建一个机器人https://github.com/rubenlagus/TelegramBots

It works as a simple application, but when I try to add Spring Boot it doesn't work.它作为一个简单的应用程序工作,但是当我尝试添加 Spring Boot 时它不起作用。 I suppose it is because Spring Boot launches Tomcat and telegram bot tries to send/recieve some http.我想这是因为 Spring Boot 启动了 Tomcat 并且电报机器人尝试发送/接收一些 http。

I don't get any errors (bot launches as @Component bean).我没有收到任何错误(机器人作为 @Component bean 启动)。

Is it even possible to connect this kind of bot and a Spring Boot app or at least a web application?甚至可以将这种机器人与 Spring Boot 应用程序或至少是 Web 应用程序连接起来吗?

You can try to use telegrambots-spring-boot-starter from the same library.您可以尝试使用同一个库中的telegrambots-spring-boot-starter

Your main configuration should looks like:您的主要配置应如下所示:

@SpringBootApplication
public class YourApplicationMainClass {

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

        SpringApplication.run(YourApplicationMainClass.class, args);
    }
}

And class of your bot:和你的机器人类:

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

A bit more information you can find here https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-spring-boot-starter您可以在此处找到更多信息https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-spring-boot-starter

As @Bobby said, you can try the正如@Bobby 所说,您可以尝试

telegrambots-spring-boot-starter project telegrambots-spring-boot-starter 项目

And also you can add the new telegrambots-extensions dependency which make you able to manage command bot.您还可以添加新的 Telegrambots-extensions 依赖项,使您能够管理命令机器人。

So the code will be所以代码将是

@Component
public class Foo extends TelegramLongPollingCommandBot {

        @Override
        public void processNonCommandUpdate(Update update) {

Also you can manage the command in this way.您也可以通过这种方式管理命令。

@Component
public class FooCommand extends DefaultBotCommand {

      @Override
      public void execute(AbsSender absSender, User user, Chat chat, Integer messageId, String[] arguments) {

You can register your TelegramLongPollingCommandBot whitin the SpringBoot class main, as below:您可以在 SpringBoot 类 main 中注册您的 TelegramLongPollingCommandBot,如下所示:

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

[ https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-extensions] [ https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-extensions]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从start.spring.io启动Spring Boot 2x应用程序 - Can't launch Spring Boot 2x application from start.spring.io 您可以在多个线程上启动Spring Boot Rest应用程序吗? - Can you launch a Spring boot rest application on multiple threads? 如何在没有嵌入式tomcat的情况下启动Spring Boot应用程序? - How do I launch a Spring Boot application without the embedded tomcat? 尝试在Spring Boot应用程序连接到Telegram Bot时出错 - Error when try to connect to telegram bot at spring boot app 我可以从另一个 spring boot 应用程序中编译并运行 spring boot jar 吗? - Can I compile and run a spring boot jar from inside another spring boot application? 可以从Eclipse(STS)启动Spring Boot,但不能从CLI启动 - Can launch Spring Boot from Eclipse (STS) but not from CLI 我无法保存实体(Spring Boot 应用程序) - I can not save entity (Spring Boot Application) 无法将Spring Boot应用程序启动到Linux服务器中 - Failed to launch Spring boot application into Linux server 如何在运行时使用 spring 引导应用程序中的 java 代码对 application.yaml 进行读写操作? - How can I write and read to and from a application.yaml at runtime using java code in a spring boot application? 我可以在Spring启动应用程序中使用Spring集成模块吗? - Can I use Spring integration modules in my Spring boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM