The Skript book · Chapter 4

Events: when Something Happens

Every real Skript program is a set of rules, and every rule has the same shape: a when line naming an event, and an indented body saying what to do about it. This is the fundamental unit of the language — everything else in this book happens inside one.

when a new lead arrives
  log "A lead! Their name is {name}."
output — real, executed at build time
▶ example.skript — Skode Skript · local simulation

  ● event: lead.created                → name: Ada Lovelace · phone: +1 555 0100 · email: [email protected]
  • log                                → A lead! Their name is Ada Lovelace.

  ✔ done in 2.1ms

The when line is written as a phrase, not a code, and the interpreter is deliberately generous about phrasing: when a new lead arrives, when a lead is created, and when a lead comes in all mean the same event. Under the hood each phrase resolves to an event key — this one is lead.created — which is what the trace shows, so you always know what your phrase actually matched.

The built-in events

Skript knows five families of business events out of the box:

You write (for example)Event keyThe event's subject
when a new lead arriveslead.createda lead
when a form is submittedform.submitteda submission
when a message arrivesmessage.receiveda message
when an invoice is paidinvoice.paidan invoice
when a deal is wondeal.wona deal

Each event carries fields — the data of the thing that happened — and your program reads them by name, exactly like the variables of the previous chapter. In the simulator, each event comes with realistic demo data so you have something to work with:

when an invoice is paid
  log "Invoice {number} for {amount} was paid by {name}"
output — real, executed at build time
▶ example.skript — Skode Skript · local simulation

  ● event: invoice.paid                → number: INV-0461 · amount: 990 · name: Margaret H
  • log                                → Invoice INV-0461 for 990 was paid by Margaret H

  ✔ done in 2.1ms

The demo invoice has a number, an amount and a name; the demo lead has name, phone, email, source and budget; the demo form submission has form, name, email and message. You will discover the fields naturally — the event line at the top of every trace lists the first few — and nothing bad happens if you interpolate a field that is not there: the braces stay visible, telling you exactly which name found no value.

Custom events

The five built-ins cover the common ground, but any part of Skode — or any of your own integrations — can emit events with custom names. Quote the name and the rule matches it verbatim:

when "billing.overdue" happens
  log "{name} is {days} days overdue"
output — real, executed at build time
▶ example.skript — Skode Skript · local simulation

  ● event: billing.overdue             → name: Acme · days: 12
  • log                                → Acme is 12 days overdue

  ✔ done in 2.1ms

Several rules in one file

A file may hold as many rules as you like, and this is where the --event option earns its keep. When the interpreter runs a file it fires every rule (each against its own simulated event), unless you narrow it:

when a new lead arrives
  log "lead rule ran"

when a deal is won
  log "deal rule ran — {name} closed for {amount}"
output — real, executed at build time
▶ example.skript — Skode Skript · local simulation

  ● event: deal.won                    → name: Skylark Corp · amount: 14000 · owner: Maya
  • log                                → deal rule ran — Skylark Corp closed for 14000

  ✔ done in 2.1ms

Only the deal rule ran, because we asked for deal.won. In production the narrowing is automatic — a real event fires exactly the rules that match it — so --event is purely a testing convenience.

Shaping the test data

The demo data is fine for a first run, but real testing means trying your data. --data takes a snippet of JSON and lays it over the demo event — fields you provide replace the demo's, fields you omit keep their demo values:

when a new lead arrives
  log "{name} has a budget of {budget}"
output — real, executed at build time
▶ example.skript — Skode Skript · local simulation

  ● event: lead.created                → name: Meera · phone: +1 555 0100 · email: [email protected]
  • log                                → Meera has a budget of 150

  ✔ done in 2.1ms

Between --event, --data, and (as you will see in Chapter 12) --score, you can steer a skript into any branch you need to look at, deterministically, from the command line. Chapter 14 turns that into a testing habit.

A note on it

Inside a rule, the word it refers to the event's subject — the lead, the invoice, the message. You have already seen the idiom score it with ai; you will meet assign it to "Maya" later. it keeps sentences flowing where repeating the noun would be stilted. When you interpolate, {it} resolves to the subject's name if it has one.

That is the stage set: events arrive, rules wake up, fields carry the facts. The next three chapters are about what your rules can know — variables, types, and lists — and then we return to what they can do.