Advertisement
Mayumi_H

Statuspage to Discord (webhook)

Aug 7th, 2024
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | Source Code | 0 0
  1. <?php
  2. //Status page to Discord Webhook
  3. $start_time = microtime(true);
  4. $UAstring = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0";
  5.  
  6. //Discord channel
  7. $webhook_url = 'https://discord.com/api/webhooks/--------Your Discord Web Hook----------------------------------------------------------';
  8.  
  9. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  10.     header('HTTP/1.0 405 Method Not Allowed');
  11.     echo "Only POST requests allowed\n";
  12.     exit();
  13. }
  14.  
  15. if ($_SERVER['CONTENT_TYPE'] != 'application/json') {
  16.     header('HTTP/1.0 415 Unsupported Media Type');
  17.     echo "I only accept application/json data\n";
  18.     exit();
  19. }
  20.  
  21. $json = file_get_contents('php://input');
  22. $data = json_decode($json, true);
  23.  
  24. if (!array_key_exists('incident', $data)) exit();
  25.  
  26. $title       = $data['incident']['name'];
  27. $link        = $data['incident']['shortlink'];
  28. $timestamp   = $data['incident']['updated_at'];
  29. $sta_str     = $data['incident']['status'];
  30.  
  31. if(     $sta_str == 'investigating') $description = ":yellow_circle:";
  32. else if($sta_str == 'monitoring')    $description = ":yellow_circle:";
  33. else if($sta_str == 'in_progress')   $description = ":yellow_circle:";
  34. else if($sta_str == 'update')        $description = ":red_circle:";
  35. else if($sta_str == 'identified')    $description = ":red_circle:";
  36. else if($sta_str == 'resolved')      $description = ":green_circle:";
  37. else if($sta_str == 'completed')     $description = ":green_circle:";
  38. else if($sta_str == 'scheduled')     $description = ":calendar:";
  39. else                                 $description = ":white_circle:";
  40.  
  41. //status文字を付加
  42. $description .= " **" . ucfirst($sta_str) . "**\n";
  43.  
  44. //最新のupdate内容のみを付加
  45. if (count($data['incident']['incident_updates']) >= 1) {
  46.     $description .= $data['incident']['incident_updates'][0]['body'];
  47. }
  48.  
  49. $message = array(
  50. //  "content"   => "文字のところ 必要ならセットする。",
  51.     "username"  => "Status page",
  52. //  "avatar_url" => "https://my-icons-server.com/avatar_icon.png",
  53.     "embeds" => [
  54.         [
  55.             "title" => $title,
  56.             "type"  => "rich",
  57.             "description" => $description,
  58.             "url"   => $link,
  59.             "timestamp" => $timestamp,
  60.             "color" => hexdec( "73ba00" )
  61.         ]
  62.     ]
  63. );
  64.  
  65. //Discordへ出力
  66. $ch = curl_init();
  67. curl_setopt_array( $ch, [
  68.     CURLOPT_URL  => $webhook_url,
  69.     CURLOPT_POST => true,
  70.     CURLOPT_HTTPHEADER => array('Content-Type: application/json; charset=utf-8'),
  71.     CURLOPT_USERAGENT  => $UAstring,
  72.     CURLOPT_RETURNTRANSFER => true,
  73.     CURLOPT_SSL_VERIFYPEER => false,
  74.     CURLOPT_POSTFIELDS => json_encode($message, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
  75.     CURLOPT_TIMEOUT => 60
  76. ]);
  77. curl_exec($ch);
  78. curl_close($ch);
  79.  
  80. $end_time = microtime(true);
  81. echo "ok";
  82. echo "\ntimes = " . ($end_time - $start_time) . " seconds.";
  83. ?>
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement