Blocks

The action blocks make it possible for a contexts to digest an incoming message or event with certain characteristics that matches with the execution criteria of a given block.

They are methods that receive a block of code as a parameter and must be called within the definition of the method blocks() in a Context class.

All necessary blocks can be added, one for each type of message/event that the context is expecting to receive; where (in most cases) only one of them will be executed, if a matches occurs.

Usage

Below, in the MainContext example, we're going to add some blocks and explain how each of them would capture and process an incoming message or event:

  • intent "greeting": A greeting messages, such as "Hello" or "Hi". If the intent exists and has been trained on the NLP engine.

  • postback "get_started": A click event on a button with a payload with the value "get_started" .

  • regular_expression /([a-z..: Captures the message, if this contains email address, it will return an array with the occurrences found.

  • any_attachment: Catches any attachment.

  • keyword: It will be executed if the incoming message value is "stop", "close" or "quit".

  • everything_else: It will be executed in the case of none of the blocks declared above could be executed.

class MainContext < Conversation

  def blocks

    intent "greeting" do
      @reply.text "Hello!"
    end

    postback "get_started" do |params|
      @reply.text "Welcome to Kogno framework!"
    end

    regular_expression /([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})/ do |emails|
      @reply.text "You've sent me these emails #{emails.join(',')}"
    end
    
    any_attachment do |attachment_files|
      @reply.text "You've sent me a file"
    end
    
    keyword ['stop','close','quit'] do
      @reply.text "I'll stop now"
    end
    
    everything_else do
      @reply.text "I can't understand what you say yet."
    end

  end

end

The order in which these blocks are called is irrelevant. The explanation bellow..

The Block Matching Process

This process will search for existing matches between the characteristics of an incoming message or event, with the execution criteria of the called blocks in the active context of the conversation.

If the match occurs, the block will be executed and in most cases this process will stop.

The matching process is always performed in the same predefined order, this way each block has a different execution priority.

List of Available Blocks

There is a wide variety of blocks, which are going to be listed in order of execution priority:

Execution priority example

To better understand how block matching process works, let's assume the following:

  • A user sends a message with the value "1" .

  • And the conversation is currently in the context GetNumberContext.

class GetNumberContext < Conversation

  def blocks

    any_number do |number|
      # This block will not execute, because keyword block 
      # has a higher execution priority. 
      @reply.text "You've send me the number #{number}. Captured by any_number block"
    end
    
    keyword "1" do
      @reply.text "You've send me the number 1. Captured by keyword block"
    end
    
    everything_else do 
      @reply.text "I'm expecting any number to respond something different."
    end
    
  end
  
end

As much as any_number is called (even first) and its execution condition matches with the message (which is a number), this will not be executed.

keyword "1" block will be executed, because it has a higher execution priority than any_number block, and with the execution of the first one, the matching process will be stopped.

Methods: halt() and continue()

These methods allows to control the block matching process, they can be called within an action block or in a callback in the Conversation class.

halt()

Stops the block matching process.

Usage

class MainContext < Conversation

  def actions
  
    before_anything do
      @reply.text "This block normaly doesn't stop the block matching process, but in this case it will do"
      halt()
    end
  
  end
  
end

continue()

Allows to the block matching process continues when is called in an action block.

Usage

Continuing the example in GetNumberContext.

By calling this method within keyword "1" block, any_number block will be executed too.

class GetNumberContext < Conversation

  def blocks

    any_number do |number|
      # This block will not execute because keyword has will be executed first. 
      @reply.text "You've send me the number #{number}. Captured by any_number block"
    end
    
    keyword "1" do
      @reply.text "You've send me the number 1. Captured by keyword block"
      continue()
    end
    
    everything_else do 
      @reply.text "I'm expecting any number to respond something different."
    end
    
  end
  
end

Last updated