Skip to content

Commit 0723b92

Browse files
committed
feat: testing workflows for telegram notify
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5df32ac commit 0723b92

File tree

1 file changed

+85
-88
lines changed

1 file changed

+85
-88
lines changed
Lines changed: 85 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
name: Telegram Notifier
22

3-
permissions:
4-
contents: read
5-
pull-requests: read # optional but harmless for PR events
6-
73
on:
84
push:
95
branches: ["**"]
@@ -18,10 +14,10 @@ on:
1814
description: "Message to send to Telegram"
1915
required: true
2016
chat_id:
21-
description: "Override TG_CHAT_ID (optional)"
17+
description: "Optional: override TG_CHAT_ID (e.g. @YourChannel or numeric id)"
2218
required: false
2319
parse_mode:
24-
description: "plain | MarkdownV2 | HTML"
20+
description: "Telegram parse mode (plain|MarkdownV2|HTML)"
2521
required: false
2622
default: "plain"
2723

@@ -35,13 +31,14 @@ jobs:
3531
environment: SANDBOX
3632

3733
steps:
38-
- name: Checkout (for commit list)
34+
- name: Check out (for commit list)
3935
uses: actions/checkout@v4
4036
with:
4137
fetch-depth: 0
4238

4339
- name: Build message
4440
id: msg
41+
shell: bash
4542
env:
4643
REPO: ${{ github.repository }}
4744
ACTOR: ${{ github.actor }}
@@ -65,87 +62,87 @@ jobs:
6562

6663
DISPATCH_TEXT: ${{ github.event.inputs.text }}
6764
run: |
68-
bash <<'BASH'
6965
set -euo pipefail
7066
7167
msg_header() {
72-
cat <<TXT
73-
📣 $1
74-
Repo: $REPO
75-
By: $ACTOR
76-
Ref: $REF_NAME
77-
Run: $RUN_URL
78-
TXT
79-
}
80-
81-
if [[ "${EVENT}" == "workflow_dispatch" ]]; then
82-
MESSAGE="$(msg_header "Manual notification")"$'\n\n'"${DISPATCH_TEXT:-}"
83-
84-
elif [[ "${EVENT}" == "push" ]]; then
85-
BEFORE="${GIT_BEFORE:-}"
86-
AFTER="${GIT_SHA}"
87-
if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then
88-
COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$AFTER" -n 10 || true)"
89-
else
90-
COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$BEFORE..$AFTER" | head -n 10 || true)"
91-
fi
92-
[[ -z "${COMMITS:-}" ]] && COMMITS="- (no commit messages found)"
93-
MESSAGE="$(msg_header "New push detected")"$'\n'"Last changes:"$'\n'"$COMMITS"$'\n\n'"Repo: $REPO_URL"
94-
95-
elif [[ "${EVENT}" == "pull_request" ]]; then
96-
if [[ "${PR_ACTION}" == "closed" ]]; then
97-
if [[ "${PR_MERGED}" == "true" ]]; then STATUS="PR merged ✅"; else STATUS="PR closed ❌"; fi
98-
else
99-
STATUS="PR updated ✏️"
100-
fi
101-
MESSAGE="$(msg_header "$STATUS")"$'\n'"#${PR_NUMBER}: ${PR_TITLE}"$'\n'"${PR_URL}"
102-
103-
elif [[ "${EVENT}" == "release" ]]; then
104-
NAME="${REL_NAME:-$REL_TAG}"
105-
MESSAGE="$(msg_header "Release published 🏷️")"$'\n'"Tag: ${REL_TAG}"$'\n'"Name: ${NAME}"$'\n'"${REL_URL}"
106-
107-
else
108-
MESSAGE="$(msg_header "Event: ${EVENT}")"
109-
fi
110-
111-
{
112-
echo "text<<EOF"
113-
echo "$MESSAGE"
114-
echo "EOF"
115-
} >> "$GITHUB_OUTPUT"
116-
BASH
117-
118-
- name: Send to Telegram
119-
env:
120-
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
121-
TG_CHAT_ID_DEFAULT: ${{ secrets.TG_CHAT_ID }}
122-
TEXT: ${{ steps.msg.outputs.text }}
123-
OVERRIDE_CHAT_ID: ${{ github.event.inputs.chat_id }}
124-
PARSE_MODE: ${{ github.event.inputs.parse_mode }}
125-
run: |
126-
set -euo pipefail
127-
CHAT_ID="${OVERRIDE_CHAT_ID:-$TG_CHAT_ID_DEFAULT}"
128-
if [[ -z "$TG_BOT_TOKEN" || -z "$CHAT_ID" ]]; then
129-
echo "Telegram secrets missing (TG_BOT_TOKEN or TG_CHAT_ID)." >&2
130-
exit 1
131-
fi
132-
133-
case "${PARSE_MODE:-plain}" in
134-
MarkdownV2|HTML) PMODE="$PARSE_MODE" ;;
135-
*) PMODE="" ;;
136-
esac
137-
138-
if [[ -n "$PMODE" ]]; then
139-
PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" --arg pm "$PMODE" \
140-
'{chat_id:$chat_id, text:$text, parse_mode:$pm, disable_web_page_preview:true}')"
141-
else
142-
PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" \
143-
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')"
144-
fi
145-
146-
curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
147-
-H 'Content-Type: application/json' \
148-
-d "$PAYLOAD" \
149-
| jq -e '.ok == true' >/dev/null
150-
151-
echo "✅ Message sent to Telegram."
68+
printf '📣 %s\nRepo: %s\nBy: %s\nRef: %s\nRun: %s\n' \
69+
"$1" "$REPO" "$ACTOR" "$REF_NAME" "$RUN_URL"
70+
}
71+
72+
if [[ "$EVENT" == "workflow_dispatch" ]]; then
73+
MESSAGE="$(msg_header "Manual notification")"$'\n\n'"${DISPATCH_TEXT:-}"
74+
75+
elif [[ "$EVENT" == "push" ]]; then
76+
BEFORE="${GIT_BEFORE:-}"
77+
AFTER="$GIT_SHA"
78+
if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then
79+
COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$AFTER" -n 10 || true)"
80+
else
81+
COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$BEFORE..$AFTER" | head -n 10 || true)"
82+
fi
83+
[[ -z "${COMMITS:-}" ]] && COMMITS="- (no commit messages found)"
84+
85+
MESSAGE="$(msg_header "New push detected")"$'\n'"Last changes:"$'\n'"$COMMITS"$'\n\n'"Repo: $REPO_URL"
86+
87+
elif [[ "$EVENT" == "pull_request" ]]; then
88+
if [[ "$PR_ACTION" == "closed" ]]; then
89+
if [[ "${PR_MERGED}" == "true" ]]; then
90+
STATUS="PR merged ✅"
91+
else
92+
STATUS="PR closed ❌"
93+
fi
94+
else
95+
STATUS="PR updated ✏️"
96+
fi
97+
MESSAGE="$(msg_header "$STATUS")"$'\n'"#${PR_NUMBER}: ${PR_TITLE}"$'\n'"${PR_URL}"
98+
99+
elif [[ "$EVENT" == "release" ]]; then
100+
NAME="${REL_NAME:-$REL_TAG}"
101+
MESSAGE="$(msg_header "Release published 🏷️")"$'\n'"Tag: ${REL_TAG}"$'\n'"Name: ${NAME}"$'\n'"${REL_URL}"
102+
103+
else
104+
MESSAGE="$(msg_header "Event: $EVENT")"
105+
fi
106+
107+
{
108+
echo "text<<'EOF'"
109+
echo "$MESSAGE"
110+
echo "EOF"
111+
} >> "$GITHUB_OUTPUT"
112+
113+
- name: Send to Telegram
114+
shell: bash
115+
env:
116+
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
117+
TG_CHAT_ID_DEFAULT: ${{ secrets.TG_CHAT_ID }}
118+
TEXT: ${{ steps.msg.outputs.text }}
119+
OVERRIDE_CHAT_ID: ${{ github.event.inputs.chat_id }}
120+
PARSE_MODE: ${{ github.event.inputs.parse_mode }}
121+
run: |
122+
set -euo pipefail
123+
124+
CHAT_ID="${OVERRIDE_CHAT_ID:-$TG_CHAT_ID_DEFAULT}"
125+
if [[ -z "${TG_BOT_TOKEN:-}" || -z "${CHAT_ID:-}" ]]; then
126+
echo "Telegram secrets missing (TG_BOT_TOKEN or TG_CHAT_ID)." >&2
127+
exit 1
128+
fi
129+
130+
case "${PARSE_MODE:-plain}" in
131+
MarkdownV2|HTML) PMODE="$PARSE_MODE" ;;
132+
*) PMODE="" ;;
133+
esac
134+
135+
if [[ -n "$PMODE" ]]; then
136+
PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" --arg pm "$PMODE" \
137+
'{chat_id:$chat_id, text:$text, parse_mode:$pm, disable_web_page_preview:true}')"
138+
else
139+
PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" \
140+
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')"
141+
fi
142+
143+
curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
144+
-H 'Content-Type: application/json' \
145+
-d "$PAYLOAD" \
146+
| jq -e '.ok == true' >/dev/null
147+
148+
echo "✅ Message sent to Telegram."

0 commit comments

Comments
 (0)