|
| 1 | +name: Telegram Notifier |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ["**"] |
| 6 | + tags: ["*"] |
| 7 | + pull_request: |
| 8 | + types: [opened, reopened, synchronize, closed] |
| 9 | + release: |
| 10 | + types: [published] |
| 11 | + |
| 12 | +# Avoid duplicate spam if multiple events fire at once |
| 13 | +concurrency: |
| 14 | + group: telegram-${{ github.ref }}-${{ github.event_name }} |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + notify: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + # If you created a GitHub Environment that holds the TG_* secrets, keep this. |
| 21 | + # Otherwise, remove the next line. |
| 22 | + environment: SANDBOX |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Check out (for commit list) |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Build message |
| 31 | + id: msg |
| 32 | + shell: bash |
| 33 | + run: | |
| 34 | + set -euo pipefail |
| 35 | +
|
| 36 | + REPO="${{ github.repository }}" |
| 37 | + ACTOR="${{ github.actor }}" |
| 38 | + EVENT="${{ github.event_name }}" |
| 39 | + RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 40 | + REPO_URL="${{ github.server_url }}/${{ github.repository }}" |
| 41 | + REF_NAME="${{ github.ref_name }}" |
| 42 | +
|
| 43 | + msg_header() { |
| 44 | + echo -e "📣 $1 |
| 45 | +Repo: ${REPO} |
| 46 | +By: ${ACTOR} |
| 47 | +Ref: ${REF_NAME} |
| 48 | +Run: ${RUN_URL}" |
| 49 | + } |
| 50 | + |
| 51 | + MESSAGE="" |
| 52 | + |
| 53 | + if [[ "$EVENT" == "push" ]]; then |
| 54 | + BEFORE="${{ github.event.before }}" |
| 55 | + AFTER="${{ github.sha }}" |
| 56 | + |
| 57 | + if [[ "${BEFORE}" =~ ^0+$ ]]; then |
| 58 | + RANGE="${AFTER} -n 10" |
| 59 | + else |
| 60 | + RANGE="${BEFORE}..${AFTER}" |
| 61 | + fi |
| 62 | + |
| 63 | + COMMITS="$(git log --pretty=format:'- %s (%h) by %an' ${RANGE} | head -n 10 || true)" |
| 64 | + [[ -z "${COMMITS}" ]] && COMMITS="- (no commit messages found)" |
| 65 | + |
| 66 | + MESSAGE="$(msg_header "New push detected")" |
| 67 | + MESSAGE="${MESSAGE} |
| 68 | +Last changes: |
| 69 | + ${COMMITS} |
| 70 | + |
| 71 | +Repo: ${REPO_URL}" |
| 72 | + |
| 73 | + elif [[ "$EVENT" == "pull_request" ]]; then |
| 74 | + PR_NUM="${{ github.event.pull_request.number }}" |
| 75 | + PR_TITLE="${{ github.event.pull_request.title }}" |
| 76 | + PR_URL="${{ github.event.pull_request.html_url }}" |
| 77 | + |
| 78 | + if [[ "${{ github.event.action }}" == "closed" ]]; then |
| 79 | + if [[ "${{ github.event.pull_request.merged }}" == "true" ]]; then |
| 80 | + STATUS="PR merged ✅" |
| 81 | + else |
| 82 | + STATUS="PR closed ❌" |
| 83 | + fi |
| 84 | + else |
| 85 | + STATUS="PR updated ✏️" |
| 86 | + fi |
| 87 | + |
| 88 | + MESSAGE="$(msg_header "${STATUS}")" |
| 89 | + MESSAGE="${MESSAGE} |
| 90 | +#${PR_NUM}: ${PR_TITLE} |
| 91 | + ${PR_URL}" |
| 92 | + |
| 93 | + elif [[ "$EVENT" == "release" ]]; then |
| 94 | + REL_TAG="${{ github.event.release.tag_name }}" |
| 95 | + REL_NAME="${{ github.event.release.name }}" |
| 96 | + REL_URL="${{ github.event.release.html_url }}" |
| 97 | + |
| 98 | + MESSAGE="$(msg_header "Release published 🏷️")" |
| 99 | + MESSAGE="${MESSAGE} |
| 100 | +Tag: ${REL_TAG} |
| 101 | +Name: ${REL_NAME:-${REL_TAG}} |
| 102 | + ${REL_URL}" |
| 103 | + |
| 104 | + else |
| 105 | +MESSAGE="$(msg_header "Event: ${EVENT}")" |
| 106 | + fi |
| 107 | + |
| 108 | + # Telegram hard limit ~4096 chars → keep some headroom |
| 109 | + LIMIT=3800 |
| 110 | + if (( ${#MESSAGE} > LIMIT )); then |
| 111 | + MESSAGE="${MESSAGE:0:LIMIT} |
| 112 | + …(truncated)" |
| 113 | + fi |
| 114 | + |
| 115 | +# Export message to next step |
| 116 | + { |
| 117 | + echo "text<<EOF" |
| 118 | + echo "${MESSAGE}" |
| 119 | + echo "EOF" |
| 120 | + } >> "$GITHUB_OUTPUT" |
| 121 | + |
| 122 | + - name: Send to Telegram |
| 123 | + env: |
| 124 | + TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} |
| 125 | + TG_CHAT_ID: ${{ secrets.TG_CHAT_ID }} |
| 126 | + TEXT: ${{ steps.msg.outputs.text }} |
| 127 | + shell: bash |
| 128 | + run: | |
| 129 | + set -euo pipefail |
| 130 | +
|
| 131 | + if [[ -z "${TG_BOT_TOKEN:-}" || -z "${TG_CHAT_ID:-}" ]]; then |
| 132 | + echo "❌ Telegram secrets missing (TG_BOT_TOKEN / TG_CHAT_ID)." |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | +
|
| 136 | + echo "Payload to Telegram (debug):" |
| 137 | + jq -n --arg chat_id "$TG_CHAT_ID" --arg text "$TEXT" \ |
| 138 | + '{chat_id:$chat_id, text:$text, disable_web_page_preview:true}' |
| 139 | +
|
| 140 | + jq -n --arg chat_id "$TG_CHAT_ID" --arg text "$TEXT" \ |
| 141 | + '{chat_id:$chat_id, text:$text, disable_web_page_preview:true}' \ |
| 142 | + | curl -sS --retry 3 --retry-connrefused --max-time 20 \ |
| 143 | + -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \ |
| 144 | + -H "Content-Type: application/json" \ |
| 145 | + -d @- \ |
| 146 | + | tee /tmp/tg_response.json |
| 147 | +
|
| 148 | + # Optional: basic success check |
| 149 | + if ! jq -e '.ok == true' /tmp/tg_response.json > /dev/null 2>&1; then |
| 150 | + echo "⚠️ Telegram API did not return ok=true:" |
| 151 | + cat /tmp/tg_response.json |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | +
|
| 155 | + echo "✅ Message sent to Telegram." |
0 commit comments