about summary refs log tree commit diff
path: root/bundles/tgnotify
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-09-07 17:32:13 +0200
committerRobert Günzler <r@gnzler.io>2025-09-07 17:42:42 +0200
commit38be6c13f76e893cf47636257071b440dec0c5b2 (patch)
treef7ded3235a7e3362cbd1ed3d91523968fe0faf90 /bundles/tgnotify
initial commit
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'bundles/tgnotify')
-rwxr-xr-xbundles/tgnotify/files/tgnotify33
-rw-r--r--bundles/tgnotify/files/tgnotify.conf2
-rw-r--r--bundles/tgnotify/items.py17
-rw-r--r--bundles/tgnotify/metadata.py7
4 files changed, 59 insertions, 0 deletions
diff --git a/bundles/tgnotify/files/tgnotify b/bundles/tgnotify/files/tgnotify
new file mode 100755
index 0000000..44dd85d
--- /dev/null
+++ b/bundles/tgnotify/files/tgnotify
@@ -0,0 +1,33 @@
+#!/bin/sh
+set -eu
+
+# import environment from config
+# can overwrite above defaults
+set -a
+. /etc/tgnotify.conf || true
+set +a
+
+# fail if token or chatid are missing
+tg_bot_token=${TG_BOT_TOKEN:?}
+tg_chat_id=${TG_CHAT_ID:?}
+
+main() {
+        local tg_msg
+        local tg_curl_args="-sS -o /dev/null"
+
+        if [ "$1" = "-" ]; then
+                while read -r ln; do tg_msg="${tg_msg}%0A${ln}"; done
+        else
+                tg_msg=$(printf '%s' "$*" | sed -e 's,\\n,%0A,g')
+        fi
+
+        test -n "${DEBUG:-}" && tg_curl_args="-v -o /dev/stdout"
+        test -n "${DEBUG:-}" && set -x
+
+        exec curl ${tg_curl_args} \
+                -d "chat_id=${tg_chat_id}&disable_notification=false&disable_web_page_preview=true&parse_mode=markdown&text=${tg_msg}" \
+                -H "Content-Type: application/x-www-form-urlencoded" \
+                "https://api.telegram.org/bot${tg_bot_token}/sendMessage"
+}
+
+main "$@"
diff --git a/bundles/tgnotify/files/tgnotify.conf b/bundles/tgnotify/files/tgnotify.conf
new file mode 100644
index 0000000..040c2fc
--- /dev/null
+++ b/bundles/tgnotify/files/tgnotify.conf
@@ -0,0 +1,2 @@
+TG_BOT_TOKEN=${repo.vault.cmd(f"rbw get tgnotify/token")}
+TG_CHAT_ID=${repo.vault.cmd(f"rbw get tgnotify/chatid")}
diff --git a/bundles/tgnotify/items.py b/bundles/tgnotify/items.py
new file mode 100644
index 0000000..b428d54
--- /dev/null
+++ b/bundles/tgnotify/items.py
@@ -0,0 +1,17 @@
+if node.os != "alpine":
+    raise BundleError(f"{node.name}: OS {node.os} is not supported.")
+
+from os.path import join
+
+files = {
+    "/usr/bin/tgnotify": {
+        "source": "tgnotify",
+        "mode": "0755",
+    },
+    "/etc/tgnotify.conf": {
+        "source": "tgnotify.conf",
+        "content_type": "mako",
+        "mode": "0640",
+        "group": "wheel",
+    }
+}
diff --git a/bundles/tgnotify/metadata.py b/bundles/tgnotify/metadata.py
new file mode 100644
index 0000000..f1c6fe4
--- /dev/null
+++ b/bundles/tgnotify/metadata.py
@@ -0,0 +1,7 @@
+defaults = {
+    "apk": {
+        "packages": {
+            "curl": {},
+        },
+    },
+}