Skip to content

Bot data ​

Two kinds of variables: dialogue ones — each visitor has their own — and bot ones — shared by all dialogues and scheduled runs.

FunctionWhat it does
bot.get(name)a dialogue variable — a string or nil
bot.set(name, value)saves a dialogue variable as a string → that string
bot.vars()all dialogue variables — a dict of strings
bot.store_get(name)a bot variable — a string or nil
bot.store_set(name, value)saves a bot variable as a string → that string
bot.store_delete(name)deletes a bot variable
bot.store_all()all bot variables — a dict of strings

Dialogue variables ​

These are the same variables as $name in scenario texts, the Ask («Спросить»), Variable («Переменная») and HTTP request («HTTP-запрос») blocks.

mzs
include bot
bot.get("name")  # → "Anna"
bot.set("city", "Moscow")  # → "Moscow"
bot.get("city")  # → "Moscow"
bot.get("phone")  # → nil

Values are always strings; they don't turn into numbers by themselves:

mzs
include bot
bot.get("price") + 1
text
cannot add int to string

A number from a variable — .int or .float (nil.int is 0):

mzs
include bot
bot.get("price").int * bot.get("qty").int  # → 3000
(bot.get("discount") ?? "0").int  # → 0

bot.set turns the value into a string: a number — "300", true — "true", nil — "", an array or a dict — JSON.

mzs
include bot
include json
bot.set("total", 300)  # → "300"
bot.set("paid", true)  # → "true"
bot.set("cart", [{sku: "A1", qty: 2}])  # → "[{\"sku\":\"A1\",\"qty\":2}]"
json.parse(bot.get("cart"))[0]["qty"]  # → 2

A name is Latin letters, digits and _, not starting with a digit or __. Up to 50 bot.set per run, a value up to 4000 bytes. What is written is seen by the next blocks at once, and a script failure can't roll it back.

bot.vars ​

mzs
include bot
bot.vars().keys  # → ["name","city","price","qty","__sent"]

The studio's own variables (__…) are hidden, except __sent — the visitor's last message.

An empty value ​

"" is truthy: check blank, and cover nil with ??.

mzs
include bot
bot.set("comment", nil)
(bot.get("comment") ?? "").blank  # → true
(bot.get("nope") ?? "").blank  # → true

The visitor's file ​

A photo or a document sent with the message (widget or Telegram): its link, name and MIME type. No file — nil. A file may come with no text: $message is "" then.

VariableWhat it holds
bot.get("file_url")the file's public link
bot.get("file_name")the name it was sent with
bot.get("file_type")MIME, e.g. "image/jpeg"
mzs
url = bot.get("file_url") ?? ""
if !url.blank {
  kind = (bot.get("file_type") ?? "").starts_with("image/") ? "photo" : "file"
  bot.say("Got your ${kind} «${bot.get("file_name")}», thanks!")
  bot.exit("file")
} else {
  bot.say("Please send a photo of the receipt.")
}
text
Please send a photo of the receipt.

Without code, the scenario has the same $file_url, $file_name, $file_type: a Condition block on file_url → «не пусто» (not empty).

Bot variables ​

Kept on the platform, they don't expire and are shared by all visitors. In scenario texts — $bot.name, in code — bot.store_get.

mzs
include bot
bot.store_get("usd_rate")  # → "92.5"
bot.store_set("visits", (bot.store_get("visits") ?? "0").int + 1)  # → "1"
bot.store_delete("promo")
bot.store_all()  # → {"usd_rate":"92.5","hours":"9-21","visits":"1"}

A name is up to 64 characters, by the same rules. A value is up to 4000 bytes, up to 100 variables per bot, store_set and store_delete together up to 20 per run. Bots with «Доступ студии» (Studio access) have these limits lifted: a value up to 8 MB, no limit on the number of variables or writes.

Writes are "last one wins": two dialogues incrementing a counter at the same time may lose one increment.

$ variables ​

The Router gives a script only $message and $client. Any other $name in code is nil, inside a string too:

mzs
include bot
$name  # → nil
bot.get("name")  # → "Anna"
"Hello, $name!"  # → "Hello, !"
"Hello, ${bot.get("name")}!"  # → "Hello, Anna!"

Writing $x = 1 doesn't save it to the dialogue — only bot.set does. A local variable in a string is ${x}, not $x.