Python telegram bot:访问联系信息

mrb*_*rbf 2 python django telegram python-telegram-bot

以下代码请求用户的位置和联系:

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)
Run Code Online (Sandbox Code Playgroud)

我的问题是用户点击send_contact按钮后如何访问电话号码?
PS:我正在使用python-telegram-botdjango-telegrambot

jef*_*ffc 5

我不确定django-telegrambot.但如果我没错,你reply_markup就是那个人python-telegram-bot.按下按钮时,用户的电话号码将作为Contact对象发送到您的机器人.您可以MessageHandler使用Filters.contact过滤器捕获它.

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))
Run Code Online (Sandbox Code Playgroud)