Notifications and alerting with Jennifer

Jennifer

One alert, four channels: Gotify, Telegram™, Slack™ and Discord™, sent once when something breaks and once when it is fixed.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 20

What makes a good alert

2 / 20

Four decks, four ways to deliver

DeckSends toThe secretSuccess
@deckyard/gotify 0.4.0your Gotify serverapp token, a header200
@deckyard/telegram 0.4.0the Telegram Bot APIbot token, in the URL patha Message
@deckyard/slack 0.2.0an Incoming Webhookthe webhook URL200 ok
@deckyard/discord 0.2.0a channel webhookthe webhook URL204

The bundled modules gotify.j, telegram.j, slack.j and discord.j are deprecated since 0.26.0 and removed in 0.28.0: use the decks.

3 / 20

Set up

jvc add @deckyard/gotify
jvc add @deckyard/telegram
jvc add @deckyard/slack
jvc add @deckyard/discord
jvc install            # copies each deck into vendor/deckyard/<deck>/
import "slack.j" as slack;              # before: bundled, deprecated
import "@deckyard/slack/" as slack;     # after: the deck
4 / 20

Gotify: the priority is the urgency

func priority(severity as string) {
    match ($severity) {
        when "critical" { return 8; }
        when "warning" { return 5; }
        else { return 2; }
    }
}
def g as gotify.Config init gotify.Config{url: os.getEnv("GOTIFY_URL"),
    token: os.getEnv("GOTIFY_TOKEN"), allowInsecureHttp: false};
def r as http.Response init gotify.push($g, $title, $text,
    priority($sev));
POST /gotify/message   X-Gotify-Key: AbCdEf123456789
  title=/srv is 96% full   priority=8
5 / 20

Telegram: chat ids and parse modes

def html as string init "<b>" + htmlEscape($a.title) + "</b>\n" +
    htmlEscape($a.text);     # &, < and > as entities
telegram.sendMessageWith($c.bot, $c.chatId, $html, "HTML");
  text=<b>/srv is 96% full</b>
       38 GiB free, limit 90%.
       Top: backup &lt;daily&gt;
  parse_mode=HTML
6 / 20

Slack: an Incoming Webhook and Block Kit

def m as slack.Message init slack.text(slack.message(), $a.title);
$m = slack.header($m, $a.title);
$m = slack.section($m, slack.escape($a.text));
$m = slack.contextBlock($m, "key `" + slack.escape($a.key) + "`");
def r as http.Response init slack.sendMessage($c.slackUrl, $m);
7 / 20

Discord: webhooks and embeds

def m as discord.Message init discord.noMentions(discord.message());
$m = discord.embed($m, $a.title, $a.text, colour($a.severity));
$m = discord.embedFooter($m, $a.key);
def r as http.Response init discord.sendMessage($c.discordUrl, $m);

Colours are decimal RGB: 15158332 is 0xE74C3C, red.

8 / 20

What Slack and Discord received

POST /slack/services/T000/B000/XXXX
{"text":"/srv is 96% full","blocks":[{"type":"header","text":
  {"type":"plain_text","text":"/srv is 96% full"}},{"type":"section",
  "text":{"type":"mrkdwn","text":"38 GiB free, limit 90%.\nTop: backup
  \u0026lt;daily\u0026gt;"}},{"type":"context","elements":[{"type":
  "mrkdwn","text":"key `disk:/srv`"}]}]}
POST /discord/api/webhooks/42/XXXX
{"embeds":[{"title":"/srv is 96% full","description":"38 GiB free,
  limit 90%.\nTop: backup \u003cdaily\u003e","color":15158332,
  "footer":{"text":"disk:/srv"}}],"allowed_mentions":{"parse":[]}}
9 / 20

One alert, routed by severity

export def struct Alert {
    key as string,        # names the problem: "disk:/srv"
    severity as string,   # "critical", "warning" or "ok"
    title as string,      # one line
    text as string        # the details
};
SeverityGotify priorityDiscord colourTelegram, Slack
critical815158332 redbold title, details
warning515105570 orangethe same
ok (resolved)23066993 greenthe same
10 / 20

Fan out: one broken channel must not stop the rest

for (def ch in enabled($c)) {     # in notify: the configured ones
    try {
        deliver($c, $a, $ch);
    } catch (e) {
        $failed[] = "{$ch}: [{$e.kind}] {$e.message}";
    }
}
gotify: [net] net.connect: 127.0.0.1:28799: dial tcp
  127.0.0.1:28799: connect: connection refused
telegram: [telegram] telegram: Unauthorized
slack: [slack] slack: the webhook URL must start with https://
discord: [alert] discord: HTTP 404
11 / 20

What each deck throws

gotify, slack, discordtelegram
2xxreturns the http.Responsereturns the Message
4xx, 5xx, 429returned too: check $r.statusthrows TelegramError
bad URLslack, discord: kind slack / discord-
networknet.NetError, kind netkind net, token removed

TelegramError: kind, message, errorCode, retryAfter, migrateToChatId, status. Match the kind, never the message.

12 / 20

Rate limits: 429 and Retry-After

for (def attempt in 1..MAX_TRIES + 1) {
    def got as list of int init sendOnce($c, $a, $ch); # status, wait
    $status = $got[0];
    if ($status >= 200 and $status < 300) {
        return;
    }
    if ($status != 429 or $attempt == MAX_TRIES) {
        break;
    }
    io.printf("%s: 429, retry in %d s\n", $ch, $got[1]); # <= 60
    time.sleep(time.fromSeconds($got[1]));
}
throw AlertError{...};     # kind "alert", the channel, the status
13 / 20

Telegram's flood control, in the same loop

try {                                   # in sendOnce
    toTelegram($c, $a);
    return [200, 0];
} catch (e) {
    if ($e.kind != "telegram" or $e.retryAfter == 0) {
        throw $e;
    }
    return [429, $e.retryAfter];
}
POST /tg/bot123456:TEST-TOKEN/sendMessage   -> 429
telegram: 429, retry in 2 s
POST /tg/bot123456:TEST-TOKEN/sendMessage
14 / 20

Keep the secrets out of the code

dotenv.autoload(os.cwd());   # .env, .env.local, .env.<JENNIFER_ENV>
def c as alerts.Channels init alerts.fromEnv();   # os.getEnv(...)
# .env.test: every service is the mock on 127.0.0.1:28790
SLACK_WEBHOOK_URL=http://127.0.0.1:28790/slack/services/T000/B000/XXXX
TELEGRAM_API=http://127.0.0.1:28790/tg
EnvironmentFile=/etc/diskcheck.env    # mode 0600; a real variable wins
15 / 20

A disk check that alerts once

def used as int init usedPercent($path);           # from df -P
def now as string init severity($used, $warn, $crit);
def before as string init stateOf($state, $key);   # "ok" if new
if ($now == $before) {
    exit 0;                               # nothing new: stay quiet
}
...                                       # build the Alert
def failed as list of string init alerts.notify($c, $a);
if (len($failed) == len(alerts.enabled($c))) {
    exit 1;                     # nobody heard it: retry next run
}
$state[$key] = $now;
saveState($state);                        # temp file, then rename
16 / 20

Four runs

$ jennifer run diskcheck.j / 80 90
/: 86% used, warning (was ok)
sent warning to 4 channel(s)
$ jennifer run diskcheck.j / 80 90
/: 86% used, warning (was warning)
$ jennifer run diskcheck.j / 80 85
/: 86% used, critical (was warning)
sent critical to 4 channel(s)
$ jennifer run diskcheck.j / 90 95
/: 86% used, ok (was critical)
sent ok to 4 channel(s)
17 / 20

Testing alerts against a mock server

while (true) { # lint-disable: L105
    def req as httpd.Request init httpd.accept($srv);
    def path as string init httpd.path($req);
    def svc as string init service($path);  # "slack" from /slack/...
    ...                         # log method, path, body; maybe a 429
    match ($svc) {
        when "discord" { httpd.respond($req, 204, ""); }
        ...                     # gotify, slack, tg (or 401), 404
    }
}
jennifer run mock.j 28790 &
JENNIFER_ENV=test MOCK_LIMIT=slack jennifer run once.j
18 / 20

The same idea as unit tests

func testSlack429IsRetried() {
    def srv as httpd.Server init httpd.listen("127.0.0.1:0");
    defer httpd.shutdown($srv);
    def t as task of string init spawn {
        answer($srv, 429, "1");       # Retry-After: 1
        return answer($srv, 200, "");
    };
    alerts.deliver(channels($srv), DISK, "slack");
    testing.assertTrue(strings.contains(task.wait($t), "96% full"));
}
PASS testCriticalIsPriorityEight (3 ms)
PASS testGivesUpAfterThreeTries (3 ms)
PASS testSlack429IsRetried (1002 ms)
19 / 20

Takeaways and read more

Based on Jennifer 0.27.0, November 2026.

20 / 20