1+ name : 🚀 GitHub to Telegram Notification
2+
3+ on :
4+ push :
5+ branches-ignore :
6+ - ' dependabot/**' # Optional: skip bot commits
7+
8+ jobs :
9+ notify :
10+ runs-on : ubuntu-latest
11+ steps :
12+ - name : 📩 Send Telegram Notification
13+ env :
14+ TELEGRAM_CHAT_ID : ${{ secrets.TELEGRAM_TO }}
15+ TELEGRAM_BOT_TOKEN : ${{ secrets.TELEGRAM_TOKEN }}
16+ run : |
17+ # Validate required secrets
18+ if [ -z "$TELEGRAM_CHAT_ID" ] || [ -z "$TELEGRAM_BOT_TOKEN" ]; then
19+ echo "⚠️ Missing TELEGRAM_TO or TELEGRAM_TOKEN secret. Skipping notification."
20+ exit 0
21+ fi
22+
23+ # Extract event data
24+ USER="${{ github.actor }}"
25+ REPO="${{ github.repository }}"
26+ BRANCH="${{ github.ref_name }}"
27+ COMMIT_MSG="${{ github.event.head_commit.message }}"
28+ COMMIT_SHA="${{ github.sha }}"
29+ COMMIT_URL="https://github.com/${REPO}/commit/${COMMIT_SHA}"
30+ PROFILE_URL="https://github.com/${USER}"
31+
32+ # Format timestamp in JST (UTC+9)
33+ JST_TIMESTAMP=$(TZ="Asia/Tokyo" date -d "${{ github.event.head_commit.timestamp }}" "+%Y-%m-%d %H:%M:%S JST" 2>/dev/null || echo "Unknown time")
34+
35+ # Get total commit count (fallback to "N/A" if failed)
36+ TOTAL_COMMITS=$(curl -s "https://api.github.com/repos/${REPO}/commits?per_page=1" | jq -r 'length // "N/A"' 2>/dev/null)
37+
38+ # Escape special Markdown characters in commit message
39+ ESCAPED_MSG=$(printf '%s' "$COMMIT_MSG" | sed 's/[_*[\]()~`>#+\-=|{}.!]/\\&/g')
40+
41+ # Build message
42+ MESSAGE="🚀 *New Commit Pushed!*
43+
44+ ———————————————
45+ 🏷️ *Commit by:* \`${USER}\`
46+ 📂 *Repository:* \`${REPO}\`
47+ 🌿 *Branch:* \`${BRANCH}\`
48+ 🛠️ *Total Commits:* \`${TOTAL_COMMITS}\`
49+ ⏳ *Pushed at:* \`${JST_TIMESTAMP}\`
50+ ———————————————
51+
52+ 📝 *Commit Message:*
53+ \`\`\`
54+ ${ESCAPED_MSG}
55+ \`\`\`"
56+
57+ # Send to Telegram
58+ RESPONSE=$(curl -s -o response.json -w "%{http_code}" -X POST \
59+ " https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
60+ -H "Content-Type : application/json" \
61+ -d "{
62+ \"chat_id\" : \"${TELEGRAM_CHAT_ID}\",
63+ \"text\" : \"${MESSAGE}\",
64+ \"parse_mode\" : \"MarkdownV2\",
65+ \"disable_web_page_preview\" : true,
66+ \"reply_markup\" : {
67+ \"inline_keyboard\" : [
68+ [{\"text\" : \"👤 View Profile\", \"url\": \"${PROFILE_URL}\"}],
69+ [{\"text\" : \"🔗 View Commit\", \"url\": \"${COMMIT_URL}\"}]
70+ ]
71+ }
72+ }")
73+
74+ # Check response
75+ if [ "$RESPONSE" -ne 200 ]; then
76+ echo "❌ Telegram notification failed (HTTP $RESPONSE). See response.json"
77+ cat response.json
78+ exit 1
79+ else
80+ echo "✅ Notification sent successfully!"
81+ fi
0 commit comments