Skip to content

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.

mzs
include bot
bot.say("Hello, ${bot.get("name")}!")
text
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 ​

NameWhat it is
$messagewhat the visitor has just written (a string, may be "")
$clientthe channel: "web", "telegram", "cron" (scheduled)
bot.get("name")a dialogue variable — a string or nil
mzs
include bot
$message  # → "Hello! Where is my order 10452?"
$client  # → "web"
bot.get("city")  # → "Kazan"
bot.get("nope")  # → nil

There are no other $ variables: $name in code is nil. Scenario variables are read only with bot.get — see Bot data.

Exits ​

ExitWhen
«дальше» (next)by default, or bot.exit("next")
a custom exitbot.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.

mzs
include bot
total = bot.get("price").int * bot.get("qty").int
if total >= 5000 {
  bot.exit("vip")
  exit()
}
bot.say("Total: ${total} ₽")
text
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.

mzs
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")
text
raise: order 10452 not found

try 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 ​

ModuleFor
botmessages, buttons, variables, exits, bot.http
llmthe platform's language model — The language model
json, url, csvdata formats
time, datetime; the clock is in UTC
math, decimalcalculations, money
cryptohashes, hmac, base64
pathpath parsing
taskstasks.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:

mzs
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 == "".

AllowedNot allowed (an error, exit «ошибка»)
bot.http, llm.askbot.say, bot.file
bot.store_* — bot variablesbot.edit, bot.delete
bot.get, bot.set — live only in this runbot.buttons, bot.wait
bot.exit — e.g. to a Telegram block («Telegram»)
mzs
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.