|
| 1 | +name: Notify Activity to Telegram |
| 2 | + |
| 3 | +on: |
| 4 | + watch: |
| 5 | + types: [started] |
| 6 | + fork: |
| 7 | + issues: |
| 8 | + types: [opened] |
| 9 | + pull_request: |
| 10 | + types: [opened] |
| 11 | + |
| 12 | +jobs: |
| 13 | + notify: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Set up Python |
| 17 | + uses: actions/setup-python@v4 |
| 18 | + with: |
| 19 | + python-version: '3.x' |
| 20 | + |
| 21 | + - name: Install dependencies |
| 22 | + run: pip install requests |
| 23 | + |
| 24 | + - name: Send notification to Telegram |
| 25 | + env: |
| 26 | + BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} |
| 27 | + CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} |
| 28 | + TOPIC_ID: ${{ secrets.TELEGRAM_TOPIC_ID }} |
| 29 | + EVENT_NAME: ${{ github.event_name }} |
| 30 | + ACTOR: ${{ github.actor }} |
| 31 | + REPO: ${{ github.repository }} |
| 32 | + run: | |
| 33 | + python -c " |
| 34 | + import os, requests |
| 35 | + |
| 36 | + bot_token = os.environ.get('BOT_TOKEN') |
| 37 | + chat_id = os.environ.get('CHAT_ID') |
| 38 | + topic_id = os.environ.get('TOPIC_ID') |
| 39 | + event = os.environ.get('EVENT_NAME') |
| 40 | + actor = os.environ.get('ACTOR') |
| 41 | + repo = os.environ.get('REPO') |
| 42 | + |
| 43 | + if not bot_token or not chat_id: |
| 44 | + print('Telegram credentials not configured.') |
| 45 | + exit(0) |
| 46 | + |
| 47 | + if event == 'watch': |
| 48 | + action = 'starred' |
| 49 | + elif event == 'fork': |
| 50 | + action = 'forked' |
| 51 | + elif event == 'issues': |
| 52 | + action = 'opened an issue in' |
| 53 | + elif event == 'pull_request': |
| 54 | + action = 'opened a PR in' |
| 55 | + else: |
| 56 | + action = f'triggered {event} in' |
| 57 | + |
| 58 | + message = f'User [{actor}](https://github.com/{actor}) {action} the repository [{repo}](https://github.com/{repo})!' |
| 59 | + |
| 60 | + url = f'https://api.telegram.org/bot{bot_token}/sendMessage' |
| 61 | + payload = { |
| 62 | + 'chat_id': chat_id, |
| 63 | + 'text': message, |
| 64 | + 'parse_mode': 'Markdown', |
| 65 | + 'disable_web_page_preview': True |
| 66 | + } |
| 67 | + if topic_id: |
| 68 | + payload['message_thread_id'] = topic_id |
| 69 | + |
| 70 | + response = requests.post(url, json=payload) |
| 71 | + if response.status_code != 200: |
| 72 | + print(f'Failed to send message: {response.status_code} {response.text}') |
| 73 | + else: |
| 74 | + print('Message sent successfully!') |
| 75 | + " |
0 commit comments