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