2017-02-07 8 views
1

ユーザーが「開始」を押したときに表示されるKeyboardButtonを持つ簡単な電報ボットを持っています。ユーザがKeyboardButtonを押すと、ウィンドウが現れ、そのウィンドウ内にユーザは「電話番号の共有」ボタンを押す。この電話番号を取得する必要がありますが、応答の「連絡先」オブジェクトは空です。私が使用するモジュール - pyTelegramBotApi。KeyboardButtonでユーザーの電話番号を取得できない

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) 

これは、ユーザーから電話番号を取得する正しい方法ですか?いいえ、どうすれば入手できますか?

答えて

0

いいえ、間違っています。 contactを処理するには、新しいhandlerを定義する必要があります。

def get_contact(bot, update): 
    chat_id = update.message.chat_id 
    phone_number = update.message.contact.phone_number 
    other stuffs... 
+0

申し訳ありませんが、私のせい:

dispatcher.add_handler(MessageHandler(Filters.contact, get_contact)) 

あなたはあなたのget_contact関数を定義する必要があります。私は間違ったモジュールをタグに追加しました。 python-telegram-botではなく、pyTelegramBotAPIを使用しています。 –

関連する問題