HTTP requests
bot.http(req) — a request to the internet, like the scenario's HTTP request block («HTTP-запрос»). Bot scripts have no http module.
include bot
r = bot.http({method: "POST", url: "https://…", headers: {"X-Key": "…"}, body: "…", timeout: 5})| Request field | |
|---|---|
url | required, http:// or https://; the public internet only |
method | GET (default), POST, PUT, PATCH, DELETE, in any case |
headers | a dict of strings; User-Agent: MorzeBot/1.0 is set by itself |
body | a string up to 64 KB; not sent with GET |
timeout | seconds, at most 5 (default 5) |
| Response field | |
|---|---|
ok | true for status 200–299 |
status | the response code; 0 — no response |
body | the 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
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.
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.
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")
}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:
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:
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"] # → 201Authorization
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 # → 64Several 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:
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}°")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).