Skip to content

feat: testing workflows #12

feat: testing workflows

feat: testing workflows #12

name: Telegram Notifier
on:
push:
branches: ["**"]
tags: ["*"]
pull_request:
types: [opened, reopened, synchronize, closed]
release:
types: [published]
# uncomment if you also want issues events
# issues:
# types: [opened, reopened, closed, edited, labeled, unlabeled]
workflow_dispatch:
inputs:
text:
description: "Message to send to Telegram"
required: true
chat_id:
description: "Optional: override TG_CHAT_ID (e.g. @YourChannel or numeric id)"
required: false
parse_mode:
description: "Telegram parse mode (plain|MarkdownV2|HTML)"
required: false
default: "HTML"
concurrency:
group: telegram-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: false
permissions:
contents: read
jobs:
notify:
runs-on: ubuntu-latest
environment: SANDBOX # uses TG_BOT_TOKEN and TG_CHAT_ID secrets
steps:
- name: Check out (for commit list)
uses: actions/checkout@v4
with:
fetch-depth: 50
- name: Build message (HTML)
id: msg
shell: bash
env:
REPO: ${{ github.repository }}
ACTOR: ${{ github.actor }}
EVENT: ${{ github.event_name }}
REF_NAME: ${{ github.ref_name }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
GIT_BEFORE: ${{ github.event.before }}
GIT_SHA: ${{ github.sha }}
PR_NUMBER: ${{ 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 }}
REL_TAG: ${{ github.event.release.tag_name }}
REL_NAME: ${{ github.event.release.name }}
REL_URL: ${{ github.event.release.html_url }}
ISSUE_NUM: ${{ github.event.issue.number }}
ISSUE_TITLE: ${{ github.event.issue.title }}
ISSUE_URL: ${{ github.event.issue.html_url }}
ISSUE_ACTION: ${{ github.event.action }}
DISPATCH_TEXT: ${{ github.event.inputs.text }}
run: |
set -euo pipefail
# minimal HTML escaper for Telegram
esc() { sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g'; }
header() {
printf '<b>📣 %s</b>\nRepo: <a href="%s">%s</a>\nBy: %s\nRef: <code>%s</code>\nRun: <a href="%s">view run</a>\n\n' \
"$(printf '%s' "$1" | esc)" "$REPO_URL" "$REPO" \
"$(printf '%s' "$ACTOR" | esc)" "$(printf '%s' "$REF_NAME" | esc)" "$RUN_URL"
}
MESSAGE=""
if [[ "$EVENT" == "workflow_dispatch" ]]; then
MESSAGE="$(header "Manual notification")${DISPATCH_TEXT:+$'\n'${DISPATCH_TEXT}}"
elif [[ "$EVENT" == "push" ]]; then
BEFORE="${GIT_BEFORE:-}"
AFTER="$GIT_SHA"
mapfile -t LINES < <(
if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then
git log -n 10 --pretty=format:'%s%x1f%h%x1f%an%x1f%H' "$AFTER" || true
else
git log --pretty=format:'%s%x1f%h%x1f%an%x1f%H' "$BEFORE..$AFTER" | head -n 10 || true
fi
)
COMMITS_HTML=""
if [[ ${#LINES[@]} -eq 0 ]]; then
COMMITS_HTML="- (no commit messages found)"
else
COMMIT_BASE="${REPO_URL}/commit"
for row in "${LINES[@]}"; do
IFS=$'\x1f' read -r subj short author full <<<"$row"
subj_esc="$(printf '%s' "$subj" | esc)"
author_esc="$(printf '%s' "$author" | esc)"
COMMITS_HTML+="- <a href=\"${COMMIT_BASE}/${full}\">${subj_esc}</a> (<code>${short}</code>) by ${author_esc}\n"
done
fi
MESSAGE="$(header "New push detected")Last changes:\n${COMMITS_HTML}\n\nRepo: <a href=\"${REPO_URL}\">${REPO_URL}</a>"
elif [[ "$EVENT" == "pull_request" ]]; then
if [[ "$PR_ACTION" == "closed" ]]; then
[[ "${PR_MERGED}" == "true" ]] && STATUS="PR merged ✅" || STATUS="PR closed ❌"
else
STATUS="PR updated ✏️"
fi
MESSAGE="$(header "$STATUS")#${PR_NUMBER}: $(printf '%s' "$PR_TITLE" | esc)\n${PR_URL}"
elif [[ "$EVENT" == "release" ]]; then
NAME="${REL_NAME:-$REL_TAG}"
MESSAGE="$(header "Release published 🏷️")Tag: $(printf '%s' "$REL_TAG" | esc)\nName: $(printf '%s' "$NAME" | esc)\n${REL_URL}"
elif [[ "$EVENT" == "issues" ]]; then
TITLE_ESC="$(printf '%s' "$ISSUE_TITLE" | esc)"
case "$ISSUE_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
MESSAGE="$(header "$STATUS")#${ISSUE_NUM}: ${TITLE_ESC}\n${ISSUE_URL}"
else
MESSAGE="$(header "Event: $EVENT")"
fi
{
echo "text<<EOF"
echo "$MESSAGE"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Send to Telegram
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 }}
PARSE_MODE: ${{ github.event.inputs.parse_mode }}
run: |
set -euo pipefail
CHAT_ID="${OVERRIDE_CHAT_ID:-$TG_CHAT_ID_DEFAULT}"
if [[ -z "${TG_BOT_TOKEN:-}" || -z "${CHAT_ID:-}" ]]; then
echo "Telegram secrets missing (TG_BOT_TOKEN or TG_CHAT_ID)." >&2
exit 1
fi
case "${PARSE_MODE:-HTML}" in
MarkdownV2|HTML) PMODE="$PARSE_MODE" ;;
*) PMODE="" ;;
esac
if [[ -n "$PMODE" ]]; then
PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" --arg pm "$PMODE" \
'{chat_id:$chat_id, text:$text, parse_mode:$pm, disable_web_page_preview:true}')"
else
PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" \
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')"
fi
curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
-H 'Content-Type: application/json' \
-d "$PAYLOAD" | jq -e '.ok == true' >/dev/null
echo "✅ Message sent to Telegram."