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

NoMethodError when bot has no access to the messages on a group #249

Closed Gregory280 closed 2 years ago

Gregory280 commented 2 years ago

My bot was working fine when I add the different types of message for normal message chat and group message.

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
     case message
     when Telegram::Bot::Types::Message
         case message.text
         when '/start'
         ....
      when Telegram::Bot::Types::ChatMemberUpdated
         case message.text
         when '/start'
         ....
     end
  end
end

In the group test that I created and add the bot was working fine. After I add the bot to a group that I dont own, the bot shows in the contacts group but has no access to the messages. I think the owner have to give permission to the bot (am I wrong?). And now my script is broken returning this error:


2022-04-10T01:52:31.594886+00:00 app[worker.1]: programming_memes_bot.rb:22:in `block (2 levels) in <main>': undefined method `text' for #<Telegram::Bot::Types::ChatMemberUpdated:0x0000558e090b1b38> (NoMethodError)
2022-04-10T01:52:31.594907+00:00 app[worker.1]:     from /app/vendor/bundle/ruby/2.7.0/gems/telegram-bot-ruby-0.18.0/lib/telegram/bot/client.rb:34:in `block in fetch_updates'
2022-04-10T01:52:31.594907+00:00 app[worker.1]:     from /app/vendor/bundle/ruby/2.7.0/gems/telegram-bot-ruby-0.18.0/lib/telegram/bot/client.rb:33:in `each'
2022-04-10T01:52:31.594908+00:00 app[worker.1]:     from /app/vendor/bundle/ruby/2.7.0/gems/telegram-bot-ruby-0.18.0/lib/telegram/bot/client.rb:33:in `fetch_updates'
2022-04-10T01:52:31.594909+00:00 app[worker.1]:     from /app/vendor/bundle/ruby/2.7.0/gems/telegram-bot-ruby-0.18.0/lib/telegram/bot/client.rb:25:in `listen'
2022-04-10T01:52:31.594910+00:00 app[worker.1]:     from programming_memes_bot.rb:4:in `block in <main>'
2022-04-10T01:52:31.594912+00:00 app[worker.1]:     from /app/vendor/bundle/ruby/2.7.0/gems/telegram-bot-ruby-0.18.0/lib/telegram/bot/client.rb:18:in `run'
2022-04-10T01:52:31.594932+00:00 app[worker.1]:     from /app/vendor/bundle/ruby/2.7.0/gems/telegram-bot-ruby-0.18.0/lib/telegram/bot/client.rb:8:in `run'
2022-04-10T01:52:31.594933+00:00 app[worker.1]:     from programming_memes_bot.rb:3:in `<main>'
2022-04-10T01:52:31.737397+00:00 heroku[worker.1]: Process exited with status 1

Its strange because this NoMethodError for Telegram::Bot::Types::ChatMemberUpdated occured when I didnt have the add the code for type message for ChatMemberUpdated seen is this issue There is code missing for handling this kind of situation in my script? Or there a way handle error like this so the bot wont broken when this happens again? my repo

atipugin commented 2 years ago

Hi @Gregory280

There is no method #text for ChatMemberUpdated. If you don't need any special behavior for this type of update, you can just remove it from case statement:

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    case message
    when Telegram::Bot::Types::Message
      case message.text
      when '/start'
        # Respond to `/start` message
      end
    end
  end
end
Gregory280 commented 2 years ago

My bad. Thanks! Please delete this issue xD