<?php

$GOOGLE_CHAT_WEBHOOK_URL = "https://chat.googleapis.com/v1/spaces/AAAA7hXAOCM/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=YupvEeDhNq2aF9ldCAf8TeJw8kmafVAXpj3xJWIzRGw%3D";

# 送出資訊給 Google Chat Webhook
function googleChatBot($url, $mode, $title, $info, $pic="https://dlg.x-legend.tw/apk/warning.png") {

  date_default_timezone_set('Asia/Taipei');
  $DATETIME = date('Y-m-d H:i:s');
  $SHORT_NAME = "G1EN";
  preg_match('/(.*)([A-Z]{2})/', $SHORT_NAME, $SN);
  $GAME_TYPE = $SN[1];
  $COUNTRY_CODE = $SN[2];

  if ($mode == "text") {
    $info = "[${title}]\n${GAME_TYPE}-${COUNTRY_CODE} (${DATETIME})\n$info";
    $output_messages = "{\"text\": \"${info}\"}";
  } elseif ($mode == "card") {
    $output_messages =
    "{
      \"cards\": [
        {
          \"sections\": [
            {
              \"widgets\": [
                {
                  \"image\": {
                    \"imageUrl\": \"${pic}\",
                  },
                },
                {
                  \"textParagraph\": {
                    \"text\": \"<b>[${title}]</b><br><b>${GAME_TYPE}-${COUNTRY_CODE}</b> <b><font color='#008000'>(${DATETIME})</font></b><br><b><font color='#ff0000'>${info}</font></b>\"
                  },
                },
              ]
            }
          ]
        }
      ]
    }";
  }

  echo $output_messages . "\n";

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $output_messages);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($output_messages)));
  curl_exec($ch);
  curl_close($ch);
}

googleChatBot($GOOGLE_CHAT_WEBHOOK_URL, "text", "NOTIFY", "CHECKITOUT");
googleChatBot($GOOGLE_CHAT_WEBHOOK_URL, "card", "NOTIFY", "CHECKITOUT");
