Skip to content

Commit d8c6a9e

Browse files
authored
ci: announce stable releases in Telegram (#25)
* ci: announce stable releases in Telegram channel * ci: remove whitespace from Telegram release workflow
1 parent 7faa928 commit d8c6a9e

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: Telegram release announcements
2+
3+
'on':
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
notify:
13+
name: Notify @promsdev
14+
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.release.draft == false && github.event.release.prerelease == false) }}
15+
runs-on: ubuntu-24.04
16+
timeout-minutes: 5
17+
steps:
18+
- name: Publish release or test message
19+
env:
20+
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
21+
RELEASE_PROJECT: Telegram Web Proxy Manager
22+
RELEASE_EMOJI: ✈️
23+
run: |
24+
python3 - <<'PY'
25+
import html
26+
import json
27+
import os
28+
import re
29+
import sys
30+
import urllib.error
31+
import urllib.request
32+
33+
token = os.environ.get("TELEGRAM_BOT_TOKEN", "")
34+
if not token:
35+
sys.exit("Missing TELEGRAM_BOT_TOKEN repository secret")
36+
37+
name = os.environ["RELEASE_PROJECT"]
38+
emoji = os.environ["RELEASE_EMOJI"]
39+
if os.environ["GITHUB_EVENT_NAME"] == "workflow_dispatch":
40+
message = (
41+
"🧪 <b>Проверка уведомлений / Release bot test</b>\n\n"
42+
f"{html.escape(emoji + ' ' + name)}\n"
43+
"GitHub Actions → Telegram: тестовое сообщение."
44+
)
45+
else:
46+
with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as file:
47+
release = json.load(file)["release"]
48+
if release.get("draft") or release.get("prerelease"):
49+
sys.exit("Not a stable published release")
50+
version = str(release["tag_name"])[:80]
51+
url = release["html_url"]
52+
expected = "https://github.com/" + os.environ["GITHUB_REPOSITORY"] + "/releases/"
53+
if not url.startswith(expected):
54+
sys.exit("Unexpected release URL")
55+
highlights = []
56+
for line in (release.get("body") or "").splitlines():
57+
match = re.match(r"^\s*[-*]\s+(.+)", line)
58+
if not match:
59+
continue
60+
item = match.group(1).strip()
61+
if item.startswith(("!", "http://", "https://")):
62+
continue
63+
item = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", item)
64+
item = item.replace(chr(96), "").replace("*", "").replace("~", "")
65+
item = re.sub(r"\s+", " ", item).strip()
66+
if len(item) < 6:
67+
continue
68+
if len(item) > 170:
69+
item = item[:167].rstrip() + "..."
70+
highlights.append("• " + html.escape(item))
71+
if len(highlights) == 3:
72+
break
73+
74+
message = (
75+
f"{emoji} <b>{html.escape(name)} · {html.escape(version)}</b>\n"
76+
"Новый релиз / New release"
77+
)
78+
if highlights:
79+
message += "\n\n✨ <b>Главное / Highlights</b>\n" + "\n".join(highlights)
80+
message += (
81+
"\n\n📦 <a href=\"" + html.escape(url, quote=True) +
82+
"\">Описание и загрузка / Release notes</a>"
83+
)
84+
85+
payload = json.dumps({
86+
"chat_id": "@promsdev",
87+
"text": message,
88+
"parse_mode": "HTML",
89+
"link_preview_options": {"is_disabled": True},
90+
}, ensure_ascii=False).encode("utf-8")
91+
request = urllib.request.Request(
92+
"https://api.telegram.org/bot" + token + "/sendMessage",
93+
data=payload,
94+
headers={"Content-Type": "application/json"},
95+
method="POST",
96+
)
97+
try:
98+
with urllib.request.urlopen(request, timeout=20) as response:
99+
answer = json.load(response)
100+
except urllib.error.HTTPError as error:
101+
sys.exit(f"Telegram API HTTP {error.code}: check token and channel posting permission")
102+
except urllib.error.URLError:
103+
sys.exit("Telegram API network error")
104+
if not answer.get("ok"):
105+
sys.exit("Telegram API did not confirm sending")
106+
print("Telegram channel message sent")
107+
PY

0 commit comments

Comments
 (0)