Python telegram bot: Access to contact information

邮差的信 提交于 2021-02-08 05:08:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!