Не работает код, пошарил в отладке, бот почему-то null
, помогите чем можете.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.testbot</groupId>
<artifactId>SpringTestBot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringTestBot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.9.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package org.testbot.SpringTestBot.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand;
import org.telegram.telegrambots.meta.api.objects.commands.scope.BotCommandScopeDefault;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.testbot.SpringTestBot.config.BotConfig;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class TelegramBot extends TelegramLongPollingBot {
final BotConfig config;
public TelegramBot(BotConfig config) {
this.config = config;
List<BotCommand> listofCommands = new ArrayList<>();
listofCommands.add(new BotCommand("/start", "get a welcome message"));
}
@Override
public String getBotUsername() {
return config.getBotName();
}
public String getBotToken() { return config.getToken();}
@Override
// обработка соо от пользователя и ответ на них
public void onUpdateReceived(Update update) {
if (update.hasMessage() && update.getMessage().hasText()) {
String msgText = update.getMessage().getText();
long chatId = update.getMessage().getChatId();
switch (msgText) {
case "/start":
startCommandReceived(chatId, update.getMessage().getChat().getFirstName());
break;
case "/help":
helpCommandReceived(chatId);
break;
default:
sendMsg(chatId,"Неверное сообщение ");
}
}
}
private void startCommandReceived(Long chatId,String name) {
String answer = "Привет, " + name + ", я твой бот";
sendMsg(chatId,answer);
}
private void helpCommandReceived(Long chatId) {
String answer = " Я помогаю тебе";
sendMsg(chatId,answer);
}
private void sendMsg(long chatId,String textToSend) {
SendMessage msg = new SendMessage();
msg.setChatId(String.valueOf(chatId));
msg.setText(textToSend);
try {
execute(msg);
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}
}
package org.testbot.SpringTestBot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringTestBotApplication {
public static void main(String[] args) {
SpringApplication.run(SpringTestBotApplication.class, args);
}
}
package org.testbot.SpringTestBot.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
@Data
@PropertySource("application.properties")
public class BotConfig {
@Value("${bot.name}")
String botName;
@Value("${bot.token}")
String token;
}
Начал что-то исправлять, ещё хуже всё сломал
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-01-23T23:11:15.323+03:00 ERROR 10340 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
registerBot
. И укажите хотя бы какую библиотеку используете.null
-ового бота?