intent

This block will be executed if the intent provided as argument matches with the incoming message intent and if it was previously created and trained in the NLP engine.

intent(name=String|Array, &block)

Configuration

The NLP engine must be enabled and configured in bot/config/nlp.rb file in order to implement this block.

Platforms

Platform
Supported

Messenger

WhatsApp

Telegram

Usage

In the following example, MainContext will handle 3 different intentions:

  • greeting: Greeting messages like "Hi", "Hello" and so on.

  • goodbye: Goodbye messages like "Bye", "GodBye", "Goodnight" and so on.

  • thanks: Thank you messages like "Thanks", "Thank you", "I appreciate it" and so on.

class MainContext < Conversation

  def blocks

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

    intent "godbye" do
      @reply.text "Bye bye!"
    end

    intent "thanks" do
       @reply.text "You're welcome"
    end
  
end

Example of how they look in Wit.ai

As we mention before, each intent must has been created and trained in the NLP Engine (in this case Wit.ai)

Reading Params

This block passes 4 parameters that are: text, entities, traits and confidence, which contain additional information to the intention itself.

Usage example

For the example below, let's assume the incoming message says the following:

"Please wake me up tomorrow at 7am. I'd really appreciate it"

Assuming also that we have been created and trained an intent called set_alarm, that is linked to the entity wit/datetime and the trait wit/sentiment.

In the code below, thanks to these parameters the block can send an even more appropriate reply:

class MainContext < Conversation

  def blocks
  
    intent :set_alarm do |text, entities, traits, confidence|
    
       unless entities["wit$datetime:datetime"].nil?
       
          entity = entities["wit$datetime:datetime"].first
          @reply.text "I'll wake you up at #{entity[:value]}"
          
       else
       
          @reply.text "To help you with that, I need you to tell me a time for the alarm."
          
       end
       
       @reply.typing 1.second
       
       unless traits["wit$sentiment"].nil?
       
          trait = traits["wit$sentiment"].first
          case trait[:value]
             when "positive"
             
                @reply.text "And thank you for asking so kindly."
                
             when "negative"
             
                @reply.text "But you could try to be nicer next time.."
                
             when "neutral"
                # Nothing here
          end
       end
       
    end
  
end

Params definition

text

Just the text message "Please wake me up tomorrow at 7am. I'd really appreciate it"

entities

Array with the entities found.

{
  "wit$datetime:datetime": [
    {
      "id": "313292537627827",
      "name": "wit$datetime",
      "role": "datetime",
      "start": 38,
      "end": 53,
      "body": "tomorrow at 7am",
      "confidence": 0.9995,
      "entities": [

      ],
      "type": "value",
      "grain": "hour",
      "value": "2022-04-30T07:00:00.000-07:00",
      "values": [
        {
          "type": "value",
          "grain": "hour",
          "value": "2022-04-30T07:00:00.000-07:00"
        }
      ]
    }
  ]
}

traits

Array with the traits found.

{
  "wit$sentiment": [
    {
      "id": "5ac2b50a-44e4-466e-9d49-bad6bd40092c",
      "value": "positive",
      "confidence": 0.9047
    }
  ]
}

confidence

The percentage of confidence of the NLP engine in associating the message with the intent.

0.998

any_intent Block

Catch any intent of the message and returns 5 parameters: intent, text, entities, traits and confidence.

Usage

class MainContext < Conversation

  def blocks

    any_intent do |intent|
      if intent == "gretting"
        @reply.text "Hello!"
      elsif intent == "godbye"
        @reply.text "Bye bye!"
      elsif intent == "thanks"
        @reply.text "You're welcome" 
      end 
    end
  
  end

end

Routing to Context

An intent can be routed to a specific context, learn how in Routing Chapter.

Last updated