skip to Main Content

I’m currently playing with telegram bot api via the telegram python package. So far I don’t have any problem creating custom keyboards with the following code:

bot.sendMessage(chat_id,
text=”Where are you now? ”
“/cancel to abort”,
reply_markup=ReplyKeyboardMarkup([[‘Home’, ‘Office’]], one_time_keyboard=True))

Basically the above will give me two button, “Home” and “Office”.

I’m now trying to capture user’s phone number and location, which should be doable based on the Telegram API Bot documentation at https://core.telegram.org/bots/api#keyboardbutton. However I’m really having a hard time figuring out how to get it to work.

Any ideas?

2

Answers


  1. here is an example:

    reply_markup = telegram.ReplyKeyboardMarkup([[telegram.KeyboardButton('Share contact', request_contact=True)]])
    
    bot.sendMessage(CHAT_ID, 'Example', reply_markup=reply_markup)
    

    Hope it helps!

    Login or Signup to reply.
  2. from telegram import (KeyboardButton)



    location_keyboard = KeyboardButton(text="send_location",  request_location=True)           #creating location button object
    contact_keyboard = KeyboardButton('Share contact', request_contact=True)  #creating contact button object
    custom_keyboard = [[ location_keyboard, contact_keyboard ]] #creating keyboard object
    reply_markup = ReplyKeyboardMarkup(custom_keyboard) 
    update.message.reply_text(
                    "Would you mind sharing your location and contact with me?", 
                    reply_markup=reply_markup)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search