Python 电报机器人:获取联系信息
Python telegram bot: Access to contact information
以下代码请求用户的位置和联系方式:
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 按钮后访问 phone 号码?
PS:我正在使用 python-telegram-bot and django-telegrambot
我不确定 django-telegrambot
。但如果我没记错的话,你的 reply_markup
就是 python-telegram-bot
下面的那个。按下按钮后,用户的 phone 号码将作为 Contact
object. You can catch it with a MessageHandler
and a 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))
以下代码请求用户的位置和联系方式:
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 按钮后访问 phone 号码?
PS:我正在使用 python-telegram-bot and django-telegrambot
我不确定 django-telegrambot
。但如果我没记错的话,你的 reply_markup
就是 python-telegram-bot
下面的那个。按下按钮后,用户的 phone 号码将作为 Contact
object. You can catch it with a MessageHandler
and a 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))