Created
February 13, 2023 07:36
-
-
Save yetimdasturchi/416efe14846dac0844cd63ef8efcb60f to your computer and use it in GitHub Desktop.
Github webhook to telegram bot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$token = "TOKEN"; //Telegram bot token | |
$chats = ['12345', '12345']; //Array of telegram chats to send a message | |
function send_request( $content = [] ){ | |
$url = "https://api.telegram.org/bot{$GLOBALS['token']}/sendMessage"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content)); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
//echo $result; | |
} | |
function send_message($message, $url=''){ | |
$message = "#github\n\n{$message}"; | |
$content = [ | |
'text' => $message, | |
'parse_mode' => 'html', | |
'disable_web_page_preview' => TRUE | |
]; | |
if ( !empty( $url ) ) { | |
$content['reply_markup']['inline_keyboard'] = [ | |
[ | |
[ | |
"text" => "🌐 View it on GitHub", | |
"url" => $url | |
] | |
] | |
]; | |
} | |
foreach ($GLOBALS['chats'] as $chat) { | |
$content['chat_id'] = $chat; | |
send_request( $content ); | |
} | |
} | |
function folder_exist($folder) { | |
$path = realpath($folder); | |
return ( $path !== FALSE && is_dir( $path ) ) ? TRUE : FALSE; | |
} | |
if (empty($_SERVER['CONTENT_TYPE'])) { | |
die("Missing HTTP 'Content-Type' header."); | |
} | |
if (empty($_SERVER['HTTP_X_GITHUB_EVENT'])) { | |
die("Missing HTTP 'X-Github-Event' header."); | |
} | |
switch( $_SERVER['CONTENT_TYPE'] ) { | |
case 'application/json': | |
$json = file_get_contents('php://input'); | |
break; | |
case 'application/x-www-form-urlencoded': | |
$json = $_POST['payload']; | |
break; | |
default: | |
die("Unsupported content type: {$_SERVER['CONTENT_TYPE']}"); | |
} | |
$payload = json_decode( $json, TRUE ); | |
switch ( strtolower( $_SERVER['HTTP_X_GITHUB_EVENT'] ) ) { | |
case 'ping': | |
send_message("Webhook test ping!"); | |
break; | |
case 'push': | |
$branch = explode('/', $payload['ref']); | |
$branch = end($branch); | |
$message = $payload['head_commit']['message']; | |
$url = $payload['head_commit']['url']; | |
$message = "📦 <b>Pushed to $branch at {$payload['repository']['name']}</b>\n\n{$message}"; | |
send_message($message, $url); | |
break; | |
case 'create': | |
$ref_type = $payload['ref_type']; | |
$ref = $payload['ref']; | |
$name = $payload['repository']['name']; | |
$message = "📦 <b>Created $ref_type at $name</b>\n\n$ref"; | |
send_message($message); | |
break; | |
case 'issues': | |
$title = $payload['issue']['title']; | |
$body = $payload['issue']['body']; | |
$url = $payload['issue']['html_url']; | |
$message = "⚠️ <b>New issue:</b> {$title}\n\n$body"; | |
send_message($message, $url); | |
break; | |
case 'issue_comment': | |
$title = $payload['issue']['title']; | |
$body = $payload['comment']['body']; | |
$username = '@'.$payload['comment']['user']['login']; | |
$user_url = $payload['comment']['user']['html_url']; | |
$url = $payload['issue']['html_url']; | |
$message = "📢 <b>New comment on issue:</b>\n\n<u>{$title}</u>\n\n<a href=\"{$user_url}\">{$username}</a>: {$body}"; | |
send_message($message, $url); | |
break; | |
default: | |
if ( folder_exist('logs/') ) mkdir("logs/", 0755); | |
$message = "Event: {$_SERVER['HTTP_X_GITHUB_EVENT']}\nPayload:\n\n"; | |
$filename = 'logs/'.md5(time() . rand(0, 1000)).'.txt'; | |
file_put_contents($filename, print_r($payload, TRUE)); | |
send_message( "A new log file has been opened for an undefined event: {$filename}" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shu webhook bo'lganda, serverdagi kodlar git dan yangilanishi CI/CD deyiladimi? (Bu savolni kodga aloqasi yo'q)