如何用Ruby Telegram Bot访问Contact对象的phone_number字段?
phone_number from a Contact object in the Telegram Bot Ruby library? I'm trying to implement a Telegram bot that requests a user's phone number and performs operations on it. Here's my Ruby code:
Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| case message when Telegram::Bot::Types::CallbackQuery when Telegram::Bot::Types::Message case message.text when '/help' bot.api.send_message( chat_id: message.chat.id, text: 'Display help.' ) end if message.contact.phone_number == "1111111" #undefined method bot.api.send_message( chat_id: message.chat.id, text: 'Number received.' ) end else bot.api.send_message( chat_id: message.chat.id, text: 'Unknown command.' ) end end endI can get the Contact object, but when accessing the
phone_numberfield, I get an "undefined method" error. How do I correctly access this field using the Telegram Bot Ruby wrapper library?
The error you're seeing is almost certainly because not all Message objects have a contact attribute—only messages where the user explicitly sends a contact will have this value set. For regular text messages (like /help), message.contact is nil, so calling .phone_number on it throws the undefined method 'phone_number' for nil:NilClass error.
Here's how to fix this:
1. Check if the contact exists first
Before accessing phone_number, verify that message.contact is not nil. You can do this in a few ways:
Option 1: Use a presence check
Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| case message when Telegram::Bot::Types::CallbackQuery # Handle callback queries here if needed when Telegram::Bot::Types::Message case message.text when '/help' bot.api.send_message( chat_id: message.chat.id, text: 'Display help.' ) end # Only process if a contact was sent if message.contact.present? if message.contact.phone_number == "1111111" bot.api.send_message( chat_id: message.chat.id, text: 'Number received.' ) else bot.api.send_message( chat_id: message.chat.id, text: "Got your number: #{message.contact.phone_number}" ) end end else bot.api.send_message( chat_id: message.chat.id, text: 'Unknown command.' ) end end end
Option 2: Use Ruby's safe navigation operator (&.)
If you're using Ruby 2.3 or newer, the safe navigation operator lets you safely access nested attributes without checking for nil first:
if message.contact&.phone_number == "1111111" bot.api.send_message( chat_id: message.chat.id, text: 'Number received.' ) end
This will return nil (instead of throwing an error) if message.contact is nil, so the condition will simply fail silently.
2. Verify the Contact object's attributes
For the telegram-bot-ruby library, the Telegram::Bot::Types::Contact class does have a phone_number attribute that maps directly to the Telegram API's phone_number field. As long as you're working with a valid Contact instance (not nil), this attribute will be available.
The key fix here is ensuring you only attempt to access phone_number when a contact has actually been sent by the user.
内容的提问来源于stack exchange,提问作者Carrein

