-
Notifications
You must be signed in to change notification settings - Fork 3
181 lines (156 loc) · 6.77 KB
/
telegram-notify.yml
File metadata and controls
181 lines (156 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
name: Telegram Notifier
on:
push:
branches: ["**"]
tags: ["*"]
pull_request:
types: [opened, reopened, synchronize, closed]
release:
types: [published]
# 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. @Channel or numeric id)"
required: false
concurrency:
group: telegram-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: false
permissions:
contents: read
jobs:
notify:
runs-on: ubuntu-latest
environment: SANDBOX # TG_BOT_TOKEN + TG_CHAT_ID
steps:
- name: Checkout (for commit list)
uses: actions/checkout@v4
with:
fetch-depth: 50
- name: Build message (MarkdownV2)
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
# MarkdownV2 escaper for Telegram
mde() {
sed -e 's/\\/\\\\/g' \
-e 's/_/\\_/g' -e 's/*/\\*/g' -e 's/\[/\\[/g' -e 's/]/\\]/g' \
-e 's/(/\\(/g' -e 's/)/\\)/g' -e 's/~/\\~/g' -e 's/`/\\`/g' \
-e 's/>/\\>/g' -e 's/#/\\#/g' -e 's/\\+/\\\\+/g' \
-e 's/-/\\-/g' -e 's/=/\\=/g' -e 's/|/\\|/g' -e 's/{/\\{/g' -e 's/}/\\}/g' \
-e 's/\./\\./g' -e 's/!/\\!/g'
}
header_plain() { # shared header for non-manual events
printf '📣 %s\nRepo: %s\nBy: %s\nRef: %s\nRun: %s\n' \
"$(printf '%s' "$1")" "$REPO" "$ACTOR" "$REF_NAME" "$RUN_URL"
}
MESSAGE=""
if [[ "$EVENT" == "workflow_dispatch" ]]; then
# --- Minimal manual notification (what you asked) ---
# 📣 Manual
# By: <actor>
# Run: <url>
# <your message>
MSG_ESC="$(printf '%s' "${DISPATCH_TEXT:-}" | mde)"
HEADER="📣 Manual\nBy: $(printf '%s' "$ACTOR" | mde)\nRun: $(printf '%s' "$RUN_URL" | mde)\n\n"
MESSAGE="${HEADER}${MSG_ESC}"
elif [[ "$EVENT" == "push" ]]; then
BEFORE="${GIT_BEFORE:-}"
AFTER="$GIT_SHA"
if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then
mapfile -t LINES < <(git log -n 10 --pretty=format:'%s%x1f%h%x1f%H' "$AFTER" || true)
else
mapfile -t LINES < <(git log --pretty=format:'%s%x1f%h%x1f%H' "$BEFORE..$AFTER" | head -n 10 || true)
fi
COMMITS_TXT=""
if [[ ${#LINES[@]} -eq 0 ]]; then
COMMITS_TXT="- (no commit messages found)"
else
ACTOR_URL="https://github.com/${ACTOR}"
for row in "${LINES[@]}"; do
IFS=$'\x1f' read -r subj short full <<<"$row"
subj_esc="$(printf '%s' "$subj" | mde)"
COMMITS_TXT+="- ${subj_esc} (${short}) by $(printf '%s' "$ACTOR_URL" | mde)\n"
done
fi
MESSAGE="$(header_plain 'Last 10 commits')"
MESSAGE="${MESSAGE}\n${COMMITS_TXT}\nRepo: ${REPO_URL}"
elif [[ "$EVENT" == "pull_request" ]]; then
if [[ "$PR_ACTION" == "closed" ]]; then
[[ "${PR_MERGED}" == "true" ]] && STATUS="PR merged ✅" || STATUS="PR closed ❌"
else
STATUS="PR updated ✏️"
fi
TITLE_ESC="$(printf '%s' "$PR_TITLE" | mde)"
MESSAGE="$(header_plain "$STATUS")\n#${PR_NUMBER}: ${TITLE_ESC}\n${PR_URL}"
elif [[ "$EVENT" == "release" ]]; then
NAME="${REL_NAME:-$REL_TAG}"
MESSAGE="$(header_plain 'Release published 🏷️')\nTag: $(printf '%s' "$REL_TAG" | mde)\nName: $(printf '%s' "$NAME" | mde)\n${REL_URL}"
elif [[ "$EVENT" == "issues" ]]; then
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
TITLE_ESC="$(printf '%s' "$ISSUE_TITLE" | mde)"
MESSAGE="$(header_plain "$STATUS")\n#${ISSUE_NUM}: ${TITLE_ESC}\n${ISSUE_URL}"
else
MESSAGE="$(header_plain "Event: $EVENT")"
fi
# write multi-line output safely
{
printf 'text<<MSGEOF\n'
printf '%s\n' "$MESSAGE"
printf 'MSGEOF\n'
} >> "$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 }}
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; }
# Send as MarkdownV2 (we already escaped)
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, parse_mode:"MarkdownV2", disable_web_page_preview:true}')" \
| jq -e '.ok == true' >/dev/null
echo "✅ Message sent to Telegram."