Skip to content

Commit 39f798d

Browse files
authored
Update webhook.php
1 parent 7839d52 commit 39f798d

File tree

1 file changed

+32
-87
lines changed

1 file changed

+32
-87
lines changed

.github/workflows/webhook.php

Lines changed: 32 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,34 @@
11
<?php
22

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-
?>
3+
$botToken = getenv('TELEGRAM_BOT_TOKEN');
4+
$chatId = getenv('TELEGRAM_CHAT_ID');
5+
$releaseName = getenv('RELEASE_NAME');
6+
$releaseTag = getenv('RELEASE_TAG');
7+
$releaseBody = getenv('RELEASE_BODY');
8+
$releaseUrl = "https://github.com/" . getenv('GITHUB_REPOSITORY') . "/releases/tag/" . $releaseTag;
9+
10+
$message = "🚀 *New Release: $releaseName*
11+
12+
";
13+
$message .= "🔖 *Version:* `$releaseTag`
14+
";
15+
$message .= "📝 *Details:*
16+
$releaseBody";
17+
18+
$url = "https://api.telegram.org/bot$botToken/sendMessage";
19+
20+
$postFields = [
21+
'chat_id' => $chatId,
22+
'text' => $message,
23+
'parse_mode' => 'Markdown'
24+
];
25+
26+
$ch = curl_init();
27+
curl_setopt($ch, CURLOPT_URL, $url);
28+
curl_setopt($ch, CURLOPT_POST, 1);
29+
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
30+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
31+
$response = curl_exec($ch);
32+
curl_close($ch);
33+
34+
echo $response;

0 commit comments

Comments
 (0)