The Script block
A scenario block in the Morze studio builder with mzs code inside: the Script block («Скрипт»). Each time the dialogue reaches the block, the script runs top to bottom and leaves by one of its exits.
include bot
bot.say("Hello, ${bot.get("name")}!")Hello, Anna!Under the examples of this section is what the visitor sees: a message, 📎 a file, [buttons], ✎ an edit, ✕ a delete.
What the script gets
| Name | What it is |
|---|---|
$message | what the visitor has just written (a string, may be "") |
$client | the channel: "web", "telegram", "cron" (scheduled) |
bot.get("name") | a dialogue variable — a string or nil |
include bot
$message # → "Hello! Where is my order 10452?"
$client # → "web"
bot.get("city") # → "Kazan"
bot.get("nope") # → nilThere are no other $ variables: $name in code is nil. Scenario variables are read only with bot.get — see Bot data.
Exits
| Exit | When |
|---|---|
| «дальше» (next) | by default, or bot.exit("next") |
| a custom exit | bot.exit("name") — the name of a block exit |
| «ошибка» (error) | bot.exit("error"), raise, a runtime error, a limit |
bot.exit only picks the exit — the script goes on, and the last call wins. To stop the script: exit() or a top-level return.
include bot
total = bot.get("price").int * bot.get("qty").int
if total >= 5000 {
bot.exit("vip")
exit()
}
bot.say("Total: ${total} ₽")Total: 3000 ₽An unknown exit name is an error. A block has up to 10 custom exits; next and error are always there.
«Ошибка» and $script_error
A failed script leaves by «ошибка» (error), with the error text (up to 1000 bytes) in the dialogue variable $script_error. Whatever bot.say and bot.set did before the failure stays.
include bot
id = $message.captures(/(\d+)/)?.dig(1)
raise("no order number") if id == nil
bot.say("Looking up order ${id}…")
raise("order ${id} not found")raise: order 10452 not foundtry catches an error — then the block doesn't leave by «ошибка»: x = try risky() else fallback.
Waiting for an answer
bot.wait() stops the script; the block waits for the visitor's next message and runs the script again, with the answer in $message. See Dialogue and buttons.
What is available
| Module | For |
|---|---|
bot | messages, buttons, variables, exits, bot.http |
llm | the platform's language model — The language model |
json, url, csv | data formats |
time, date | time; the clock is in UTC |
math, decimal | calculations, money |
crypto | hashes, hmac, base64 |
path | path parsing |
tasks | tasks.all, tasks.race for async fn |
Each one comes with include: include json, then json.parse(s).
Not available: the http module (use bot.http instead) and io, rand, uuid, shuffle, sample, crypto.random. println output shows only in the studio's trial run, never to the visitor.
The bot's files
A bot's scripts are files (Survey.mzs, lib/utils.mzs). The block runs one file, which includes others as modules:
include bot
include money from "lib/money"
bot.say("Total: ${money.rub(bot.get("price").int)}")A path is relative to the file's folder ("utils", "../lib/x") or to the root ("/lib/x"); .mzs may be left out. A module exports with export fn and writes include bot itself if it needs it.
Scheduled scripts
The same block with a schedule (cron, 5 fields, a time zone) runs by itself, with no visitor: $client == "cron", $message == "".
| Allowed | Not allowed (an error, exit «ошибка») |
|---|---|
bot.http, llm.ask | bot.say, bot.file |
bot.store_* — bot variables | bot.edit, bot.delete |
bot.get, bot.set — live only in this run | bot.buttons, bot.wait |
bot.exit — e.g. to a Telegram block («Telegram») |
include bot
include json
r = bot.http({url: "https://api.example.com/rates?base=USD"})
raise("no rate: ${r["status"]}") if !r["ok"]
rate = json.parse(r["body"]).dig("rates", "RUB")
bot.store_set("usd_rate", rate)
bot.store_get("usd_rate") # → "92.5"Up to 5 scheduled blocks per bot, firing at most once every 5 minutes.