Skip to content

HTTP requests ​

bot.http(req) — a request to the internet, like the scenario's HTTP request block («HTTP-запрос»). Bot scripts have no http module.

mzs
include bot
r = bot.http({method: "POST", url: "https://…", headers: {"X-Key": "…"}, body: "…", timeout: 5})
Request field
urlrequired, http:// or https://; the public internet only
methodGET (default), POST, PUT, PATCH, DELETE, in any case
headersa dict of strings; User-Agent: MorzeBot/1.0 is set by itself
bodya string up to 64 KB; not sent with GET
timeoutseconds, at most 5 (default 5)
Response field
oktrue for status 200–299
statusthe response code; 0 — no response
bodythe response body as a string, up to 256 KB (longer is cut silently)
error"" or the reason: "timeout", "the host is not found", "address not allowed: only the public internet"…

There are no response headers.

GET and JSON ​

mzs
include bot
include json
r = bot.http({url: "https://shop.example.com/api/orders/10452"})
r["ok"]  # → true
r["status"]  # → 200
order = json.parse(r["body"])
order["status"]  # → "shipped"
order["items"].map { it["name"] }  # → ["Kettle","Mug"]

Failure is not an exception ​

bot.http doesn't fail on a failed request — check ok. error of a successful response is "", and "" is truthy: checking r["error"] is useless.

mzs
include bot
r = bot.http({url: "https://shop.example.com/api/orders/1"})
r["ok"]  # → false
r["status"]  # → 404
r = bot.http({url: "https://no-such-host.invalid/"})
[r["status"], r["error"]]  # → [0,"the host is not found"]

Only a request that isn't a dict and the fourth request of a run fail.

POST with JSON ​

body is a string: encode a dict with .json. The default Content-Type is application/json if the body starts with { or [, otherwise text/plain.

mzs
include bot
include json
lead = {name: bot.get("name"), phone: "+79991234567", source: "chatbot"}
r = bot.http({
  method: "POST",
  url: "https://crm.example.com/api/leads",
  headers: {"Authorization": "Bearer sk_test_123"},
  body: lead.json,
})
if r["ok"] {
  bot.set("lead_id", json.parse(r["body"])["id"])
  bot.say("Request received ✅")
} else {
  bot.say("That didn't work, a manager will contact you.")
  bot.exit("error")
}
text
Request received ✅

The bot's owner sees a token in the code; the visitor doesn't.

Query string and form ​

The visitor's text goes into a link only through url.encode or url.query:

mzs
include url
q = url.query({q: "green tea", limit: 3})
"https://shop.example.com/search?${q}"  # → "https://shop.example.com/search?q=green%20tea&limit=3"

A form — the same url.query in the body, with its own Content-Type:

mzs
include bot
include url
r = bot.http({
  method: "POST",
  url: "https://forms.example.com/leads",
  headers: {"Content-Type": "application/x-www-form-urlencoded"},
  body: url.query({name: "Anna", msg: $message}),
})
r["status"]  # → 201

Authorization ​

mzs
include crypto
include json
{"Authorization": "Basic ${crypto.base64("user:pass")}"}  # → {"Authorization":"Basic dXNlcjpwYXNz"}
body = {event: "lead", id: 7}.json
{"X-Signature": crypto.hmac("secret", body)}["X-Signature"].len  # → 64

Several requests at once ​

Up to 3 requests per run, each up to 5 s, all within the run's 10 s. Independent requests run in parallel, with async fn:

mzs
include bot
include json
include tasks
async fn fetch(path) { bot.http({url: "https://api.example.com${path}"}) }
[rates, weather] = tasks.all([fetch("/rates"), fetch("/weather")])
usd = json.parse(rates["body"]).dig("rates", "RUB")
t = json.parse(weather["body"])["temp"]
bot.say("Dollar: ${usd} ₽, in Kazan ${t}°")
text
Dollar: 92.5 ₽, in Kazan -3.4°

Up to 5 tasks at once. One more shared count: at most 10 requests per visitor message across all the scenario's blocks (HTTP request blocks and bot.http).