Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from Fastjur/feature/telegram-notifications
Browse files Browse the repository at this point in the history
Add telegram notification on PR opened
  • Loading branch information
RubenNair authored Jun 3, 2022
2 parents 4f064d0 + cd558c7 commit 7173a5d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/pr-opened-notification.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Notify PR Opened

on:
pull_request:
branches:
- main

jobs:
notify-telegram:
name: Notify PR Opened - Telegram
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Notify Telegram
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
run: |
cd notifications
python3 telegram.py pr_opened "${{ github.actor }}" "${{ github.event.pull_request.title }}" "${{ github.event.pull_request.html_url }}"
37 changes: 37 additions & 0 deletions notifications/TelegramNotifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys

import requests


class TelegramNotifier:
def __init__(self, bot_token, chat_id):
self.bot_token = bot_token
self.chat_id = chat_id

def notify(self, message, inline_keyboard_markup):
url = 'https://api.telegram.org/bot{}/sendMessage'.format(self.bot_token)
data = {
'chat_id': self.chat_id,
'text': message,
'parse_mode': 'Markdown',
'reply_markup': {
'inline_keyboard': inline_keyboard_markup
},
}
print(data)
res = requests.post(url, json=data)
if res.status_code < 200 or res.status_code >= 300:
print("❌ Error sending message:")
print(res.text)
sys.exit(1)

def notify_pr_opened(self, pr_author, pr_title, pr_url):
with open('templates/pr_opened.txt') as f:
message_text = ''.join(f.readlines()).format(pr_author, pr_title, pr_url)
print("📩 Sending message:")
print(message_text)

inline_keyboard_markup = [
[{'text': 'View on GitHub', 'url': pr_url}]
]
self.notify(message_text, inline_keyboard_markup)
27 changes: 27 additions & 0 deletions notifications/telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
import os

from TelegramNotifier import TelegramNotifier

if __name__ == '__main__':
bot_token = os.environ['TELEGRAM_BOT_TOKEN']
chat_id = os.environ['TELEGRAM_CHAT_ID']

if bot_token is None or chat_id is None:
print("Please set the TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID environment variables")
sys.exit(1)
telegram_notifier = TelegramNotifier(bot_token, chat_id)

if len(sys.argv) < 2:
print('Usage: {} <message_type>'.format(sys.argv[0]))
sys.exit(1)

message_type = sys.argv[1]
if message_type == 'pr_opened':
if len(sys.argv) != 5:
print('Usage: {} pr_opened <pr_author> <pr_title> <pr_url>'.format(sys.argv[0]))
sys.exit(1)
pr_author = sys.argv[2]
pr_title = sys.argv[3]
pr_url = sys.argv[4]
telegram_notifier.notify_pr_opened(pr_author, pr_title, pr_url)
4 changes: 4 additions & 0 deletions notifications/templates/pr_opened.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
🔄 *A new Pull Request has been opened!*
PR Author: `{}`.
PR Title: _{}_.
PR URL: `{}`

0 comments on commit 7173a5d

Please sign in to comment.