Python 无法获取用户';s带键盘按钮的电话号码
我有一个简单的电报机器人,它有一个键盘按钮,当用户按下“开始”时就会出现。用户按下键盘按钮后,出现一个窗口,在该窗口中,用户按下“共享电话号码”按钮。我需要获取此电话号码,但响应中的“联系人”对象为空。我使用的模块-py-botapiPython 无法获取用户';s带键盘按钮的电话号码,python,api,telegram,telegram-bot,Python,Api,Telegram,Telegram Bot,我有一个简单的电报机器人,它有一个键盘按钮,当用户按下“开始”时就会出现。用户按下键盘按钮后,出现一个窗口,在该窗口中,用户按下“共享电话号码”按钮。我需要获取此电话号码,但响应中的“联系人”对象为空。我使用的模块-py-botapi import telebot from telebot import types bot = telebot.TeleBot(token) @bot.message_handler(commands=['start']) def register(messag
import telebot
from telebot import types
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def register(message):
keyboard = types.ReplyKeyboardMarkup(one_time_keyboard=True)
reg_button = types.KeyboardButton(text="Share your phone number", request_contact=True)
keyboard.add(reg_button)
response = bot.send_message(message.chat.id,
"You should share your phone number",
reply_markup=keyboard)
print(response.contact) # response.contact = None here
if __name__ == '__main__':
bot.polling(none_stop=True)
这是从用户那里获取电话号码的正确方法吗?如果没有,我怎样才能得到它 不,这是错的。您应该定义一个新的
处理程序
来处理联系人
dispatcher.add_handler(MessageHandler(Filters.contact, get_contact))
然后您应该定义您的get\u contact
功能:
def get_contact(bot, update):
chat_id = update.message.chat_id
phone_number = update.message.contact.phone_number
other stuffs...
您应该为联系人定义新的处理程序
@bot.message_handler(content_types=['contact'])
def contact_handler(message):
print(message.contact.phone_number)
对不起,是我的错。我在标签中添加了错误的模块。我使用的是pyTelegramBotAPI,而不是python电报机器人。