Last active
December 21, 2022 17:30
-
-
Save miyarappo/fd7422908f0c0909aa8dea49fd4a9b40 to your computer and use it in GitHub Desktop.
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
const ouraBaseUrl = 'https://api.ouraring.com/v1/' | |
const ouraAccessToken = 'xxxx' | |
const slackProfileUrl = 'https://slack.com/api/users.profile.set' | |
const slackAccessToken = 'xxxx' | |
const slackHooksUrl = 'xxxx' | |
function main() { | |
const readinessScore = getReadinessScore() | |
const sleepScore = getSleepScore() | |
const activityScore = getActivityScore() | |
const {statusEmoji, statusText} = decideStatus(readinessScore) | |
updateSlackProfile(statusEmoji, statusText) | |
postSlackChannel(readinessScore, sleepScore, activityScore, statusEmoji) | |
} | |
function getReadinessScore() { | |
const date = new Date() | |
const currentDate = Utilities.formatDate(date, 'JST', 'yyyy-MM-dd') | |
const previousDate = Utilities.formatDate(new Date(date.setDate(date.getDate() - 1)), 'JST', 'yyyy-MM-dd') | |
const ouraApiOptions = { | |
"method": "get", | |
"headers": { | |
"Authorization": "Bearer "+ ouraAccessToken | |
} | |
} | |
const ouraReadinessUrl = `${ouraBaseUrl}readiness?start=${previousDate}&end=${currentDate}` | |
const response = UrlFetchApp.fetch(ouraReadinessUrl, ouraApiOptions) | |
const responseData = JSON.parse(response) | |
const readinessScore = responseData.readiness.length === 0 ? 0 : responseData.readiness[0].score | |
return readinessScore | |
} | |
function getSleepScore() { | |
const date = new Date() | |
const currentDate = Utilities.formatDate(date, 'JST', 'yyyy-MM-dd') | |
const previousDate = Utilities.formatDate(new Date(date.setDate(date.getDate() - 1)), 'JST', 'yyyy-MM-dd') | |
const ouraApiOptions = { | |
"method": "get", | |
"headers": { | |
"Authorization": "Bearer "+ ouraAccessToken | |
} | |
} | |
const ouraReadinessUrl = `${ouraBaseUrl}sleep?start=${previousDate}&end=${currentDate}` | |
const response = UrlFetchApp.fetch(ouraReadinessUrl, ouraApiOptions) | |
const responseData = JSON.parse(response) | |
const sleepScore = responseData.sleep.length === 0 ? 0 : responseData.sleep[0].score | |
return sleepScore | |
} | |
function getActivityScore() { | |
const date = new Date() | |
const currentDate = Utilities.formatDate(date, 'JST', 'yyyy-MM-dd') | |
const previousDate = Utilities.formatDate(new Date(date.setDate(date.getDate() - 1)), 'JST', 'yyyy-MM-dd') | |
const ouraApiOptions = { | |
"method": "get", | |
"headers": { | |
"Authorization": "Bearer "+ ouraAccessToken | |
} | |
} | |
const ouraReadinessUrl = `${ouraBaseUrl}activity?start=${previousDate}&end=${currentDate}` | |
const response = UrlFetchApp.fetch(ouraReadinessUrl, ouraApiOptions) | |
const responseData = JSON.parse(response) | |
const activityScore = responseData.activity.length === 0 ? 0 : responseData.activity[0].score | |
return activityScore | |
} | |
function decideStatus(readinessScore) { | |
const statusObj = { | |
"great": { | |
"emoji": ":star-struck:", | |
"text": "絶好調!今日の俺は神" | |
}, | |
"good": { | |
"emoji": ":smile:", | |
"text": "良い感じ" | |
}, | |
"normal": { | |
"emoji": ":simple_smile:", | |
"text": "まぁまぁ" | |
}, | |
"delicate": { | |
"emoji": ":tired_face:", | |
"text": "微妙。今日は早めに帰る" | |
}, | |
"bad": { | |
"emoji": ":scream:", | |
"text": "無理ぽよ" | |
}, | |
"sleeping": { | |
"emoji": ":sleeping:", | |
"text": "寝てます" | |
} | |
} | |
let statusEmoji | |
let statusText | |
if (readinessScore >= 90) { | |
statusEmoji = statusObj.great.emoji | |
statusText = statusObj.great.text | |
} else if (readinessScore >= 80 && readinessScore < 90 ) { | |
statusEmoji = statusObj.good.emoji | |
statusText = statusObj.good.text | |
} else if (readinessScore >= 70 && readinessScore < 80 ) { | |
statusEmoji = statusObj.normal.emoji | |
statusText = statusObj.normal.text | |
} else if (readinessScore >= 60 && readinessScore < 69 ) { | |
statusEmoji = statusObj.delicate.emoji | |
statusText = statusObj.delicate.text | |
} else if (readinessScore == 0) { | |
statusEmoji = statusObj.sleeping.emoji | |
statusText = statusObj.sleeping.text | |
}else { | |
statusEmoji = statusObj.bad.emoji | |
statusText = statusObj.bad.text | |
} | |
return {statusEmoji: statusEmoji, statusText: statusText} | |
} | |
function updateSlackProfile(statusEmoji, statusText) { | |
const slackProsileOpstions = { | |
"method": "post", | |
"headers": { | |
"Authorization": "Bearer "+ slackAccessToken | |
}, | |
"contentType": "application/json", | |
"payload": JSON.stringify({ | |
"profile": { | |
"status_emoji": statusEmoji, | |
"status_text": statusText | |
} | |
}) | |
} | |
UrlFetchApp.fetch(slackProfileUrl, slackProsileOpstions) | |
} | |
function postSlackChannel(readinessScore, sleepScore, activityScore, statusEmoji) { | |
let text = `まだ寝てます :sleeping:` | |
if (readinessScore > 0) { | |
text = `:mega: Oura Ringが取得したデータを通知します :mega:\n本日のコンディションスコアは${readinessScore}です ${statusEmoji}\n睡眠スコアは${sleepScore}です\nアクティビティスコアは${activityScore}です` | |
} | |
const slackApiOptions = { | |
"method": "post", | |
"contentType": "application/json", | |
"payload": JSON.stringify({ | |
"username": "Oura Score Notification", | |
"icon_emoji": ":green_heart:", | |
"text": text | |
}) | |
} | |
UrlFetchApp.fetch(slackHooksUrl, slackApiOptions) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment