Conversational Forms

This feature, which includes two methods ask() and answer() allows you to create conversational forms.

ask(answer_route=String)

This method triggers a question and temporarily narrows the conversation until an expected answer is obtained.

Receives an argument, answer_route, which contains the route (in the format "context_name/answer_label"), where the logic for the answer resides.

Usage

ask("profile/get_email_address")
circle-info

If ask() is called in the same context where the answer() method is called, it is not necessary to include the context name in the route.

answer(label=String|Symbol, &block)

In this method, the logic for obtaining the expected answer is defined, as well as the exit logic in case the user decides not to answer.

Receives two arguments: label, which is the identification of the answer and block, where the logic for the answer is defined by calling all necessary action blocks within it.

Additionally, the ask(&block) method can be called, which will be executed automatically in the activation of the answer block. Within it the question can be sent to the user.

Usage

class ProfileContext < Conversation

  def blocks

    answer "get_email_address" do 

      ask do
        @reply.text "What is your email?"
      end

      regular_expression /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/ do |emails|
        @reply.text("Good, I'll register you under this email: #{emails.first}")      
        exit_answer()
      end

      keyword "stop" do 
        @reply.text "I'm stopping the sign up process now."
        exit_answer()
      end

      everything_else do
        @reply.text "I need an email in order to continue. Or write 'stop' if you want to cancel"
      end

    end
  
  end
    
end

exit_answer()

This method, which must be called within an answer block, returns the conversation to the context it was in before the ask() method was called.

circle-info

Another way to exit from an answer block is by calling to the ask() method again, but this time with a different answer_route.

Full Example

In the following example we will perform a user sign up process, by asking his email, age (optional) and their favorite color.

Last updated

Was this helpful?