Я используя библиотеку org.telegram.telegrambots. Написал long-polling бота и теперь хочу получить отправленную юзером геолокацию, но бот не видит update'а при отправке сообщения. При этом все остальные функции работают.
Код бота:
@Log4j
@Controller
public class TelegramBotController extends TelegramLongPollingBot {
@Value("${bot.name}")
private String botName;
@Autowired
private ChatStateService chatStateService;
private final BotUtilServices botUtilServices;
private final TelegramBotCommandsHandler commandsHandler;
private final TelegramBotStatesHandler statesHandler;
public TelegramBotController(@Value("${bot.token}")String botToken, BotUtilServices botUtilServices){
super(botToken);
this.botUtilServices = botUtilServices;
this.commandsHandler = new TelegramBotCommandsHandler(this);
this.statesHandler = new TelegramBotStatesHandler(this);
}
@Override
public void onUpdateReceived(Update update) {
if(!update.hasMessage() || !update.getMessage().hasText())
return;
var message = update.getMessage().getText().trim();
var chatId = update.getMessage().getChatId();
if (message.startsWith("/")) {
String commandIdentifier = message.split(" ")[0].toLowerCase();
commandsHandler.retrieveCommand(commandIdentifier).execute(update);
} else {
statesHandler.retrieveAction(chatStateService.getState(chatId)).execute(update);
}
}
public void sendMessage(Long chatId,String text){
String chatIdStr = chatId.toString();
SendMessage sendMessage = new SendMessage(chatIdStr,text);
try{
log.debug("Sending message: " + text);
execute(sendMessage);
}
catch(Exception e){
log.error(e.getMessage());
}
}
@Override
public String getBotUsername() {
return botName;
}
public ChatStateService getChatStateService() { return this.chatStateService; }
public BotUtilServices getBotUtilServices() { return botUtilServices; }
}
Команда, где я пытаюсь получить геолокацию:
public class GetLocationAction implements Action {
private final BotUtilServices utils;
private final ChatStateService chatStateService;
private final TelegramBotController telegramBotController;
public GetLocationAction(BotUtilServices utils, ChatStateService chatStateService, TelegramBotController telegramBotController){
this.utils = utils;
this.chatStateService = chatStateService;
this.telegramBotController = telegramBotController;
}
@Override
public void execute(Update update) {
Location location = update.getMessage().getLocation();
String weather;
if(location==null)
weather = utils.getWeather(0,0);
else
weather = utils.getWeather(location.getLongitude(), location.getLatitude());
Long chatId = update.getMessage().getChatId();
telegramBotController.sendMessage(chatId,weather);
chatStateService.setState(chatId,BotState.IDLE);
}
}
Пример переписки с ботом: