Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python telegram bot: Access to contact information

Following code requests location and contact from user:

def contact(bot, update):
    dp = DjangoTelegramBot.dispatcher

    con_keyboard = KeyboardButton(text="send_contact", request_contact=True)
    loc_keyboard = KeyboardButton(text="send_location", request_location=True)
    custom_keyboard = [[ con_keyboard ,loc_keyboard]]
    reply_markup = ReplyKeyboardMarkup(custom_keyboard)

    bot.sendMessage(update.message.chat_id, 
              text="Would you mind sharing your location and contact with me", 
              reply_markup=reply_markup)

My question is how can I access to phone number after user clicks on send_contact button?
PS: I am using python-telegram-bot and django-telegrambot

like image 677
mrbf Avatar asked Dec 14 '25 17:12

mrbf


1 Answers

I am not sure with django-telegrambot. But if I'm not wrong your reply_markup is the one under python-telegram-bot. When the button is pressed, the user's phone number will be sent to your bot as a Contact object. You can catch it with a MessageHandler and a Filters.contact filter.

from telegram.ext import MessageHander, Filters

def contact_callback(bot, update):
    contact = update.effective_message.contact
    phone = contact.phone_number

dispatcher.add_handler(MessageHandler(Filters.contact, contact_callback))
like image 53
jeffffc Avatar answered Dec 16 '25 18:12

jeffffc