#!/bin/sh # # tg-ask -- push an approval request to a Telegram chat. # # Copyright (c) 2026 Konrad Lother # SPDX-License-Identifier: BSD-3-Clause # # Called by the ack mechanism as one of the channels: # # SPLINTER_ACK_CHANNELS="$HOME/.splinter/bin/tg-ask" # # It sends and returns; it never waits. The answer comes back through the # gateway, which is the one reader of that chat and drops the answer file when it # sees "ok " or "no ". Two pollers on one chat would eat each other's # updates - that is why this script only writes. # # SPLINTER_TELEGRAM_TOKEN bot token (else ~/.splinter/telegram.token) # SPLINTER_ACK_CHAT chat id of the parent # set -u msg="${1:-Zustimmung angefordert}" token="${SPLINTER_TELEGRAM_TOKEN:-}" # the same file the gateway reads; the old key_env lookup matched a line that # splinter.conf does not allow, and fed it to eval if [ -z "$token" ] && [ -r "${HOME}/.splinter/telegram.token" ]; then token=$(head -n 1 "${HOME}/.splinter/telegram.token") fi chat="${SPLINTER_ACK_CHAT:-}" if [ -z "$chat" ]; then echo "tg-ask: SPLINTER_ACK_CHAT is not set" >&2 exit 1 fi if [ -z "$token" ]; then echo "tg-ask: no bot token (SPLINTER_TELEGRAM_TOKEN)" >&2 exit 1 fi # The message carries the id, so the reply can name it. # The URL carries the token, and argv is readable by every user (ps -ef): curl # gets the URL as a config on stdin instead. A token has no quotes or # backslashes; one that does is refused rather than escaped. case "$token" in *'"'*|*\\*) echo "tg-ask: the token contains a quote or backslash" >&2; exit 1 ;; esac out=$(printf 'url = "https://api.telegram.org/bot%s/sendMessage"\n' "$token" | curl -sS --max-time 15 \ --data-urlencode "chat_id=${chat}" \ --data-urlencode "text=${msg}" \ -K - 2>&1) || { echo "tg-ask: curl failed: ${out}" >&2 exit 1 } case "$out" in *'"ok":true'*) printf 'tg-ask: sent (%s)\n' "$msg" ;; *) printf 'tg-ask: telegram rejected: %s\n' "$out" >&2; exit 1 ;; esac