问题
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
回答1:
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))
来源:https://stackoverflow.com/questions/46465706/python-telegram-bot-access-to-contact-information