|
| 1 | +name: Notify |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - develop |
| 8 | + pull_request: |
| 9 | + types: [opened, closed, reopened, merged] |
| 10 | + issues: |
| 11 | + types: [opened, closed, reopened] |
| 12 | + |
| 13 | +env: |
| 14 | + NODE_VERSION: 20 |
| 15 | + |
| 16 | + TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} |
| 17 | + TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} |
| 18 | + |
| 19 | + GITHUB_ACTOR: ${{ github.actor }} |
| 20 | + GITHUB_EVENT_NAME: ${{ github.event_name }} |
| 21 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 22 | + GITHUB_REF: ${{ github.ref }} |
| 23 | + GITHUB_SHA: ${{ github.sha }} |
| 24 | + |
| 25 | +jobs: |
| 26 | + telegram-notify: |
| 27 | + name: Telegram Notification |
| 28 | + runs-on: ubuntu-latest |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout repository |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Setup Node.js |
| 35 | + uses: actions/setup-node@v4 |
| 36 | + with: |
| 37 | + node-version: ${{ env.NODE_VERSION }} |
| 38 | + |
| 39 | + - name: Install dependencies |
| 40 | + run: npm install node-fetch |
| 41 | + |
| 42 | + - name: Send Telegram notification |
| 43 | + run: | |
| 44 | + node -e " |
| 45 | + import fetch from 'node-fetch'; |
| 46 | +
|
| 47 | + const token = process.env.TELEGRAM_BOT_TOKEN; |
| 48 | + const chatId = process.env.TELEGRAM_CHAT_ID; |
| 49 | + const actor = process.env.GITHUB_ACTOR; |
| 50 | + const event = process.env.GITHUB_EVENT_NAME; |
| 51 | + const repo = process.env.GITHUB_REPOSITORY; |
| 52 | + const ref = process.env.GITHUB_REF; |
| 53 | + const sha = process.env.GITHUB_SHA; |
| 54 | +
|
| 55 | + let message = `*GitHub Notification*\nRepository: ${repo}\nActor: ${actor}\nEvent: ${event}\nRef: ${ref}\nSHA: ${sha}`; |
| 56 | +
|
| 57 | + if (event === 'pull_request') { |
| 58 | + const pr = JSON.parse(process.env.GITHUB_EVENT_PATH); |
| 59 | + message += `\nPR Title: ${pr.pull_request.title}\nAction: ${pr.action}\nURL: ${pr.pull_request.html_url}`; |
| 60 | + } |
| 61 | +
|
| 62 | + if (event === 'issues') { |
| 63 | + const issue = JSON.parse(process.env.GITHUB_EVENT_PATH); |
| 64 | + message += `\nIssue Title: ${issue.issue.title}\nAction: ${issue.action}\nURL: ${issue.issue.html_url}`; |
| 65 | + } |
| 66 | +
|
| 67 | + fetch(`https://api.telegram.org/bot${token}/sendMessage`, { |
| 68 | + method: 'POST', |
| 69 | + headers: { 'Content-Type': 'application/json' }, |
| 70 | + body: JSON.stringify({ |
| 71 | + chat_id: chatId, |
| 72 | + text: message, |
| 73 | + parse_mode: 'Markdown' |
| 74 | + }) |
| 75 | + }); |
| 76 | + " |
0 commit comments