Send a webhook or email
A Cell set keeps per-key counters and flags. When a flag turns on (ON RISE) or off (ON FALL), it can call a URL or send an email.
Webhook when an order ships
Keep the secret part of the URL out of the statement:
CREATE SECRET slack_path AS 'T000/B000/XXXX';
CREATE CELLS shipping ON orders KEY id (
shipped BIT ALL status = 'shipped',
items COUNT
)
ON RISE shipped NOTIFY 'https://hooks.slack.com/services/{{secret:slack_path}}'
BODY '{"text": "Order {{key}} shipped with {{items}} items."}';
The call is a JSON POST with a 5-second timeout. {{key}} is the key, {{items}} is the items cell. Without BODY, the body is a JSON object with the set, key, bit, edge and all cell values.
Email when an item runs out
Create a mailer once. With Google:
aios google auth alerts@example.com # sign in once, in a browser
CREATE MAILER alerts OAUTH 'google' ACCOUNT 'alerts@example.com' FROM_NAME 'Stock alerts';
CREATE CELLS stock ON items KEY sku (
out BIT ALL stock <= 0
)
ON RISE out MAIL alerts TO 'purchasing@example.com'
SUBJECT 'Out of stock: {{key}}' BODY 'SKU {{key}} is out of stock.';
For an SMTP server with a password, store the password as a secret and use CREATE MAILER m USER 'u' PASSWORD SECRET smtp_pw HOST 'smtp.example.com' FROM 'alerts@example.com'.
Check it
SHOW CELLS shipping ACTS; -- the last 64 calls and whether they worked
A failed call is tried again on that key's next change. More: Cells.
