telegram-bot-ruby
telegram-bot-ruby copied to clipboard
Undefined method `text' for #<Telegram::Bot::Types::ChatMemberUpdated
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)
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 #Telegram::Bot::Types::ChatMemberUpdated:0x0000564a798fa010
You have to check type of message before accessing it, see https://github.com/atipugin/telegram-bot-ruby/issues/226#issuecomment-797296565
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
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
.
Thanks! Now everything is working fine.