电报api,无法获取电话号码

时间:2017-09-20 06:49:35

标签: java telegram telegram-bot

在代码中我写了一些评论。 我需要获取用户的电话号码。 Api telegraph允许您使用keyboardButton.setText("Share your number>").setRequestContact(true);用户收到消息来执行此操作,并且只需单击一下即可发送联系人。之后,我尝试在控制台System.out.println(update.getMessage().getContact());中显示联系人,但我总是得到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("#############");

    }
 }
}

1 个答案:

答案 0 :(得分:0)

删除此行进行检查

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

共享联系人回复没有文字