Java Code Examples for org.telegram.telegrambots.meta.api.methods.send.SendMessage#setChatId()

The following examples show how to use org.telegram.telegrambots.meta.api.methods.send.SendMessage#setChatId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnsubCommand.java    From telegram-notifications-plugin with MIT License 7 votes vote down vote up
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
    Subscribers subscribers = Subscribers.getInstance();
    String ans;

    Long id = chat.getId();

    boolean isSubscribed = subscribers.isSubscribed(id);

    if (isSubscribed) {
        subscribers.unsubscribe(id);
        ans = botStrings.get("message.unsub.success");
    } else {
        ans = botStrings.get("message.unsub.alreadyunsub");
    }

    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(ans);

    try {
        absSender.execute(answer);
    } catch (TelegramApiException e) {
        LOGGER.error(LOG_TAG, e);
    }
}
 
Example 2
Source File: ExampleBot.java    From telegram-spring-boot-starter-example with MIT License 6 votes vote down vote up
@Override
public void onUpdateReceived(Update update) {
	if (update.hasMessage()) {
		Message message = update.getMessage();
		SendMessage response = new SendMessage();
		Long chatId = message.getChatId();
		response.setChatId(chatId);
		String text = message.getText();
		response.setText(text);
		try {
			execute(response);
			logger.info("Sent message \"{}\" to {}", text, chatId);
		} catch (TelegramApiException e) {
			logger.error("Failed to send message \"{}\" to {} due to error: {}", text, chatId, e.getMessage());
		}
	}
}
 
Example 3
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 6 votes vote down vote up
private static SendMessage onDeleteAlertCommand(Message message, String language) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);

    sendMessage.setChatId(message.getChatId());

    ReplyKeyboardMarkup replyKeyboardMarkup = getAlertsListKeyboard(message.getFrom().getId(), language);
    if (replyKeyboardMarkup != null) {
        sendMessage.setReplyMarkup(replyKeyboardMarkup);
        sendMessage.setText(LocalisationService.getString("chooseNewAlertCity", language));
        DatabaseManager.getInstance().insertWeatherState(message.getFrom().getId(), message.getChatId(), ALERTDELETE);
    } else {
        sendMessage.setReplyMarkup(getAlertsKeyboard(language));
        sendMessage.setText(LocalisationService.getString("noAlertList", language));
    }

    sendMessage.setReplyToMessageId(message.getMessageId());
    return sendMessage;
}
 
Example 4
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private void sendHideKeyboard(Integer userId, Long chatId, Integer messageId) throws TelegramApiException {
    SendMessage sendMessage = new SendMessage();
    sendMessage.setChatId(chatId.toString());
    sendMessage.enableMarkdown(true);
    sendMessage.setReplyToMessageId(messageId);
    sendMessage.setText(Emoji.WAVING_HAND_SIGN.toString());

    ReplyKeyboardRemove replyKeyboardRemove = new ReplyKeyboardRemove();
    replyKeyboardRemove.setSelective(true);
    sendMessage.setReplyMarkup(replyKeyboardRemove);

    execute(sendMessage);
    DatabaseManager.getInstance().insertWeatherState(userId, chatId, STARTSTATE);
}
 
Example 5
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage onNewCurrentWeatherCommand(Long chatId, Integer userId, Integer messageId, String language) {
    ForceReplyKeyboard forceReplyKeyboard = getForceReply();

    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);
    sendMessage.setChatId(chatId.toString());
    sendMessage.setReplyToMessageId(messageId);
    sendMessage.setReplyMarkup(forceReplyKeyboard);
    sendMessage.setText(LocalisationService.getString("onWeatherNewCommand", language));

    DatabaseManager.getInstance().insertWeatherState(userId, chatId, CURRENTNEWWEATHER);
    return sendMessage;
}
 
Example 6
Source File: WebHookExampleHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BotApiMethod onWebhookUpdateReceived(Update update) {
    if (update.hasMessage() && update.getMessage().hasText()) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setChatId(update.getMessage().getChatId().toString());
        sendMessage.setText("Well, all information looks like noise until you break the code.");
        return sendMessage;
    }
    return null;
}
 
Example 7
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage onListAlertCommand(Message message, String language) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);

    sendMessage.setChatId(message.getChatId());
    sendMessage.setReplyToMessageId(message.getMessageId());
    sendMessage.setReplyMarkup(getAlertsKeyboard(language));
    sendMessage.setText(getAlertListMessage(message.getFrom().getId(), language));
    sendMessage.setReplyToMessageId(message.getMessageId());

    return sendMessage;
}
 
Example 8
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage sendChooseOptionMessage(Long chatId, Integer messageId,
                                            ReplyKeyboard replyKeyboard, String language) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);
    sendMessage.setChatId(chatId.toString());
    sendMessage.setReplyToMessageId(messageId);
    sendMessage.setReplyMarkup(replyKeyboard);
    sendMessage.setText(LocalisationService.getString("chooseOption", language));

    return sendMessage;
}
 
Example 9
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage onSettingsChoosen(Message message, String language) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);

    ReplyKeyboardMarkup replyKeyboardMarkup = getSettingsKeyboard(language);
    sendMessage.setReplyMarkup(replyKeyboardMarkup);
    sendMessage.setReplyToMessageId(message.getMessageId());
    sendMessage.setChatId(message.getChatId());
    sendMessage.setText(getSettingsMessage(language));

    DatabaseManager.getInstance().insertWeatherState(message.getFrom().getId(), message.getChatId(), SETTINGS);
    return sendMessage;
}
 
Example 10
Source File: FilesHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private void onUploadCommand(Message message, String language) throws InvalidObjectException, TelegramApiException {
    DatabaseManager.getInstance().addUserForFile(message.getFrom().getId(), INITIAL_UPLOAD_STATUS);
    SendMessage sendMessageRequest = new SendMessage();
    sendMessageRequest.setText(LocalisationService.getString("sendFileToUpload", language));
    sendMessageRequest.setChatId(message.getChatId());
    execute(sendMessageRequest);
}
 
Example 11
Source File: FilesHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private void onCancelCommand(Message message, String language) throws InvalidObjectException, TelegramApiException {
    DatabaseManager.getInstance().deleteUserForFile(message.getFrom().getId());
    SendMessage sendMessageRequest = new SendMessage();
    sendMessageRequest.setText(LocalisationService.getString("processFinished", language));
    sendMessageRequest.setChatId(message.getChatId());
    execute(sendMessageRequest);
}
 
Example 12
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage onLanguageChosen(Integer userId, Long chatId, Integer messageId, String language) {
    String languageCode = LocalisationService.getLanguageCodeByName(language);
    DatabaseManager.getInstance().putUserWeatherLanguageOption(userId, languageCode);

    SendMessage sendMessageRequest = new SendMessage();
    sendMessageRequest.enableMarkdown(true);
    sendMessageRequest.setChatId(chatId.toString());
    sendMessageRequest.setText(LocalisationService.getString("languageUpdated", languageCode));
    sendMessageRequest.setReplyToMessageId(messageId);
    sendMessageRequest.setReplyMarkup(getMainMenuKeyboard(languageCode));

    DatabaseManager.getInstance().insertWeatherState(userId, chatId, MAINMENU);
    return sendMessageRequest;
}
 
Example 13
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage onAlertDeleteBackOptionSelected(Message message, String language) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);
    sendMessage.setReplyToMessageId(message.getMessageId());
    sendMessage.setChatId(message.getChatId());
    sendMessage.setReplyMarkup(getAlertsKeyboard(language));
    sendMessage.setText(LocalisationService.getString("alertsMenuMessage", language));

    DatabaseManager.getInstance().insertWeatherState(message.getFrom().getId(), message.getChatId(), ALERT);
    return sendMessage;
}
 
Example 14
Source File: HelloCommand.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) {

    if (!DatabaseManager.getInstance().getUserStateForCommandsBot(user.getId())) {
        return;
    }

    String userName = chat.getUserName();
    if (userName == null || userName.isEmpty()) {
        userName = user.getFirstName() + " " + user.getLastName();
    }

    StringBuilder messageTextBuilder = new StringBuilder("Hello ").append(userName);
    if (arguments != null && arguments.length > 0) {
        messageTextBuilder.append("\n");
        messageTextBuilder.append("Thank you so much for your kind words:\n");
        messageTextBuilder.append(String.join(" ", arguments));
    }

    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(messageTextBuilder.toString());

    try {
        absSender.execute(answer);
    } catch (TelegramApiException e) {
        BotLogger.error(LOGTAG, e);
    }
}
 
Example 15
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage onForecastWeatherReceived(Long chatId, Integer userId, Integer messageId, String text, String language) {
    String unitsSystem = DatabaseManager.getInstance().getUserWeatherOptions(userId)[1];
    String weather = WeatherService.getInstance().fetchWeatherForecast(text, userId, language, unitsSystem);
    SendMessage sendMessageRequest = new SendMessage();
    sendMessageRequest.enableMarkdown(true);
    sendMessageRequest.setReplyMarkup(getMainMenuKeyboard(language));
    sendMessageRequest.setReplyToMessageId(messageId);
    sendMessageRequest.setText(weather);
    sendMessageRequest.setChatId(chatId.toString());

    DatabaseManager.getInstance().insertWeatherState(userId, chatId, MAINMENU);
    return sendMessageRequest;
}
 
Example 16
Source File: SilentSender.java    From TelegramBots with MIT License 5 votes vote down vote up
public Optional<Message> forceReply(String message, long id) {
  SendMessage msg = new SendMessage();
  msg.setText(message);
  msg.setChatId(id);
  msg.setReplyMarkup(new ForceReplyKeyboard());

  return execute(msg);
}
 
Example 17
Source File: TelegramBot.java    From telegram-notifications-plugin with MIT License 5 votes vote down vote up
public void sendMessage(Long chatId, String message) {
    final SendMessage sendMessageRequest = new SendMessage();

    sendMessageRequest.setChatId(chatId.toString());
    sendMessageRequest.setText(message);
    sendMessageRequest.enableMarkdown(true);

    try {
        execute(sendMessageRequest);
    } catch (TelegramApiException e) {
        LOG.log(Level.SEVERE, String.format(
                "TelegramBot: Error while sending message: %s%n%s", chatId, message), e);
    }
}
 
Example 18
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage sendHelpMessage(Long chatId, Integer messageId, ReplyKeyboardMarkup replyKeyboardMarkup, String language) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);
    sendMessage.setChatId(chatId);
    sendMessage.setReplyToMessageId(messageId);
    if (replyKeyboardMarkup != null) {
        sendMessage.setReplyMarkup(replyKeyboardMarkup);
    }
    sendMessage.setText(getHelpMessage(language));
    return sendMessage;
}
 
Example 19
Source File: WeatherHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private static SendMessage sendRateMessage(Long chatId, Integer messageId, ReplyKeyboardMarkup replyKeyboardMarkup, String language) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);
    sendMessage.setChatId(chatId);
    sendMessage.setReplyToMessageId(messageId);
    if (replyKeyboardMarkup != null) {
        sendMessage.setReplyMarkup(replyKeyboardMarkup);
    }
    sendMessage.setText(LocalisationService.getString("rateMeMessage", language));

    return sendMessage;
}
 
Example 20
Source File: FilesHandlers.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
private void onStartWithParameters(Message message, String language, String part) throws InvalidObjectException, TelegramApiException {
    if (DatabaseManager.getInstance().doesFileExists(part.trim())) {
        SendDocument sendDocumentRequest = new SendDocument();
        sendDocumentRequest.setDocument(part.trim());
        sendDocumentRequest.setChatId(message.getChatId());
        execute(sendDocumentRequest);
    } else {
        SendMessage sendMessageRequest = new SendMessage();
        sendMessageRequest.setText(LocalisationService.getString("wrongFileId", language));
        sendMessageRequest.setChatId(message.getChatId());
        execute(sendMessageRequest);
    }
}