Botscript
Overview
BotScript is a safe, lightweight scripting language designed for chatbot logic execution. It is inspired by Python, with familiar syntax and a curated set of features to ensure safety and simplicity.
If you're comfortable with Python or JavaScript, you'll find BotScript intuitive. You can experiment with it in BotStudio by selecting the ? icon > BotScript Documentation, and testing scripts via the Script Evaluator.
Core Concepts
What You Can Do with BotScript:
- Use built-in functions
- Assign and manipulate variables
- Embed variables in chatbot responses using string interpolation
Data Types
BotScript supports a subset of Python types:
- Primitives:
bool,int,float,str - Collections:
list,dict,set,tuple,slice - Special Objects: Named Entity Recognition (NER) objects
Data Structures
Lists
- Creation:
['apple', 'banana', 'orange'] - Zero-indexed access:
fruits[0]→'apple'
Dictionaries (Objects)
- Creation:
{'TicketId': 'TICK09786', 'authors': ['Peter', 'Sara']} - Access:
api_response['TicketId']
Sets
- Creation:
{1, 2, 2, 3}→{1, 2, 3}(duplicates removed)
Strings
- Newline:
'Hello\nWorld' - Concatenation:
'Email: ' + email
Entities (NER Functions)
first(x)– First occurrencelatest(x)– Latest occurrencener_all(x)– All occurrencesner_clear(x)– Clear allner_pop(x)– Remove and return latestner_exists(x)– Check existence
Date & Time Functions
datetime_now(tz)
Returns an object with:
year,month,day,hour,minute,secondweekday,yearday,timestamp,timezone,epochs
Example: datetime_now('Europe/Copenhagen')['year'] → 2025
datetime_from_isostring(s)
Example: datetime_from_isostring('2024-12-31T23:59:00')
datetime_from_timestamp(ts, tz)
Example: datetime_from_timestamp(1704067200, 1)
Operators
- Boolean:
and,or,not - Arithmetic:
+,-,*,/,//,% - String/List/Tuple:
+for concatenation - Comparison:
==,!=,<,<=,>,>=,in,not in - Conditional Expression:
x if y else z
Built-in Functions with Descriptions and Examples
General Utilities
join(sep, list)– Joins elements of a list into a string separated bysep.- Example:
join(', ', [1, 2, 3])→'1, 2, 3'
- Example:
sum(list)– Calculates the sum of all numeric elements.- Example:
sum([1, 2, 3])→6
- Example:
len(obj)– Returns the number of elements in a list, tuple, set, or dict.- Example:
len([10, 20, 30])→3
- Example:
str(x)– Converts a value to a string.- Example:
str(42)→'42'
- Example:
int(x)– Converts a value to an integer.- Example:
int('7')→7
- Example:
float(x)– Converts a value to a float.- Example:
float('3.14')→3.14
- Example:
bool(x)– Converts a value to boolean.- Example:
bool('true')→True
- Example:
list(x)– Converts to a list.- Example:
list((1, 2))→[1, 2]
- Example:
dict(x)– Converts a list of key-value pairs to a dictionary.- Example:
dict([(1, 'a'), (2, 'b')])→{1: 'a', 2: 'b'}
- Example:
set(x)– Converts a sequence to a set (unique values).- Example:
set([1, 1, 2])→{1, 2}
- Example:
tuple(x)– Converts a list to a tuple.- Example:
tuple([1, 2])→(1, 2)
- Example:
String Operations
lower(s)– Converts a string to lowercase.- Example:
lower('HELLO')→'hello'
- Example:
upper(s)– Converts a string to uppercase.- Example:
upper('hello')→'HELLO'
- Example:
replace(s, old, new)– Replaces all occurrences ofoldwithnewin strings.- Example:
replace('bot', 'o', 'a')→'bat'
- Example:
split(s)– Splits stringsby whitespace.- Example:
split('one two')→['one', 'two']
- Example:
split_by(s, sep)– Splits stringsby the delimitersep.- Example:
split_by('a,b,c', ',')→['a', 'b', 'c']
- Example:
startswith(s, prefix)– Checks if stringsstarts withprefix.- Example:
startswith('chatbot', 'chat')→True
- Example:
endswith(s, suffix)– Checks if stringsends withsuffix.- Example:
endswith('chatbot', 'bot')→True
- Example:
mask_email(s)– Obfuscates email addresses.- Example:
mask_email('user@example.com')→'u***@*******m'
- Example:
Collection Functions
keys(d)– Returns a list of keys in dictionaryd.- Example:
keys({'a': 1, 'b': 2})→['a', 'b']
- Example:
values(d)– Returns a list of values in dictionaryd.- Example:
values({'a': 1, 'b': 2})→[1, 2]
- Example:
items(d)– Returns a list of key-value pairs (tuples) from dictionaryd.- Example:
items({'a': 1})→[('a', 1)]
- Example:
dict_update(a, b)– Updates dictionaryawith entries fromb.- Example:
dict_update({'x': 1}, {'y': 2})→{'x': 1, 'y': 2}
- Example:
map(x, expr, collection)– Appliesexprto each itemxincollection.- Example:
map(x, x * 2, [1, 2, 3])→[2, 4, 6]
- Example:
foldl(x, acc, expr, init, collection)– Accumulates a value by applyingexprfor eachxwith accumulatoracc.- Example:
foldl(x, acc, acc + x, 0, [1, 2, 3])→6
- Example:
remove_if(x, cond, collection)– Removes items wherecondis true.- Example:
remove_if(x, x > 2, [1, 2, 3, 4])→[1, 2]
- Example:
zip(a, b)– Combines two sequences into a list of tuples.- Example:
zip([1, 2], ['a', 'b'])→[(1, 'a'), (2, 'b')]
- Example:
Utility Checks
exists(var)– Checks whether the variablevaris defined.- Example:
exists(my_var)→Trueifmy_varexists
- Example:
raises_error(code)– Returns True if the expressioncodewould raise an error.- Example:
raises_error('1/0')→True
- Example:
any(xs)– Returns True if any boolean in the listxsis True.- Example:
any([False, True])→True
- Example:
all(xs)– Returns True if all values in listxsare True.- Example:
all([True, True])→True
- Example:
re_findall(pattern, input, flags)– Returns a list of all matches for regexpatternininput.- Example:
re_findall('\d+', 'A1 B23')→['1', '23']
- Example:
Constants
- Math:
math.e,math.pi,math.nan,math.inf - Regex:
re.IGNORECASE,re.I,re.MULTILINE,re.M,re.DOTALL
Using Variables in Text
When inserting variables into chatbot responses, use interpolation:
'Hello, ' + name
Avoid just writing name, as it may print the literal word 'name'.
For any missing functionality, you can request new features via the support channel.