88 types : [opened, reopened, synchronize, closed]
99 release :
1010 types : [published]
11- # 👇 Manual trigger from the Actions tab
1211 workflow_dispatch :
1312 inputs :
1413 text :
1514 description : " Message to send to Telegram"
1615 required : true
1716 chat_id :
18- description : " Optional: override TG_CHAT_ID"
17+ description : " Optional: override TG_CHAT_ID (e.g. @YourChannel or numeric id) "
1918 required : false
2019 parse_mode :
2120 description : " Telegram parse mode (plain|MarkdownV2|HTML)"
@@ -39,77 +38,72 @@ jobs:
3938
4039 - name : Build message
4140 id : msg
41+ env :
42+ # Context values (moved to env so the script is pure bash)
43+ REPO : ${{ github.repository }}
44+ ACTOR : ${{ github.actor }}
45+ EVENT : ${{ github.event_name }}
46+ REF_NAME : ${{ github.ref_name }}
47+ RUN_URL : ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
48+ REPO_URL : ${{ github.server_url }}/${{ github.repository }}
49+
50+ GIT_BEFORE : ${{ github.event.before }}
51+ GIT_SHA : ${{ github.sha }}
52+
53+ PR_NUMBER : ${{ github.event.pull_request.number }}
54+ PR_TITLE : ${{ github.event.pull_request.title }}
55+ PR_URL : ${{ github.event.pull_request.html_url }}
56+ PR_ACTION : ${{ github.event.action }}
57+ PR_MERGED : ${{ github.event.pull_request.merged }}
58+
59+ REL_TAG : ${{ github.event.release.tag_name }}
60+ REL_NAME : ${{ github.event.release.name }}
61+ REL_URL : ${{ github.event.release.html_url }}
62+
63+ DISPATCH_TEXT : ${{ github.event.inputs.text }}
4264 shell : bash
4365 run : |
4466 set -euo pipefail
4567
46- REPO="${{ github.repository }}"
47- ACTOR="${{ github.actor }}"
48- EVENT="${{ github.event_name }}"
49- RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
50- REPO_URL="${{ github.server_url }}/${{ github.repository }}"
51-
5268 msg_header() {
5369 echo "📣 $1
5470Repo : $REPO
5571By : $ACTOR
56- Ref : ${{ github.ref_name }}
72+ Ref : $REF_NAME
5773Run : $RUN_URL"
5874 }
5975
6076 if [[ "$EVENT" == "workflow_dispatch" ]]; then
61- # Manual test: use the provided text as-is
62- MESSAGE="$(msg_header "Manual notification")"
63- MESSAGE="$MESSAGE
64-
65- ${{ github.event.inputs.text }}"
66-
77+ MESSAGE="$(msg_header "Manual notification")"$'\n\n'"${DISPATCH_TEXT:-}"
6778 elif [[ "$EVENT" == "push" ]]; then
68- BEFORE="${{ github.event.before }}"
69- AFTER="${{ github.sha }}"
70-
71- if [[ "$BEFORE" =~ ^0+$ ]]; then
72- RANGE="$AFTER -n 10"
73- COMMITS="$(git log --pretty=format:'- %s (%h) by %an' $RANGE | head -n 10 || true)"
79+ BEFORE="${GIT_BEFORE:-}"
80+ AFTER="$GIT_SHA"
81+ if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then
82+ COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$AFTER" -n 10 || true)"
7483 else
7584 COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$BEFORE..$AFTER" | head -n 10 || true)"
7685 fi
7786 [[ -z "${COMMITS:-}" ]] && COMMITS="- (no commit messages found)"
7887
79- MESSAGE="$(msg_header "New push detected")"
80- MESSAGE="$MESSAGE
81- Last changes :
82- $COMMITS
83-
84- Repo : $REPO_URL"
85-
88+ MESSAGE="$(msg_header "New push detected")"$'\n'"Last changes:"$'\n'"$COMMITS"$'\n\n'"Repo : $REPO_URL"
8689 elif [[ "$EVENT" == "pull_request" ]]; then
87- if [[ "${{ github.event.action }} " == "closed" ]]; then
88- if [[ "${{ github.event.pull_request.merged }} " == "true" ]]; then
90+ if [[ "$PR_ACTION " == "closed" ]]; then
91+ if [[ "$PR_MERGED " == "true" ]]; then
8992 STATUS="PR merged ✅"
9093 else
9194 STATUS="PR closed ❌"
9295 fi
9396 else
9497 STATUS="PR updated ✏️"
9598 fi
96- MESSAGE="$(msg_header "$STATUS")"
97- MESSAGE="$MESSAGE
98- # ${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}
99- ${{ github.event.pull_request.html_url }}"
100-
99+ MESSAGE="$(msg_header "$STATUS")"$'\n'"#${PR_NUMBER}: ${PR_TITLE}"$'\n'"${PR_URL}"
101100 elif [[ "$EVENT" == "release" ]]; then
102- MESSAGE="$(msg_header "Release published 🏷️")"
103- MESSAGE="$MESSAGE
104- Tag : ${{ github.event.release.tag_name }}
105- Name : ${{ github.event.release.name || github.event.release.tag_name }}
106- ${{ github.event.release.html_url }}"
107-
101+ NAME="${REL_NAME:-$REL_TAG}"
102+ MESSAGE="$(msg_header "Release published 🏷️")"$'\n'"Tag : ${REL_TAG}"$'\n'"Name: ${NAME}"$'\n'"${REL_URL}"
108103 else
109- MESSAGE="$(msg_header "Event : $EVENT")"
104+ MESSAGE="$(msg_header "Event : $EVENT")"
110105 fi
111106
112- # Export the (safely) raw text for next step
113107 {
114108 echo "text<<EOF"
115109 echo "$MESSAGE"
@@ -126,20 +120,17 @@ MESSAGE="$(msg_header "Event: $EVENT")"
126120 run : |
127121 set -euo pipefail
128122
129- # Choose chat id (manual override if provided)
130123 CHAT_ID="${OVERRIDE_CHAT_ID:-$TG_CHAT_ID_DEFAULT}"
131124 if [[ -z "$TG_BOT_TOKEN" || -z "$CHAT_ID" ]]; then
132125 echo "Telegram secrets missing (TG_BOT_TOKEN or TG_CHAT_ID)." >&2
133126 exit 1
134127 fi
135128
136- # Parse mode handling
137129 case "${PARSE_MODE:-plain}" in
138130 MarkdownV2|HTML) PMODE="$PARSE_MODE" ;;
139- *) PMODE="" ;; # plain text
131+ *) PMODE="" ;;
140132 esac
141133
142- # Build payload with jq to escape safely
143134 if [[ -n "$PMODE" ]]; then
144135 PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" --arg pm "$PMODE" \
145136 '{chat_id:$chat_id, text:$text, parse_mode:$pm, disable_web_page_preview:true}')"
0 commit comments