Python电报漫游器:访问联系信息 [英] Python telegram bot: Access to contact information

查看:63
本文介绍了Python电报漫游器:访问联系信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码要求用户提供位置和联系方式:

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)

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

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

推荐答案

我不确定 django-telegrambot .但是,如果我没记错的话,您的 reply_markup python-telegram-bot 下的那个.按下按钮后,用户的电话号码将作为 MessageHandler Filters.contact 过滤器.

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))

这篇关于Python电报漫游器:访问联系信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆