Writing Actions
Actions are written in JavaScript using the in‑app editor. Code runs in a sandboxed environment with autocompletion for Fostrom resources and built‑in linting that guides correct usage of the APIs.
Editor basics
Use the editor’s autocomplete (Ctrl+Space) to insert devices, tags, packet schemas, and webhooks directly into your code. If a dropdown is empty, you need to create that resource first (for example, add a tag or webhook in settings) before the corresponding function call will work. Linting validates arguments as you type; for instance, entering a non‑existent tag will surface an inline error like “not a valid tag name,”.
Runtime-provided values
Your action receives the triggering message payload in the form of a convenient alias:
payload: the message payload shaped by your selected Packet Schema.- use
payload.<field_name>to access values from the message payload
Example:
console.log("Checking whether Grav Mod needs replacement.")
if (payload.replace_grav_mod) {
console.warn("Replacing Gravity Mod...")
// Your effects go here
}
console.log("Hello World")
Logging
Use the standard console functions:
console.debug(),console.log(),console.warn()andconsole.error()- They will show up in the test logs as well as execution logs so you can track things easily.
This helps you keep routine debug output out of production logs while retaining important warnings and errors.
Effects with fostrom.*
You invoke side effects through the fostrom. APIs. Each call is type‑guided by the editor and expects you to select real resources from the dropdowns.
fostrom.sendMail(device, packetSchema, payload)
device: select a specific device from the dropdownpacketSchema: select a Message Schema from the dropdownpayload: an object that conforms to the schema fields.
Example:
fostrom.sendMail(spacecraft_1_life_support, set_grav_mod, { replace: true })
fostrom.broadcastMail(tag, packetSchema, payload)
tag: select a device tag (the editor validates tag names) from the dropdown.packetSchema: select a Message Schema from the dropdown.payload: an object matching the schema.
Example:
fostrom.broadcastMail(maintenance, set_grav_mod, { replace: true })
fostrom.triggerWebhook(webhook, body)
- webhook: select a pre-configured webhook from the dropdown.
- body: an object or string. The expected shape depends on the webhook’s content type.
Example:
fostrom.triggerWebhook(simple_wh, { event: "grav_mod_replace", at: Date.now() })
Putting it together
This sketch mirrors what you see in the screenshots: check a payload flag, log status, send to targeted devices, and broadcast to a tag.
console.log("Checking whether Grav Mod needs replacement.")
if (payload.replace_grav_mod) {
console.warn("Replacing Gravity Mod...")
// Trigger a webhook if needed
// fostrom.triggerWebhook(simple_wh, `Triggering Grav Mod Replacement at ${Date.now()}`)
// Send targeted device messages
fostrom.sendMail(grav_mod_replacer, set_grav_mod, { replace: true })
fostrom.sendMail(spacecraft_1_life_support, set_grav_mod, { replace: true })
}
// Fan out to a tagged group
fostrom.broadcastMail(maintenance, set_grav_mod, { replace: true })
Notes and tips
- Always pick devices, tags, packet schemas, and webhooks from the editor’s dropdowns. Free‑typed values that don’t match known resources will lint‑fail and also fail action-test.
- Payload objects must match the selected Packet Schema’s fields and types.