[英]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.