Skip to content

Telegram – Repo Notifications (Clean Text) #19

Telegram – Repo Notifications (Clean Text)

Telegram – Repo Notifications (Clean Text) #19

# .github/workflows/telegram-notifier.yml
name: Telegram Notifier
on:
pull_request:
types: [opened, reopened, synchronize, closed]
release:
types: [published]
issues:
types: [opened, reopened, closed, edited, labeled, unlabeled]
workflow_dispatch:
inputs:
title:
description: "Manual title (default: 📢 Manual)"
required: false
default: "📢 Manual"
text:
description: "Message body"
required: true
chat_id:
description: "Override TG_CHAT_ID (e.g. @Channel or numeric id)"
required: false
permissions:
contents: read
concurrency:
group: telegram-notify-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: false
jobs:
notify:
runs-on: ubuntu-latest
environment: SANDBOX
steps:
- name: Build message (plain text)
id: msg
shell: bash
env:
REPO: ${{ github.repository }}
ACTOR: ${{ github.actor }}
EVENT: ${{ github.event_name }}
REF: ${{ github.ref_name }}
RUN: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
# PR
PR_NUM: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_URL: ${{ github.event.pull_request.html_url }}
PR_ACTION: ${{ github.event.action }}
PR_MERGED: ${{ github.event.pull_request.merged }}
# Release
REL_TAG: ${{ github.event.release.tag_name }}
REL_NAME: ${{ github.event.release.name }}
REL_URL: ${{ github.event.release.html_url }}
# Issue
ISS_NUM: ${{ github.event.issue.number }}
ISS_TITLE: ${{ github.event.issue.title }}
ISS_URL: ${{ github.event.issue.html_url }}
# Manual
WD_TITLE: ${{ github.event.inputs.title }}
WD_TEXT: ${{ github.event.inputs.text }}
run: |
set -euo pipefail
header() { printf '%s\nRepo: %s\nBy: %s\nRef: %s\nRun: %s\n' "$1" "$REPO" "$ACTOR" "$REF" "$RUN"; }
case "$EVENT" in
workflow_dispatch)
TITLE="${WD_TITLE:-📢 Manual}"
MSG="$(header "$TITLE")"
[[ -n "${WD_TEXT:-}" ]] && MSG+=$'\n'"${WD_TEXT}"
;;
pull_request)
if [[ "${PR_ACTION}" == "closed" ]]; then
[[ "${PR_MERGED}" == "true" ]] && STATUS="PR merged ✅" || STATUS="PR closed ❌"
else
STATUS="PR updated ✏️"
fi
MSG="$(header "$STATUS")"$'\n'"#${PR_NUM}: ${PR_TITLE}"$'\n'"${PR_URL}"
;;
release)
NAME="${REL_NAME:-$REL_TAG}"
MSG="$(header 'Release published 🏷️')"$'\n'"Tag: ${REL_TAG}"$'\n'"Name: ${NAME}"$'\n'"${REL_URL}"
;;
issues)
case "${{ github.event.action }}" in
opened) STATUS="Issue opened 🐞" ;;
reopened) STATUS="Issue reopened ♻️" ;;
closed) STATUS="Issue closed ✅" ;;
edited) STATUS="Issue edited ✏️" ;;
labeled) STATUS="Issue labeled 🏷️" ;;
unlabeled) STATUS="Issue unlabeled 🏷️" ;;
*) STATUS="Issue update 📌" ;;
esac
MSG="$(header "$STATUS")"$'\n'"#${ISS_NUM}: ${ISS_TITLE}"$'\n'"${ISS_URL}"
;;
*)
MSG="$(header "Event: $EVENT")"
;;
esac
{
printf 'text<<MSGEOF\n'; printf '%s\n' "$MSG"; printf 'MSGEOF\n'
} >> "$GITHUB_OUTPUT"
- name: Send to Telegram (plain text)
shell: bash
env:
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
TG_CHAT_ID_DEFAULT: ${{ secrets.TG_CHAT_ID }}
TEXT: ${{ steps.msg.outputs.text }}
OVERRIDE_CHAT_ID: ${{ github.event.inputs.chat_id }}
run: |
set -euo pipefail
CHAT_ID="${OVERRIDE_CHAT_ID:-$TG_CHAT_ID_DEFAULT}"
[[ -n "${TG_BOT_TOKEN:-}" && -n "${CHAT_ID:-}" ]] || { echo "Missing TG secrets"; exit 1; }
RESP="$(curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
-H 'Content-Type: application/json' \
-d "$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" \
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')")"
echo "Telegram response: $RESP"
echo "$RESP" | jq -e '.ok == true' >/dev/null
echo "✅ Message sent."