You must implement a subscription to the daily weather forecast. There is no way to "capture" the parameters for the current location with the "/ subscribe" command.

Here is a piece of code:

public class Bot extends TelegramLongPollingBot { public void onUpdateReceived(Update update) { if (update.hasMessage()) { String command = update.getMessage().getText(); Message message = update.getMessage(); long chat_id = update.getMessage().getChatId(); Float latitude = message.getLocation().getLatitude(); Float longitude = message.getLocation().getLongitude(); if (message.hasLocation()) { SendMessage sendMessage = new SendMessage(); String weatherResponse = ""; //текущая погода try { weatherResponse = Weather.getInstance().currentWeather(latitude, longitude); } catch (IOException e) { e.printStackTrace(); } sendMessage.setText(weatherResponse); sendMessage.setChatId(chat_id); try { execute(sendMessage); } catch (TelegramApiException e) { e.printStackTrace(); } //прогноз погоды на ближайшие сутки try { weatherResponse = Weather.getInstance().forecastWeather(latitude, longitude); } catch (IOException e) { e.printStackTrace(); } sendMessage.setText(weatherResponse).setChatId(chat_id); try { execute(sendMessage); } catch (TelegramApiException e) { e.printStackTrace(); } }else if (command.equals("/subscribe")) { // подписка на прогноз погоды Database database = new Database(); try { database.WriteDB(message.getChatId(), message.getChat().getUserName(), message.getChat().getFirstName(), message.getChat().getLastName(), message.getLocation().getLatitude(), //здесь значение параметра равно NULL message.getLocation().getLongitude()); //здесь значение параметра равно NULL } catch (SQLException e) { e.printStackTrace(); } SendMessage sendMsg = new SendMessage(); sendMsg.setChatId(chat_id).setText("Thanks for subscribe!!!"); try { execute(sendMsg); } catch (TelegramApiException e) { e.printStackTrace(); } try { execute(sendMsg); } catch (TelegramApiException e) { e.printStackTrace(); } } else if (command.equals("/unsubscribe")) { // отписка от прогноза погоды } } }.................... 

I can not figure out how to implement the connection between sending the current location and the subscription command ("/ subscribe").

  • Not very clear what the problem is. Do you want the weather command for the given coordinates to come after the command to the user once in a certain period of time? If so, then I do not see a problem, create some class-service, which in a separate thread performs what is needed. For example, it retrieves from the database a list of users with coordinates for which the last mailing was carried out more than a specified period of time, sending to everyone, updating the time of sending. Periodicity can be set using any library, of which there are quite a few, take at least Spring with @Scheduled - iksuy
  • In principle, the weather distribution algorithm is clear. I cannot write current location data to the database. When the "/ subscribe" command, latitude and longitude are empty. - Johnny_WAS
  • And you make a request for geolocation data? In KeyboardButton you need to set requestLocation to true . - iksuy
  • Not. I send geolocation using standard Telegram methods, and check in the if condition (message.hasLocation ()). - Johnny_WAS
  • Most likely you need to make a request, because without the knowledge of the user, the client will not transmit his geolocation data. - iksuy

0