atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.35k stars 217 forks source link

undefined method `from' for nil:NilClass (NoMethodError) #255

Closed syxanash closed 2 years ago

syxanash commented 2 years ago

Hey I'm trying to add my bot to a group (even with bot group enabled in botfather) but I get this issue:

Traceback (most recent call last):
    8: from test.rb:5:in `<main>'
    7: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/client.rb:8:in `run'
    6: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/client.rb:18:in `run'
    5: from test.rb:6:in `block in <main>'
    4: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/client.rb:25:in `listen'
    3: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/client.rb:33:in `fetch_updates'
    2: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/client.rb:33:in `each'
    1: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/client.rb:37:in `block in fetch_updates'
/home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.8.6.1/lib/telegram/bot/client.rb:51:in `log_incoming_message': undefined method `from' for nil:NilClass (NoMethodError)

I remember it used to work in the past but for some reasons it's not anymore, I'm using the code example you mentioned in the README.md:

require 'telegram/bot'

token = 'YOUR_TELEGRAM_BOT_API_TOKEN'

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    case message.text
    when '/start'
      bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}")
    when '/stop'
      bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
    end
  end
end
syxanash commented 2 years ago

this is the error when I update the library to the latest version instead:

Traceback (most recent call last):
    8: from test.rb:5:in `<main>'
    7: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.19.2/lib/telegram/bot/client.rb:10:in `run'
    6: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.19.2/lib/telegram/bot/client.rb:20:in `run'
    5: from test.rb:6:in `block in <main>'
    4: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.19.2/lib/telegram/bot/client.rb:27:in `listen'
    3: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.19.2/lib/telegram/bot/client.rb:35:in `fetch_updates'
    2: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.19.2/lib/telegram/bot/client.rb:35:in `each'
    1: from /home/pi/.rvm/gems/ruby-2.5.0/gems/telegram-bot-ruby-0.19.2/lib/telegram/bot/client.rb:36:in `block in fetch_updates'
test.rb:7:in `block (2 levels) in <main>': undefined method `text' for #<Telegram::Bot::Types::ChatMemberUpdated:0x0285b3e8> (NoMethodError)
atipugin commented 2 years ago

Hey @syxanash,

You need to check message type first, see https://github.com/atipugin/telegram-bot-ruby/issues/226#issuecomment-797296565

syxanash commented 2 years ago

just noticed, will close as a duplicate issue then, thank you for your help!