A script runs the saved version of your code with the inputs from its trigger. A command can call it directly, or a pipeline can reach a Run script step and continue with the values the script returns. You create and save that version on Scripts.
Each run starts with a fresh sandbox: ordinary JavaScript variables last for that run only. Use pipeline variables to pass values to later steps, or script storage when a value must survive the next run.
Turn on Custom Code
Custom Code is off for a new channel. While it is off, you cannot open, make, change or delete scripts. The bot says: “Custom code is not enabled for this channel. Enable it on the Features page.” A script that makes a bot call is then denied.
- Open the Features settings page.
- Find Custom Code in the Automation area.
- Switch it on. The switch takes effect right away.
Run a script from a pipeline
- Open a pipeline and add the Run script step. It is in the Code area of the steps.
- Choose the saved script the step runs.
- Save the pipeline.
The step is the only way a pipeline runs a script.
A chat command can also run a script directly, when its What it does is Runs a code script.
When a command runs the script directly, nothing from bot.send reaches chat.
Use nnz.api.chat.send or nnz.api.chat.reply to post.
If the run does not end cleanly, the bot tells the viewer in chat: “Sorry, that command hit a snag and didn’t finish.”
What happens in a run
Every run takes these steps, in this order:
- The bot finds the script. A deleted script is not found.
- The bot checks that the script is switched on.
- The bot checks that the channel’s plan includes code scripts, and that the channel has script time left for this period.
- The bot takes the script’s published version. A version that failed its check on save never runs.
- The bot reads which bot calls that version makes, and grants only those. One call that is not allowed stops the whole run before it starts.
- The script runs in a fresh, closed sandbox.
- The bot saves the time of the run and its error, if there was one.
A script that stops at one of the first five checks does not run at all.
What a script gets
The sandbox gives the script three names: bot, nnz and console.
The script has no access to files, the network or the server, except through bot calls.
| Name | What it does |
|---|---|
bot.args | The words chat typed after the command word, as a list. |
bot.getVar(key) | Reads a pipeline variable. It gives null when the variable is not set. |
bot.setVar(key, value) | Writes a pipeline variable. The value is text. |
bot.send(message) | Adds text to the step’s output. It does not post in chat. |
bot.stopPipeline() | Stops the pipeline after this step. |
console.log(...) | Writes a line to the run’s log. console.info does the same. console.warn and console.error keep their lines with a warn: or error: prefix. |
The script works on its own copy of the pipeline variables.
Calls such as nnz.api.chat.send reach the bot, and the bot posts in chat in the channel’s one bot voice.
The calls under nnz are listed on Script basics.
This script reads the first word after the command, keeps it in a variable and greets that viewer in chat:
const target = bot.args[0] || 'chat';
bot.setVar('hug.target', target);
nnz.api.chat.send('Hugs for ' + target + '!');
What comes back to the pipeline
When the script ends, the step hands these back to the pipeline:
- Every variable the script wrote, with its new value.
- The text the script passed to
bot.send, as the step’s output. Later steps read it as the variablelast.output, written as\{last.output\}in a template. A failed run gives no output. - The stop, when the script called
bot.stopPipeline().
A run that does not end cleanly fails the step. A failed step stops the pipeline, unless the step is set to go on after an error.
Limits
Every run has the same limits.
| Limit | Value |
|---|---|
| Script execution time, including pauses in your code | 8 seconds |
| Waiting for one bot call | Up to 15 seconds |
| Bot calls | 64 |
| Statements | 200,000 |
| Memory | 64 MB |
Output from bot.send | 8,192 characters; the rest is cut off |
| Depth of nested function calls | 64 |
| Items in one list | 10,000 |
| Time for one regular expression | 1 second |
One nnz.time.sleep pause | 5 seconds |
| Log lines kept | 200; the rest are counted |
The script’s 8-second clock pauses while a bot call waits for its answer.
Each call has its own 15-second deadline, so a run involving speech synthesis or a network request can take longer than eight seconds in elapsed time.
With at most 64 calls, the outer elapsed-time bound is 968 seconds; it is a safety ceiling rather than a useful target for a chat interaction.
A script cannot build code from text: eval and new Function do not work.
The script runs in strict mode.
A bot call that takes longer than 15 seconds does not fail the run.
That call gives back null, nnz.lastError says the call took too long, and the script goes on.
The log keeps 200 lines.
After that, the log ends with a line such as “5 more console line(s) not shown.”
Errors
| Message | Why |
|---|---|
| run_code requires a valid code_script_id. | The step names no script. |
| Script not found. | The script was deleted. |
| Script is disabled. | The script is switched off. |
| Sandbox execution quota exhausted for this period. | The channel used up its script time for this period. |
| Code scripts need the Base plan. | The channel’s plan has no script time. |
| Sandbox execution quota could not be verified, so the script was not run. | The bot could not check the script time, so it did not run the script. |
| Script has no valid published version. | The script has no saved version that passed its check. |
| Capability is not enabled: … | Custom Code is off, and the script makes a bot call. |
| Unknown capability: … | The script names a bot call that does not exist. |
| Capability denied: … | The script made a bot call it was not granted. |
| Host-call budget exceeded. | The script made more than 64 bot calls. |
| Execution exceeded its time budget. | The script used its own execution-time allowance, or reached the outer elapsed-time bound. |
| Execution exceeded a resource limit. | The script went over the statements, the memory or the depth of nested calls. |
An error the script throws itself shows its own message. The bot keeps the last error of each script; Scripts explains where you read it.