Routing

Route an incoming message or event to a specific context of the conversation.

It's a very useful feature when it comes to decentralizing the handling of incoming messages or events and thus to achieving a more distributed code.

Postback

A postback payload can contain a path to a context, in addition to the information that the payload normally carries.

Examples

Route only

"context_name/payload"

With params

set_params("context_name/payload", {p1: "value1", p2: "value2"})

Usage

class MainContext < Conversation

  def blocks
  
    intent "greeting" do
    
      @rsp.quick_reply(
        "Would you like to sign up?",
        [
          {
            title: "Create an Account",
            payload: "profile/sign_up"
          }
        ]
      )      
      
    end
    
  end
  
end

The click event on "Create an Account" button will be handled by the ProfileContext which will receive the payload without the context path information.

class ProfileContext < Conversation

  def blocks
  
    postback "sign_up" do 
    
      @reply.text "To create a new account please click in the button bellow."
      @reply.url(
        {
          title: "Create a new User",
          url: "https://kogno.io/sign_up"
        }
      )
      
    end
    
  end
  
end

Intent

In order to implement this type of routing. In the NLP engine, just create an intent whose name starts with the name of an existing context in the project, followed by an underscore and the intent's own information. For instance: profile_sign_up , where profile is the context and sign_up is the intent.

Intent creation example in Wit.ai

Usage

The profile_sign_up intent will be handled by the ProfileContext and this will be able to capture the "sign_up" intent, without path information.

class ProfileContext < Conversation

  def blocks
  
    intent "sign_up" do 
    
      @reply.text "To create a new account please click in the button bellow."
      @reply.url(
        {
          title: "Create a new User",
          url: "https://kogno.io/sign_up"
        }
      )
      
    end
    
  end
  
end

Commands (Telegram)

In order to routing Telegram commands, just edit the config.routes.commands field in config/platforms/telegram.rb, adding a line with the format :command => :context_name for each command that we want to route.

config.routes.commands = {
  :start => :main,
  :sign_up => :profile
}

Usage

class ProfileContext < Conversation

  def blocks
  
    command "sign_up" do 
    
      @reply.text "To create a new account please click in the button bellow."
      @reply.url(
        {
          title: "Create a new User",
          url: "https://kogno.io/sign_up"
        }
      )
      
    end
    
  end
  
end

Commands that have not been configured will be handled by the default context.

A click event on a deep link can contain a path to a context, if the value of the params: ref (Messenger) or start (Telegram), starts with the name of an existing context in a given project.

Messenger Example

https://m.me/kogno.io/ref=profile_sign_up

Telegram Example

https://t.me/KognoBot?start=profile_sign_up

In both examples, the click events will be be handled by ProfileContext and param value will be "sign_up", without path information

class ProfileContext < Conversation

  def blocks

    deep_link do |value|
    
      if value == "sign_up"
        @reply.text "To create a new account please click in the button bellow."
        @reply.url(
          {
            title: "Create a new User",
            url: "https://kogno.io/sign_up"
          }
        )    
      end  
      
    end

  end
  
end

Last updated