#!/bin/bash
###### 檔案資訊 ######
#
# 檔名: send_chatbot_text_only_by_thread_test
#
# 功能: 將檢測空間達到設定上限的資訊,
#       發送至各版本的 Skype / Google Meet 群組
#       以討論串的方式進行 ( 以小時/分鐘做區隔 )
#
###### 檔案資訊 ######

# 如果 gamerc 存在, 則載入它
[ -f ~/.gamerc ] && source ~/.gamerc

# 如果是非 Ctrl 架構請自行設定遊戲名稱跟國家名稱
[ -z "${GAME_TYPE}" ] && GAME_TYPE="非Ctrl架構, 自行設定"
[ -z "${COUNTRY_CODE}" ] && COUNTRY_CODE="非Ctrl架構, 自行設定"

# 如果有需要自訂或是修改顯示的遊戲與國家名稱, 請自行修正
[ "${GAME_TYPE}" == "AK" ] && GAME_TYPE_DISPLAY="M7" || GAME_TYPE_DISPLAY=${GAME_TYPE}
[ ! -z "${COUNTRY_CODE_DISPLAY}" ] && COUNTRY_CODE_DISPLAY=${COUNTRY_CODE_DISPLAY} || COUNTRY_CODE_DISPLAY=${COUNTRY_CODE}

TITLE=$1
CONTENT=$2
TIME_TYPE=$3
TIME_INTERVAL=$4

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

[ "$4" == "" ] && exec echo "Usage: $0 <MESSAGES_TITLE> <MESSAGES_CONTENT> <TIME_TYPE> <TIME_INTERVAL>"

DATETIME_OUTPUT=$(date +"%Y-%m-%d %T %Z")
DATETIME_FOR_TK=$(date +"%Y%m%d%H%M%S")

THREAD_KEY=$(google_chat_thread_generater ${DATETIME_FOR_TK} ${TIME_TYPE} ${TIME_INTERVAL})

echo "DATETIME_OUTPUT => $DATETIME_OUTPUT"
echo "DATETIME_FOR_TK => $DATETIME_FOR_TK"
echo "THREAD_KEY => $THREAD_KEY"

MESSAGE=$(printf "[${TITLE}]\n${DATETIME_OUTPUT}\n${GAME_TYPE_DISPLAY}-${COUNTRY_CODE_DISPLAY} => ${CONTENT}")
curl -s -X POST -H "Content-Type: application/json" $GOOGLE_CHAT_WEBHOOK_URL_THREAD -d '{ "text": "'"${MESSAGE}"'", "thread": { "threadKey": "'"${THREAD_KEY}"'" } }'
