Skip to content

Tasks

A task is a construct which is comparable to a dialog. However, tasks are meant for non-interactive, non-interruptible computations, therefore, no statements are allowed that perform user-level side effects, like say, ask or show.

Typically, tasks are executed when an event comes in:

task event: "ping" do
  counter = counter + 1
end

The advantage of using tasks over dialogs here is that tasks execute outside of the current dialog context. Therefore, the current dialog continues undisturbed, and the dialog stack and history remain untouched.

There fore they execute without any other effects (like __returning__) and are also much more performant than dialogs.

Named tasks

Tasks can be given a name, just like dialogs:

task calculate do
  result = 12345
end

These can be then executed by calling perform from a dialog

dialog main do
  perform calculate
  say "The result is: " + result
end

Guard clauses

Just like guards in dialogs, tasks can have a guard clause which will decide whether the task will be executed or not.

task calculate when user.frontend == "slack" do
  revenue = revenue + 200
end

task calculate do
  revenue = revenue + 100
end

Task hooks

Tasks can be hooked into several places while the Bubblescript interpreter does its work.

The hook is attached by creating a task which has the before: keyword in its declaration. Tasks like these are executed before the event occurs.

All tasks with a before: hook are executed one after the other, not just the first one like with perform.

task before: dialog

Executed just before the dialog with the given name is executed.

dialog main do
  say welcome
end

task before: main do
  welcome = "Good afternoon"
end

task before: __resolve__

This task is execued just before the Bubblescript interpreter starts looking for message triggers; e.g. when the users sends a message that is not caught by an ask and starts bubbling up in the dialog stack.

For instance, it can be used to perform basic classification. The user's utterance is stored in the message variable.

task before: __resolve__ do
  branch do
  message =~ "invoice" ->
    topic = "invoice"
  true ->
    topic = "unknown"
  end
end

dialog __unknown__ do
  say topic
end

task before: __user_message__

The before: __user_message__ hook gets executed any time a user's message enters into the system, regardless of it being caught by an ask or not. This hook can also be used to enrich the message variable with extra metadata.