-
Notifications
You must be signed in to change notification settings - Fork 3
156 lines (133 loc) · 4.36 KB
/
telegram-notify.yml
File metadata and controls
156 lines (133 loc) · 4.36 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
name: Telegram Notifier
on:
push:
branches: ["**"]
tags: ["*"]
pull_request:
types: [opened, reopened, synchronize, closed]
release:
types: [published]
# 👇 Manual trigger from the Actions tab
workflow_dispatch:
inputs:
text:
description: "Message to send to Telegram"
required: true
chat_id:
description: "Optional: override TG_CHAT_ID"
required: false
parse_mode:
description: "Telegram parse mode (plain|MarkdownV2|HTML)"
required: false
default: "plain"
concurrency:
group: telegram-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: false
jobs:
notify:
runs-on: ubuntu-latest
environment: SANDBOX
steps:
- name: Check out (for commit list)
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Build message
id: msg
shell: bash
run: |
set -euo pipefail
REPO="${{ github.repository }}"
ACTOR="${{ github.actor }}"
EVENT="${{ github.event_name }}"
RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
REPO_URL="${{ github.server_url }}/${{ github.repository }}"
msg_header() {
echo "📣 $1
Repo: $REPO
By: $ACTOR
Ref: ${{ github.ref_name }}
Run: $RUN_URL"
}
if [[ "$EVENT" == "workflow_dispatch" ]]; then
# Manual test: use the provided text as-is
MESSAGE="$(msg_header "Manual notification")"
MESSAGE="$MESSAGE
${{ github.event.inputs.text }}"
elif [[ "$EVENT" == "push" ]]; then
BEFORE="${{ github.event.before }}"
AFTER="${{ github.sha }}"
if [[ "$BEFORE" =~ ^0+$ ]]; then
RANGE="$AFTER -n 10"
COMMITS="$(git log --pretty=format:'- %s (%h) by %an' $RANGE | head -n 10 || true)"
else
COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$BEFORE..$AFTER" | head -n 10 || true)"
fi
[[ -z "${COMMITS:-}" ]] && COMMITS="- (no commit messages found)"
MESSAGE="$(msg_header "New push detected")"
MESSAGE="$MESSAGE
Last changes:
$COMMITS
Repo: $REPO_URL"
elif [[ "$EVENT" == "pull_request" ]]; then
if [[ "${{ github.event.action }}" == "closed" ]]; then
if [[ "${{ github.event.pull_request.merged }}" == "true" ]]; then
STATUS="PR merged ✅"
else
STATUS="PR closed ❌"
fi
else
STATUS="PR updated ✏️"
fi
MESSAGE="$(msg_header "$STATUS")"
MESSAGE="$MESSAGE
#${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}
${{ github.event.pull_request.html_url }}"
elif [[ "$EVENT" == "release" ]]; then
MESSAGE="$(msg_header "Release published 🏷️")"
MESSAGE="$MESSAGE
Tag: ${{ github.event.release.tag_name }}
Name: ${{ github.event.release.name || github.event.release.tag_name }}
${{ github.event.release.html_url }}"
else
MESSAGE="$(msg_header "Event: $EVENT")"
fi
# Export the (safely) raw text for next step
{
echo "text<<EOF"
echo "$MESSAGE"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Send to Telegram
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
# Choose chat id (manual override if provided)
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
# Parse mode handling
case "${PARSE_MODE:-plain}" in
MarkdownV2|HTML) PMODE="$PARSE_MODE" ;;
*) PMODE="" ;; # plain text
esac
# Build payload with jq to escape safely
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."