L
L
leet8962019-05-18 17:11:05
ruby
leet896, 2019-05-18 17:11:05

Inline keyboards Ruby telegram Bot not working, How to do it right?

Greetings to all, I'm making Inline keyboards buttons for the bot, but they don't
work anyway .
and where to replace to make the code work, thank you in advance!

require 'telegram/bot'

token = ''


Telegram::Bot::Client.run(token) do |bot|
bot.listen do |message|
  case message.text
  when Telegram::Bot::Types::CallbackQuery
      if message.data == 'touch'
      bot.api.send_message(chat_id: message.from.id, text: "Don't touch me!")
    end
    if message.data == 'test'
      bot.api.send_message(chat_id: message.from.id, text: "Don't touch me!")
    end
  when '/start'
    kb = [
      Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Test', callback_data: 'test1'),
      Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Touch me', callback_data: 'touch'),
      Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Switch to inline', switch_inline_query: 'some text') 
    ]
    markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
    bot.api.send_message(chat_id: message.chat.id, text: 'Make a choice', reply_markup: markup)
  end
end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
triopsidae, 2019-05-19
@leet896

The problem is that your case accepts message.text because of this, it will give an error when the button is clicked. Solution example:

Telegram::Bot::Client.run(token) do |bot|
    bot.listen do |message|
        case message
        when Telegram::Bot::Types::CallbackQuery
            case message.data
            when 'touch'
                bot.api.send_message(chat_id: message.from.id, text: "Don't touch me!")
            when 'test'
                bot.api.send_message(chat_id: message.from.id, text: "Don't test me!")
            end
        when Telegram::Bot::Types::Message
            case message.text
            when '/start'
                kb = [
                    Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Test', callback_data: 'test'),
                    Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Touch me', callback_data: 'touch'),
                    Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Switch to inline', switch_inline_query: 'some text') 
                ]
                markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
                bot.api.send_message(chat_id: message.chat.id, text: 'Make a choice', reply_markup: markup)
            end
        end
    end
end

The code is not perfect, I hope it is at least understandable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question