Skip to content

Miscellaneous

condition(expression, expression_tags, conversation_tags)

Evaluates conditions based on tags using "any of", "all of" or "none of" expressions.

Consider using flow_condition instead, which is compatible with the corresponding CMS UI field.

Parameters:

expression  :
   "any of"  = ( .. OR .. )
   "all of"  = ( .. AND .. )
   "none of" = NOT ( .. AND .. )
   "" || nil = "any of"
   all others lead to false

expression_tags:
   which tags should be in the conversation tags
   [] leads to true

conversation_tags:
   having all the tags to check
   [] leads to false

Examples:

iex> condition("any of", ["a", "b"], ["a", "c", "d"])
true
iex> condition("any of", ["a", "b"], ["c", "d"])
false
iex> condition("all of", ["a", "b"], ["a", "c", "d"])
false
iex> condition("all of", ["a", "b"], ["a", "b", "c", "d"])
true
iex> condition("none of", ["a", "b"], ["a", "b", "c", "d"])
false
iex> condition("none of", ["a", "b"], ["c", "d"])
true
iex> condition("all of", ["a", "b"], [])
false

contains(string, substring)

Return whether a given value is part of the given list, or whether a given value is a substring of the given string.

This is a polymorphic function. When one of the arguments is nil, it will return false. In other cases, when invalid values are passed in, it will raise a runtime error.

Examples:

iex> contains("my name is", "name")
true
iex> contains([1, 2, 3], 3)
true
iex> contains("", "a")
false
iex> contains([], [])
false

count(element, list)

Counts the amount of elements in a list or an enumerable

Examples:

iex> count("a", ["a", "b", "a", "c"])
2
iex> count("a", "banana")
3

Not enumerable, returns nil:

iex> count("a", false)
nil

distance(a, b)

Return the geographical distance (in meters) between two geographical locations.

The parameters need be valid geographical locations, e.g. maps with a lon and lat attribute.

Examples:

iex> distance([4.893414, 52.364909], [4.8952, 52.3702])
600.6996152390233

empty?(list)

Returns true when list or enumerable is empty.

Examples:

iex> empty?(%{})
true
iex> empty?(%{a: 1})
false
iex> empty?([])
true
iex> empty?([1, 2])
false
iex> empty?("hello")
false
iex> empty?("")
true
iex> empty?(nil)
true
iex> empty?(false)
true
iex> empty?(:some_other_atom)
false

entity(args)

Construct a new entity

event(name)

Construct an event for use in expecting:

extract(sentence, entity, scope)

Given an input sentence and a match specification, extracts the information from the sentence.

When the entity matches, a %Bubble.Message{} is returned with its captures filled with the information extracted from the entity.

extract_all(sentence, entities, scope)

Given an input sentence and a list of match specifications, returns a message object that is enriched with all return values from all the matching entities.

For entities that defined normal captures, the corresponding capture variables are filled.

@postcode entity(match: "/[0-9]{4}\s*[A-Za-z]{2}/[=pc]")
@yesno entity(match: "/yes|no/[=yn]")
message = extract_all("yes I want to 1061BM", [@postcode])
say(message.pc) # "1061BM"
say(message.yn) # "yes"

For entities that have a return: declaration, the message.returns is set to an array containing all the return values of all the matching entities.

@topic_sales entity(return: "sales", match: "price | buy | order")
@topic_service entity(return: "service", match: "problem | issue")
message = extract_all("I want to buy a problem", [@topic_sales, @topic_service])
say(join(message.returns, ",")) # "sales,service"

intent(args)

Construct a new intent.

Examples:

@my_intent intent(match: [
  # BML: https://developer.botsquad.com/bubblescript/bml/
  "[gpe]"

  # With an entity:
  entity(match: "[gpe]")
])

The following options can be given to the intent constructor:

  • match - a BML match expression.
  • id - the identifier of the intent.
  • label - the label that is used when the intent is used as part of a dialog label or in the expecting clause of an 'ask'.
  • dtmf - the DTMF key that can be pressed to trigger this intent.

intent(base, args)

Construct an intent based on an already existing intent, overwriting the properties that are given in the second argument.

@yes intent(label: "Yes")

dialog main do
  yes_with_dtmf = intent(@yes, dtmf: "1")
end

length(string)

Return the length of a string or the length of a list.

This is a polymorphic function: When passed in a string, it returns the number of characters in the string. When passed a list or a map, it returns the number of elements. For all other values, it returns 0.

Examples:

iex> length("hello")
5
iex> length([1, 2, 3])
3

random(number)

Return a random item.

This is a polymorphic function: When a number is given, returns a random number between 0 and that number. When a list is given, returns a random element from the list.

Examples:

iex> random(10)
2
iex> random([4, 2, 6, 9])
6

reverse(string)

Reverse the given list or string.

When a different value is passed in, it will raise a runtime error.

Examples:

iex> reverse("abcd")
"dcba"
iex> reverse([1, 2, 3])
[3, 2, 1]

slice(string, startpos, endpos \\ nil)

Slice a substring out of the given string, or a sublist out of the given list. Arrays and strings are 0-indexed, meaning that the first element has the position of 0.

If no endpos is given, the array or list will be sliced until the end.

When a different value is passed in, it will raise a runtime error.

Examples:

iex> slice("a slice of bread", 11)
"bread"
iex> slice("a slice of bread", 2, 5)
"slice"
iex> slice([1, 2, 3], 0, 1)
[1]

take_random(enumerable, count)

Take a number of items from a list, map or string at random.

Examples:

iex> take_random("hello", 2)
"ol"
iex> take_random([1, 2, 3], 2)
[3, 2]
iex> take_random(%{1 => "a", 2 => "b", 3 => "c"}, 2)
%{1 => "a", 3 => "c"}

uuid()

Generate a random, version 4, UUID.

valid_json_schema?(value, schema)

Confirm if a value is valid according to the given JSON schema.

If you need more extensive validation info, refer to validate_json_schema/2.

Examples:

iex> schema = %{
...>   "type" => "object",
...>   "properties" => %{
...>     "foo" => %{"type" => "number"}
...>   }
...> }
iex> valid_json_schema?(%{"foo" => 1}, schema)
true
iex> valid_json_schema?(%{"foo" => "1"}, schema)
false
iex> valid_json_schema?(%{"foo" => 1}, "not a schema")
:invalid_schema
iex> valid_json_schema?(%{"foo" => 1}, %{"type" => "invalid"})
:invalid_schema

validate_json_schema(value, schema)

Validate a value against a JSON schema.

Returns detailed error information when validation fails. See ExJsonSchema for more information.

Examples:

iex> schema = %{
...>   "type" => "object",
...>   "properties" => %{
...>     "foo" => %{"type" => "number"}
...>   }
...> }
iex> validate_json_schema(%{"foo" => 1}, schema)
[]
iex> validate_json_schema(%{"foo" => "hi"}, schema)
[
  %ExJsonSchema.Validator.Error{
    path: "#/foo",
    error: %ExJsonSchema.Validator.Error.Type{expected: ["number"], actual: "string"}
  }
]
iex> validate_json_schema(%{"foo" => 1}, "not a schema")
:invalid_schema
iex> validate_json_schema(%{"foo" => 1}, %{"type" => "invalid"})
:invalid_schema