Skip to content

Dialogue and buttons ​

FunctionWhat it does
bot.buttons(items)a menu under the bot's last message
bot.wait()stops the script; the visitor's next message runs it again

bot.buttons ​

An item is a label (a string or a number) or a link {text, url}. Pressing a label sends it as the visitor's next message; a link opens and sends nothing.

mzs
include bot
bot.say("How will you pay?")
bot.buttons(["Card", "Cash", {text: "Pricing", url: "https://example.com/prices"}])
text
How will you pay?
[Card] [Cash] [Pricing ↗]

The menu sits under the bot's last message — bot.say first, then the buttons. Empty labels are skipped, extra spaces collapse.

A menu from data:

mzs
include bot
include json
r = bot.http({url: "https://cafe.example.com/products"})
menu = json.parse(r["body"])["items"]
bot.say("What would you like?")
bot.buttons(menu.map { "${it["name"]} — ${it["price"]} ₽" }.take(10))
text
What would you like?
[Cappuccino — 220 ₽] [Latte — 250 ₽] [Raf — 280 ₽]

Up to 10 buttons per call, up to 3 calls per run, a label up to 64 characters, url only http(s).

bot.wait ​

A script can't wait in the middle of its code. bot.wait() ends the run (like exit(); try doesn't catch it), the block waits, and the visitor's answer runs the script from the first line — with the answer in $message.

mzs
include bot
bot.say("What is your name?")
bot.wait()
bot.say("never")
text
What is your name?

Only bot.set variables live between runs. So the order is:

  1. ask the question and remember the step — bot.set("step", "name");
  2. bot.wait();
  3. in the next run, read the step and save $message as the answer to it.

While the block waits, its exit doesn't matter. The scenario's global blocks fire before it.

Step by step ​

mzs
include bot
step = bot.get("order_step") ?? ""
answer = $message.trim

fn ask(field, question, options = nil) {
  bot.set("order_step", field)
  bot.say(question)
  bot.buttons(options) if options != nil
  bot.wait()
}

if step == "size" && answer in ["S", "M", "L"] {
  bot.set("size", answer)
} else if step == "phone" && answer.replace(/\D/, "").len >= 10 {
  bot.set("phone", answer.replace(/\D/, ""))
}

ask("size", "What size?", ["S", "M", "L"]) if (bot.get("size") ?? "").blank
ask("phone", "Your phone number?") if (bot.get("phone") ?? "").blank

bot.set("order_step", "")
bot.say("Done: size ${bot.get("size")}, phone ${bot.get("phone")}.")
bot.exit("next")
text
What size?
[S] [M] [L]
The visitor's messageWhat the bot answers
«I want a T-shirt»«What size?» and buttons, waits
pressed «M»«Your phone number?», waits
«hi»«Your phone number?» again — not a valid answer
«+7 999 123-45-67»«Done: …», leaves by «дальше» (next)

ask ends with bot.wait(), so nothing runs after the call. A wrong answer isn't saved — the same question is asked again.

Editing the question after the answer ​

The question's id is saved in a variable — the next run replaces the question with the choice:

mzs
include bot
if bot.get("menu_msg") == nil {
  bot.set("menu_msg", bot.say("What are you interested in?"))
  bot.buttons(["Delivery", "Payment"])
  bot.wait()
}
bot.edit(bot.get("menu_msg"), "You chose: **${$message}**")
text
What are you interested in?
[Delivery] [Payment]

Common mistakes ​

MistakeWhat the visitor seesThe fix
a question without bot.wait()all the questions at oncebot.wait() after each question
bot.exit("next") instead of waitingthe scenario goes on without an answerbot.wait()
no step variablethe first "hi" saved as the namesave the answer only when step == "…"
bot.say("Hello!") at the top, unconditionallya greeting before every questiongreet only when step == ""
the answer not saved with bot.setthe same question over and overbot.set(step, $message)

Several such scripts in one bot — use different step names (order_step, profile_step): dialogue variables are shared.