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.
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
.
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()
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()
halt()
Stops the block matching process.
Usage
continue()
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.
Last updated