|
| 1 | +name: Notify Chat on new issue |
| 2 | +on: |
| 3 | + issues: |
| 4 | + types: [opened] |
| 5 | + |
| 6 | +permissions: |
| 7 | + issues: read |
| 8 | + |
| 9 | +jobs: |
| 10 | + notify-telegram: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Build Telegram message |
| 14 | + id: build |
| 15 | + run: | |
| 16 | + set -euo pipefail |
| 17 | + # Inputs from event JSON |
| 18 | + issue_number="${{ github.event.issue.number }}" |
| 19 | + issue_title="${{ github.event.issue.title }}" |
| 20 | + issue_body="${{ github.event.issue.body || '' }}" |
| 21 | + issue_user="${{ github.event.issue.user.login }}" |
| 22 | + issue_url="${{ github.event.issue.html_url }}" |
| 23 | + labels_array="${{ toJson(github.event.issue.labels) }}" |
| 24 | +
|
| 25 | + # Extract label names (jq not available by default). Use GitHub-provided string if simple: |
| 26 | + # Build comma-separated labels |
| 27 | + labels="" |
| 28 | + if [ "${labels_array}" != "[]" ]; then |
| 29 | + # labels_array is JSON like [{"id":..., "name":"bug",...},...] |
| 30 | + # Use simple parsing to extract "name" fields |
| 31 | + labels=$(printf '%s' "${labels_array}" | sed -n 's/.*"name":"\([^"]*\)".*/\1/p' | paste -sd ", " -) |
| 32 | + fi |
| 33 | + if [ -z "$labels" ]; then |
| 34 | + labels="(none)" |
| 35 | + fi |
| 36 | +
|
| 37 | + # Truncate body |
| 38 | + maxlen=800 |
| 39 | + if [ "${#issue_body}" -gt "$maxlen" ]; then |
| 40 | + issue_body="${issue_body:0:$maxlen}...(truncated)" |
| 41 | + fi |
| 42 | +
|
| 43 | + # Escape HTML for Telegram parse_mode=HTML |
| 44 | + esc() { |
| 45 | + printf '%s' "$1" | sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g' |
| 46 | + } |
| 47 | +
|
| 48 | + repo="${GITHUB_REPOSITORY}" |
| 49 | + repo_url="https://github.com/${repo}" |
| 50 | + repo_link="<a href=\"${repo_url}\">${repo}</a>" |
| 51 | + issue_link="<a href=\"${issue_url}\">#${issue_number} ${esc "${issue_title}"}</a>" |
| 52 | +
|
| 53 | + body="Repo: ${repo_link}%0AIssue: ${issue_link}%0AAuthor: ${esc "${issue_user}"}%0ALabels: ${esc "${labels}"}%0A%0A${esc "${issue_body}"}" |
| 54 | + echo "body=$body" >> "$GITHUB_OUTPUT" |
| 55 | +
|
| 56 | + - name: Send Telegram notification |
| 57 | + env: |
| 58 | + TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} |
| 59 | + TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} |
| 60 | + run: | |
| 61 | + if [ -z "${TELEGRAM_BOT_TOKEN}" ] || [ -z "${TELEGRAM_CHAT_ID}" ]; then |
| 62 | + echo "Missing TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID; skipping." |
| 63 | + exit 0 |
| 64 | + fi |
| 65 | + body="${{ steps.build.outputs.body }}" |
| 66 | + curl -sS --fail -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ |
| 67 | + -d "chat_id=${TELEGRAM_CHAT_ID}" \ |
| 68 | + -d "text=${body}" \ |
| 69 | + -d "parse_mode=HTML" |
0 commit comments