telegram-bot-ruby icon indicating copy to clipboard operation
telegram-bot-ruby copied to clipboard

How to save user's text messages correctly?

Open mechnicov opened this issue 5 years ago • 1 comments

I want to make quiz. But I don't understand how to do it.

I can't save user's message correctly.

For example I have one question:

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    bot.api.send_message(chat_id: message.chat.id, text: '2+2')
    bot.api.send_message(chat_id: message.chat.id, text: 'OK') if message.text == '4'
  end
end

So dialogue is:

                        /start

2+2

                           4

2+2

OK

Using filter /start:

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: '2+2')
      bot.api.send_message(chat_id: message.chat.id, text: 'OK') if message.text == '4'
    end
  end
end

So dialogue:

                        /start

2+2

                           4

mechnicov avatar Jan 17 '19 21:01 mechnicov

If we have only one question we can do something like this:

  bot.listen do |message|
    case message.text
    when '/start'
      bot.api.send_message(chat_id: message.chat.id, text: '2+2')
    when message.text == '4'
      bot.api.send_message(chat_id: message.chat.id, text: 'OK')
    end
  end
end

But for full quiz functionality, I think you need to add a database to your application and store last user action for understanding on which step the user is now.

alexander-saratovcev avatar May 10 '20 11:05 alexander-saratovcev

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Feb 19 '23 15:02 github-actions[bot]