Skip to content

Commit

Permalink
[actions] Auto send release to telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuda-lev committed Nov 29, 2024
1 parent c18ebd8 commit 4bd4b61
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
77 changes: 77 additions & 0 deletions .github/scripts/send_release_to_telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import asyncio
import os
from telegram import Bot, LinkPreviewOptions


def format_release_message(tag_name: str, name: str, body: str) -> str:
"""
Formats a GitHub release message for Telegram with custom styling.
:param tag_name: The release tag.
:param name: The release name.
:param body: The release description.
:return: A neatly formatted message string ready to be sent on Telegram.
"""
formatted_parts = []
in_code_block = False
current_code_block = []
pip_command = None

lines = body.split("\n")

for line in lines:
if line.startswith("```"):
if in_code_block:
in_code_block = False
current_code_block.append(line)
formatted_parts.append("\n".join(current_code_block))
current_code_block = []
else:
in_code_block = True
current_code_block.append(line)
elif in_code_block:
current_code_block.append(line)
elif line.startswith("> Update with pip:"):
pip_command = line.split("`")[1]
elif line.startswith("-"):
line = line.replace("[", r"\[")
formatted_parts.append(f"• {line[1:]}")

if pip_command:
formatted_parts.append(f"\n>> Update with:\n```bash\n{pip_command}\n```")

message_body = "\n".join(formatted_parts)

formatted_message = (
f"🎉 **NEW VERSION • {name}**\n\n"
f"📝 [Changelog](https://github.com/david-lev/pywa/releases/tag/{tag_name})\n\n"
f"{message_body}\n"
)

return formatted_message


async def send_to_telegram():
bot_token = os.getenv("TELEGRAM_BOT_TOKEN")
chat_id = os.getenv("TELEGRAM_CHAT_ID")
release_name = os.getenv("RELEASE_NAME")
release_tag = os.getenv("RELEASE_TAG")
release_body = os.getenv("RELEASE_BODY")

message = format_release_message(release_tag, release_name, release_body)

bot = Bot(token=bot_token)
await bot.send_message(
chat_id=chat_id,
text=message,
parse_mode="Markdown",
link_preview_options=LinkPreviewOptions(
url="https://pywa.readthedocs.io/",
prefer_large_media=False,
show_above_text=True,
),
)


if __name__ == "__main__":
asyncio.run(send_to_telegram())
33 changes: 33 additions & 0 deletions .github/workflows/telegram-notify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Send Release to Telegram

on:
release:
types: [created]

jobs:
notify-telegram:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.12

- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install python-telegram-bot
- name: Send to Telegram
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
RELEASE_NAME: ${{ github.event.release.name }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
RELEASE_BODY: ${{ github.event.release.body }}

run: python .github/scripts/send_release_to_telegram.py

0 comments on commit 4bd4b61

Please sign in to comment.