An onlyIf clause is used for fine customization of activation rules. An onlyIf clause attached to an activation rule works like post-match condition that will either confirm activation by this rule, or reject it based on user-defined predicate.

An onlyIf function expects a predicate lambda-function as an argument bringing context, request and activator in a lambda context.

For example, this intent rule will be activated only if an entry with key name is in a client context:

        activators {
            intent("MakeOrder").onlyIf { context.client.containsKey("name") }
        }

Chaining

Multiple onlyIf conditions can be attached to a single activation rule by chaining working as a logical and:

        activators {
            intent("MakeOrder")
                .onlyIf { context.client.containsKey("name") }
                .onlyIf { activator.intent!!.confidence >= 0.7 }
        }

Built-in predicates

JAICF provides several handy onlyIf predicates out of the box:

disableIf

Opposite to an onlyIf

        activators {
            intent("TextMeBack").disableIf { request is TelephonyBotRequest }
        }

onlyIfInClient, onlyIfInSession

The rule will be activated only if an entry with a given key is in client/session context, and value has a given type (optional), and a given predicate is true (optional)

        activators {
            intent("MakeOrder").onlyIfInClient<Int>("age") { it >= 18 }
            intent("MakeOrder").onlyIfInSession<Int>("age") { it >= 18 }
        }

onlyIfNotInClient, onlyIfNotInSession

The rule will be activated only if an entry with a given key is not in client/session context

        activators {
            intent("Name").onlyIfInNotInClient("name")
            intent("Name").onlyIfInNotInSession("name")
        }

onlyFrom

The rule will be activated only if the current request is received from a given channel, or the rule was activated by a given Activator, or both

        activators {
            intent("MakeOrder").onlyFrom(telegram)
            intent("MakeOrder").onlyFrom(dialogflow)
            intent("MakeOrder").onlyFrom(facebook and caila)
        }

onlyIfBargeIn, disableIfBargeIn

The rule will be activated (rejected) only if the current request is a telephony barge-in request

        activators {
            intent("WhatsYourName").disableIfBargeIn()
            intent("Details").onlyIfBargeIn()
        }