Tryserg
Tryserg

Reputation: 23

Telegram api, can't get phone number

In the code I write some comments. I need to get the phone number of the user. Api telegraph allows you to do this using keyboardButton.setText("Share your number>").setRequestContact(true); user receives a message and he sends a contact with one click. After that I try to display contact in the console System.out.println(update.getMessage().getContact());, but I always get null

public void onUpdateReceived(Update update) {

if (update.hasMessage() && update.getMessage().hasText()) {
    long chat_id = update.getMessage().getChatId();

    if (update.getMessage().getText().equals("/start")) {

        SendMessage sendMessage = new SendMessage()
                .setChatId(chat_id)
                .setText("You send /start");

        // create keyboard
        ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
        sendMessage.setReplyMarkup(replyKeyboardMarkup);
        replyKeyboardMarkup.setSelective(true);
        replyKeyboardMarkup.setResizeKeyboard(true);
        replyKeyboardMarkup.setOneTimeKeyboard(true);

        // new list
        List<KeyboardRow> keyboard = new ArrayList<>();

        // first keyboard line
        KeyboardRow keyboardFirstRow = new KeyboardRow();
        KeyboardButton keyboardButton = new KeyboardButton();
        keyboardButton.setText("Share your number >").setRequestContact(true);
        keyboardFirstRow.add(keyboardButton);

        // add array to list
        keyboard.add(keyboardFirstRow);

        // add list to our keyboard
        replyKeyboardMarkup.setKeyboard(keyboard);

        try {
            sendMessage(sendMessage);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }

        System.out.println("#############");
        System.out.println(update.getMessage().getContact());
        System.out.println("#############");

    }
 }
}

Upvotes: 2

Views: 3665

Answers (2)

LNT
LNT

Reputation: 916

Remove this line for check

if(update.hasMessage() && update.getMessage().hasText())

share contact reply don't have text

Upvotes: 0

DarwinFernandez
DarwinFernandez

Reputation: 364

The object update.getMessage().getContact() is not null when the user push the button "Share your number >"

try change your code like this:

if (update.getMessage().getText().equals("/start")) {

    SendMessage sendMessage = new SendMessage()
            .setChatId(chat_id)
            .setText("You send /start");

    // create keyboard
    ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
    sendMessage.setReplyMarkup(replyKeyboardMarkup);
    replyKeyboardMarkup.setSelective(true);
    replyKeyboardMarkup.setResizeKeyboard(true);
    replyKeyboardMarkup.setOneTimeKeyboard(true);

    // new list
    List<KeyboardRow> keyboard = new ArrayList<>();

    // first keyboard line
    KeyboardRow keyboardFirstRow = new KeyboardRow();
    KeyboardButton keyboardButton = new KeyboardButton();
    keyboardButton.setText("Share your number >").setRequestContact(true);
    keyboardFirstRow.add(keyboardButton);

    // add array to list
    keyboard.add(keyboardFirstRow);

    // add list to our keyboard
    replyKeyboardMarkup.setKeyboard(keyboard);

    try {
        sendMessage(sendMessage);
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }


}else{
    System.out.println("#############");
    System.out.println(update.getMessage().getContact());
    System.out.println("#############");

}

Upvotes: 3

Related Questions