Skip to content

Commit 1052887

Browse files
authored
telegram bot update
1 parent d386343 commit 1052887

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

.github/workflows/webhook.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
$telegram_bot_token = getenv('TELEGRAM_BOT_TOKEN');
4+
$telegram_chat_id = getenv('TELEGRAM_CHAT_ID');
5+
$github_repo_owner = getenv('REPO_OWNER');
6+
$github_repo_name = getenv('REPO_NAME');
7+
8+
function send_to_telegram($message) {
9+
global $telegram_bot_token, $telegram_chat_id;
10+
11+
$url = "https://api.telegram.org/bot$telegram_bot_token/sendMessage";
12+
$data = [
13+
'chat_id' => $telegram_chat_id,
14+
'text' => $message,
15+
'parse_mode' => 'Markdown',
16+
'disable_web_page_preview' => false,
17+
];
18+
19+
$opts = [
20+
"http" => [
21+
"method" => "POST",
22+
"header" => "Content-type: application/x-www-form-urlencoded",
23+
"content" => http_build_query($data)
24+
]
25+
];
26+
27+
$context = stream_context_create($opts);
28+
$response = file_get_contents($url, false, $context);
29+
30+
if ($response === FALSE) {
31+
error_log("Error sending message to Telegram.");
32+
} else {
33+
echo "Message sent successfully.\n";
34+
}
35+
}
36+
37+
function get_release_info($repo_owner, $repo_name, $tag) {
38+
$url = "https://api.github.com/repos/$repo_owner/$repo_name/releases/tags/$tag";
39+
40+
$opts = [
41+
"http" => [
42+
"method" => "GET",
43+
"header" => "User-Agent: PHP"
44+
]
45+
];
46+
47+
$context = stream_context_create($opts);
48+
$response = file_get_contents($url, false, $context);
49+
50+
if ($response === FALSE) {
51+
return null;
52+
}
53+
54+
$release_info = json_decode($response, true);
55+
56+
if (isset($release_info['tag_name'], $release_info['name'], $release_info['body'])) {
57+
return [
58+
'tag' => $release_info['tag_name'],
59+
'name' => $release_info['name'],
60+
'body' => $release_info['body']
61+
];
62+
}
63+
64+
return null;
65+
}
66+
67+
$payload = file_get_contents('php://input');
68+
$data = json_decode($payload, true);
69+
70+
if (isset($data['action'], $data['release']) && $data['action'] === 'published') {
71+
$release_tag = $data['release']['tag_name'];
72+
$release_name = $data['release']['name'];
73+
$release_body = $data['release']['body'];
74+
75+
$release_info = get_release_info($github_repo_owner, $github_repo_name, $release_tag);
76+
77+
if ($release_info) {
78+
79+
$message = "🎉 **NEW VERSION • " . $release_info['name'] . "**\n\n";
80+
$message .= "📝 [Changelog](https://github.com/$github_repo_owner/$github_repo_name/releases/tag/$release_tag)\n\n";
81+
$message .= $release_info['body'];
82+
83+
send_to_telegram($message);
84+
}
85+
} else {
86+
error_log("No release data found or incorrect action type.");
87+
}
88+
89+
?>

0 commit comments

Comments
 (0)