I am working on Java Spring Boot project which includes simple Telegram Bot. My problem: I don't want to push credentials such as bot username and bot token to my remote repository. To achieve this, I simply put .env file into .gitignore, but without creds, Java CI can't build project. I get quite natural root exception: org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
Here is the code of my bot:
import application.carsharingapp.exception.NotificationSendingException;
import application.carsharingapp.service.notification.NotificationService;
import io.github.cdimascio.dotenv.Dotenv;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
@RequiredArgsConstructor
@Service
public class TelegramNotificationService extends TelegramLongPollingBot
implements NotificationService {
private static final Dotenv DOTENV = Dotenv.configure().ignoreIfMissing().load();
private static final String BOT_USERNAME = DOTENV.get("BOT_USERNAME");
private static final String BOT_TOKEN = DOTENV.get("BOT_TOKEN");
private static final String TARGET_CHAT_ID = DOTENV.get("TARGET_CHAT_ID");
@Override
public void sendNotification(String message) {
SendMessage sendMessage = new SendMessage();
sendMessage.setText(message);
sendMessage.setChatId(TARGET_CHAT_ID);
try {
execute(sendMessage);
} catch (TelegramApiException e) {
throw new NotificationSendingException("Can't send notification "
+ "to chat: " + sendMessage.getChatId(), e);
}
}
@Override
public void onUpdateReceived(Update update) {
}
@Override
public String getBotUsername() {
return BOT_USERNAME;
}
@Override
public String getBotToken() {
return BOT_TOKEN;
}
}
And here is code of my bot's config:
import application.carsharingapp.service.notification.impl.TelegramNotificationService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
@Configuration
public class TelegramBotConfig {
@Bean
public TelegramBotsApi telegramBotsApi(TelegramNotificationService telegramNotificationService)
throws TelegramApiException {
TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class);
api.registerBot(telegramNotificationService);
return api;
}
}
I would really like to hear some advice or tips on how to solve this problem and what are the possible solutions.
To solve this problem I tried to set some mock values for credentials, but that I got 404 Not found error