Built-in Functions
Macros are built-in functions that perform some common tasks. Below is a list of some common macros that you can use at any stage in your CSML flows.

OneOf

Return one the elements of the array passed in the first argument at random.
Useful for randomisation of dialogue parts!
1
say OneOf(["I like you", "You're awesome"])
Copied!

Shuffle

Given an array, return it with its elements in a random order.
Especially useful in questions to randomize the available options!
1
do btn1 = Button("Blue")
2
do btn2 = Button("Red")
3
do btns = Shuffle([btn1, btn2])
4
5
say Question(
6
title = "Select a pill",
7
buttons = btns
8
)
Copied!

Find

Return whether a string is contained in another string.
1
say Find("needle", in="haystack") // false
2
say Find("yes", in="well yes, I like cheese") // true
Copied!

Length

Return the length of a given string or array
1
say Length("My horse is amazing") // 19
2
say Length([1, 2, 3]) // 3
Copied!

Random

Return a random floating point number in the range 0-1 (0 included, 1 excluded).
1
say Random() // 0.03196249773128712
2
say Random() // 0.6416423921015862
Copied!

Floor

Return the largest integer less than or equal to the given number.
1
say Floor(1.23456) // 1
Copied!
This is useful to generate a random integer in a given range:
1
say Floor(Random() * 5) // a random integer between 0-4 (included)
2
say Floor(Random() * 8) + 12 // a random integer between 12 - 19 (included)
Copied!

UUID

Generate a random UUID (v1 or v4, defaults to v4)
1
say UUID() // "aa4b9fb4-4d37-488c-981f-8aebc4eb9eaa"
2
say UUID("v1") // "d0b40e8e-7ea4-11eb-9439-0242ac130002"
3
say UUID("v4") // "4b784011-e49b-4913-9d58-7abf4f8a56bc"
Copied!

Time

The Time() helpers lets your manipulate timestamps and dates easily.
1
do time = Time() // initialize a Time object at the current UTC time
2
do time = Time().at(2021, 03, 28, 12, 53, 20, 123) // initialize a time object at 2021-03-28T12:53:20.123Z
3
4
do time.unix() // generate the unix timestamp (in milliseconds)
5
6
do time.format() // returns an ISO8601 string
7
do time.format("%h%d") // returns a string with a custom format
8
9
do time.add(60) // adds 60 seconds to the value
10
do time.sub(60) // subtract 60 seconds to the value
11
12
do time = Time().parse("2021-03-28") // parse a date
13
do time = Time().parse("2021-03-28T12:53:20Z") // parse an ISO-formatted string
14
do time = Time().parse("01/01/2021", "%d/%m/%Y") // parse a custom-formatted string
Copied!
CSML's Time function is based on Rust's Chrono library. All the formatting options are listed here: https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html

Exists

Check if a variable has been saved in the chatbot's memory before for the current user.
1
do Exists("myvar") // false
2
remember myvar = 123
3
do Exists("myvar") // true
Copied!
Exists only checks for the existance of top-level variable names in the chatbot's memory for the current user, i.e obj and not obj.prop
Last modified 18d ago