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 218 forks source link

Undefined method `text' for #<Telegram::Bot::Types::ChatMemberUpdated #245

Closed dvarrui closed 1 year ago

dvarrui commented 2 years ago

When running bot.rb example (after replace token value of course) I get this error:

❯ ./bot.rb
./bot.rb:10:in `block (2 levels) in <main>': undefined method `text' for #<Telegram::Bot::Types::ChatMemberUpdated 
❯ ruby -v
ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]
❯ rbenv version
3.0.3 (set by /home/david/proy/repos/external/telegram-bot-ruby/examples/.ruby-version)
Gregory280 commented 2 years ago

I created a bot and then tried to give it admin role in a group. Now the bot does not work anymore . It gives the same error of undefined method text for #

atipugin commented 2 years ago

You have to check type of message before accessing it, see https://github.com/atipugin/telegram-bot-ruby/issues/226#issuecomment-797296565

dvarrui commented 2 years ago

I don't know why... but now It works

> ruby -v
ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]

> rbenv version 
3.0.3 (set by /home/david/proy/repos/external/telegram-bot-ruby/examples/.ruby-version)
#!/usr/bin/env ruby 

require 'rubygems'
require 'telegram/bot'

#token = 'replace-me-with-your-real-token'
token = File.read('private.token').strip

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 '/end'
      bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}!")
    else
      bot.api.send_message(chat_id: message.chat.id, text: "I don't understand you :(")
    end
  end
end
atipugin commented 2 years ago

It will work until you receive message with type other than Message. That's why I suggest you to check type anyway. I need to update example code, because it was created when there were no other types except Message.

Gregory280 commented 2 years ago

Thanks! Now everything is working fine.