Skip to content

Fix GitHub Actions versions and add configurable Telegram group send gate - #143

Open
Sanix-Darker wants to merge 3 commits into
mainfrom
chore/telegram-send-gate-fix
Open

Fix GitHub Actions versions and add configurable Telegram group send gate#143
Sanix-Darker wants to merge 3 commits into
mainfrom
chore/telegram-send-gate-fix

Conversation

@Sanix-Darker

@Sanix-Darker Sanix-Darker commented Aug 2, 2026

Copy link
Copy Markdown
Member

Summary

  • Fixed deprecated/invalid workflow action references (actions/checkout@v4, actions/setup-python@v5, and a broken absolute path in notify_on_broken_link_detected workflow).
  • Prevented report_news from committing/pushing when no hash changes are present.
  • Added configurable Telegram group send gate (.github/workflows/telegram_send_gate.sh) and wired it into all group-targeted report workflows.
  • Added docs for launching and configuring the gate via repository variables.

Checks

  • workflow YAML loaded with python -c yaml.safe_load against all workflows in the repo
  • shell script syntax checks (bash -n) on changed shell files

@Sanix-Darker Sanix-Darker self-assigned this Aug 2, 2026
@Sanix-Darker
Sanix-Darker requested a review from elhmn August 2, 2026 12:37
Comment on lines +1 to +134
#!/usr/bin/env bash
set -euo pipefail

log() {
echo "[telegram-send-gate] $*"
}

# Defaults (can be overridden per workflow)
: "${TELEGRAM_SEND_GATE_ENABLED:=true}"
: "${TELEGRAM_SEND_GATE_LOOKBACK_MESSAGES:=5}"
: "${TELEGRAM_SEND_GATE_MIN_HUMAN_MESSAGES:=1}"
: "${TELEGRAM_SEND_GATE_ON_ERROR:=skip}"

write_outputs() {
local should_send=$1
local reason=$2
local human_count=$3
local sampled_count=$4

if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
{
echo "telegram_send_gate=${should_send}"
echo "telegram_send_gate_reason=${reason}"
echo "telegram_send_gate_human_messages=${human_count}"
echo "telegram_send_gate_sampled_messages=${sampled_count}"
} >> "$GITHUB_OUTPUT"
fi
}

skip_send() {
local reason=$1
local human_count=${2:-0}
local sampled_count=${3:-0}

write_outputs false "$reason" "$human_count" "$sampled_count"
exit 0
}

if [[ "${TELEGRAM_SEND_GATE_ENABLED,,}" != "true" ]]; then
log "Send gate disabled; allowing send."
write_outputs true disabled 0 0
exit 0
fi

if [[ -z "${TELEGRAM_CHAT_ID:-}" ]]; then
log "Missing TELEGRAM_CHAT_ID."
skip_send missing_chat_id
fi

if [[ -z "${TELEGRAM_BOT_TOKEN:-}" ]]; then
log "Missing TELEGRAM_BOT_TOKEN."
skip_send missing_bot_token
fi

if ! command -v jq >/dev/null 2>&1; then
log "jq is required for send gate logic and is not available."
if [[ "${TELEGRAM_SEND_GATE_ON_ERROR,,}" == "allow" ]]; then
write_outputs true missing_dependency_jq_allow 0 0
exit 0
fi

skip_send missing_dependency_jq_skip
fi

if ! [[ "$TELEGRAM_SEND_GATE_LOOKBACK_MESSAGES" =~ ^[0-9]+$ ]] || (( TELEGRAM_SEND_GATE_LOOKBACK_MESSAGES == 0 )); then
log "Invalid TELEGRAM_SEND_GATE_LOOKBACK_MESSAGES: '$TELEGRAM_SEND_GATE_LOOKBACK_MESSAGES'."
skip_send invalid_lookback
fi

if ! [[ "$TELEGRAM_SEND_GATE_MIN_HUMAN_MESSAGES" =~ ^[0-9]+$ ]]; then
log "Invalid TELEGRAM_SEND_GATE_MIN_HUMAN_MESSAGES: '$TELEGRAM_SEND_GATE_MIN_HUMAN_MESSAGES'."
skip_send invalid_min_human
fi

API_BASE="https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}"
set +e
RESP=$(curl -sS --max-time 30 --retry 2 --retry-delay 2 "${API_BASE}/getUpdates?limit=100&timeout=0")
CURL_CODE=$?
set -e
if (( CURL_CODE != 0 )); then
log "Telegram getUpdates call failed with exit code $CURL_CODE."
if [[ "${TELEGRAM_SEND_GATE_ON_ERROR,,}" == "allow" ]]; then
write_outputs true api_curl_error_allow 0 0
exit 0
fi
skip_send api_curl_error_skip
fi

if [[ -z "$RESP" ]]; then
log "Telegram API returned empty response."
if [[ "${TELEGRAM_SEND_GATE_ON_ERROR,,}" == "allow" ]]; then
write_outputs true api_empty_allow 0 0
exit 0
fi

skip_send api_empty_skip
fi

if ! echo "$RESP" | jq -e '.ok == true' >/dev/null 2>&1; then
DESCRIPTION=$(echo "$RESP" | jq -r '.description // "unknown"')
log "Telegram API error: $DESCRIPTION"
if [[ "${TELEGRAM_SEND_GATE_ON_ERROR,,}" == "allow" ]]; then
write_outputs true api_error_allow 0 0
exit 0
fi

skip_send api_error_skip
fi

CHAT_MESSAGES=$(echo "$RESP" | jq --arg chat_id "$TELEGRAM_CHAT_ID" --argjson lookback "$TELEGRAM_SEND_GATE_LOOKBACK_MESSAGES" '
.result
| map(
if has("message") then .message
elif has("channel_post") then .channel_post
else empty
end
)
| map(select((.chat.id|tostring) == $chat_id))
| sort_by(.date)
| reverse
| .[:$lookback]
')

SAMPLED_COUNT=$(echo "$CHAT_MESSAGES" | jq 'length')
HUMAN_COUNT=$(echo "$CHAT_MESSAGES" | jq '[.[] | select(.from and (.from.is_bot == false))] | length')

if (( HUMAN_COUNT >= TELEGRAM_SEND_GATE_MIN_HUMAN_MESSAGES )); then
log "Allowed. Checked ${SAMPLED_COUNT} recent update(s), found ${HUMAN_COUNT} human message(s)."
write_outputs true human_presence_ok "$HUMAN_COUNT" "$SAMPLED_COUNT"
exit 0
fi

log "Blocked. Checked ${SAMPLED_COUNT} recent update(s), found ${HUMAN_COUNT} human message(s) (minimum required: ${TELEGRAM_SEND_GATE_MIN_HUMAN_MESSAGES})."
skip_send human_presence_low "$HUMAN_COUNT" "$SAMPLED_COUNT"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sanix-Darker

What do you think about make this script executed as a standalone GitHub action?

Like that, the boy will depends on this action to be executed or not.

It will help to keep the other actions clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants