Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answers ReplyKeyboardMarkup #26

Closed
raphaelmro opened this issue Dec 6, 2015 · 6 comments
Closed

Answers ReplyKeyboardMarkup #26

raphaelmro opened this issue Dec 6, 2015 · 6 comments

Comments

@raphaelmro
Copy link

How can I get the ReplyKeyboardMarkup answer to interact with the user?
I've tried something like

puts answers

for the given code

option = 'What you wanna do?'
        answers =
            Telegram::Bot::Types::ReplyKeyboardMarkup
                .new(keyboard: [%w(Add Remove), %w(Search)], one_time_keyboard: true)
        bot.api.send_message(chat_id: message.chat.id, text: option, reply_markup: answers)

but just returned an object.

Thanks

@atipugin
Copy link
Owner

atipugin commented Dec 6, 2015

Following code did the job perfectly:

require 'bundler/setup'
require 'telegram/bot'

token = 'secret'

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    option = 'What you wanna do?'
    answers = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: [%w(Add Remove), %w(Search)], one_time_keyboard: true)
    bot.api.send_message(chat_id: message.chat.id, text: option, reply_markup: answers)
  end
end

Proof pic:

screen shot 2015-12-07 at 00 08 27

Seems like something wrong with your code, not the gem.

@raphaelmro
Copy link
Author

Yes, it works but i wanna do stg like:
When the user type '/help' the 'menu' is opened.

Telegram::Bot::Client.run(token_telegram) do |bot|
  bot.listen do |message|
    case message.text
      when '/help'
        option = 'What you wanna do?'
        answers =
            Telegram::Bot::Types::ReplyKeyboardMarkup
                .new(keyboard: [%w(Add Remove), %w(Search], one_time_keyboard: true)
        bot.api.send_message(chat_id: message.chat.id, text: option, reply_markup: answers)

...and depending the users' option, i will do an action. The problem is, how can i get the value i choose?
Example: Type '/help' > (Menu is displayed) > Choose 'Add' >(Bot asks for a string to add)> Users type>Store the string(array, hash or db)

@raphaelmro raphaelmro reopened this Dec 7, 2015
@raphaelmro
Copy link
Author

Sorry, pressed the wrong button and reopened. lol

@atipugin
Copy link
Owner

atipugin commented Dec 7, 2015

I got you. You have to store somehow user's current state and decide what to do depending on this state. In your example, you need to change user's state to "adding_something" if he/she types "/help" and chooses "Add". All next incoming messages from that user should be processed according his state. So, if user with state "adding_something" types "/help" again, you have treat it as a string, not a command to show the help menu.

I did something similar here: https://github.com/atipugin/telekrug. It uses sinatra + webhooks, but in general idea is the same.

Take a look here: https://github.com/atipugin/telekrug/blob/master/app/workers/process_message.rb. This class (sidekiq worker) handles each incoming message from any user.

@raphaelmro
Copy link
Author

Yeah, i understood but i' having problem to get the 'Add' string when you press the option, in this case my reply to the question. A simple message.text was not supposed to get it?

  Telegram::Bot::Client.run(token_telegram) do |bot|
    bot.listen do |message|
      case message.text
        when '/help'
          option = 'What you wanna do?'
          answers =
              Telegram::Bot::Types::ReplyKeyboardMarkup
                  .new(keyboard: [%w(Add Remove), %w(Search], one_time_keyboard: true)
          bot.api.send_message(chat_id: message.chat.id, text: option, reply_markup: answers)
          my answer = message.text.. #my problem is here - not getting the 'add' reply
          case my_answer
            when "Add"
             # (do stg here)
            when "Remove"
             # (do stg here)
            when "Search"
             # (do stg here)
          end

Sorry if it's a newbie question, i'm learning ruby with this project.

@atipugin
Copy link
Owner

atipugin commented Dec 7, 2015

It doesn't work this way. You can't handle answers inside when '/help' block, because answer message never gets inside that block (as your statement condition clearly says: "only messages with '/help' text goes here").

@atipugin atipugin closed this as completed Dec 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants