Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
アイドルマスターシンデレラガールズ スターライトステージの新着ニュースをWebhookを通じてDiscordに通知する。
<?php
function sendDiscordWebhook($message)
{
$url = "<DISCORD WEBHOOK URL>";
$data = [
"content" => $message
];
$header = array(
"Content-Type: application/json",
"Content-Length: ".strlen(json_encode($data)),
"User-Agent: DiscordBot (http://example.com, v0.0.1)"
);
$context = array(
"http" => array(
"method" => "POST",
"header" => implode("\r\n", $header),
"content" => json_encode($data)
)
);
$context = stream_context_create($context);
file_get_contents($url, false, $context);
}
if (file_exists(__DIR__ . "/starlight-stage_readed.json")) {
$readed = json_decode(file_get_contents(__DIR__ . "/starlight-stage_readed.json"), true);
} else {
$readed = [];
}
$json = file_get_contents("https://apis.game.starlight-stage.jp/information/ajax_announce");
$json = json_decode($json, true);
foreach ($json["announce_list"] as $one) {
$id = $one["announce_id"];
$title = $one["title"];
$link_num = $one["link_num"];
$post_at = $one["post_at"];
if (in_array($id, $readed)) {
continue;
}
$url = "https://apis.game.starlight-stage.jp/information/detail/" . $id . "/1/" . "10" . "/" . $link_num;
$html = file_get_contents($url);
preg_match("/<div class=\"text\">([\s\S]+)<\/div>/", $html, $m);
if (!isset($m[1])) {
continue;
}
$text = $m[1];
$text = strip_tags($text);
$text = trim($text);
sendDiscordWebhook(":new:__**" . $title . "**__ (" . $post_at . ")\n```" . mb_strimwidth($text, 0, 1950, "...") . "```\n" . $url);
$readed[] = $id;
}
file_put_contents(__DIR__ . "/starlight-stage_readed.json", json_encode($readed));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment