Skip to content

Instantly share code, notes, and snippets.

@orange634nty
Last active December 24, 2021 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orange634nty/7675cd5efc425b0551034a7d34370372 to your computer and use it in GitHub Desktop.
Save orange634nty/7675cd5efc425b0551034a7d34370372 to your computer and use it in GitHub Desktop.
const PRESENTATION_ID = "xxxpresentationidxxx"
const FOLDER_ID = "xxxfolderidxxx"
const DISOCRD_WEBHOOK_TOKEN = "xxxwebhooktokenxxx"
const DISCORD_WEBHOOK_URL = `https://discord.com/api/webhooks/xxxx/${DISOCRD_WEBHOOK_TOKEN}`
const main = () => {
const slide = SlidesApp.openById(PRESENTATION_ID).getSlides()[0]
const imgBlob = getImageBlob(slide, PRESENTATION_ID)
const file = DriveApp.getFolderById(FOLDER_ID).createFile(imgBlob).setName("date.png")
const imageUrl = updateAccessAndGetImageUrl(file)
postToDiscord([{title: "スライドから画像が投稿できます!", image: {url: imageUrl}}])
}
/**
* スライドから画像のBlob取得
*/
const getImageBlob = (silde, presentationId) => {
const url = `https://docs.google.com/presentation/d/${presentationId}/export/png?id=${presentationId}&pageid=${silde.getObjectId()}`
const options = {
method: "get",
headers: {"Authorization": `Bearer ${ScriptApp.getOAuthToken()}`},
muteHttpExceptions: true
}
const response = UrlFetchApp.fetch(url, options)
if (response.getResponseCode() === 200) {
return response.getAs("image/png")
} else {
return null
}
}
/**
* 画像ファイルのアクセス権を更新して、閲覧できるURLを返す
*/
const updateAccessAndGetImageUrl = file => {
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW)
return `http://drive.google.com/uc?export=view&id=${file.getId()}`
}
/**
* Webhook を使って Discord へ投稿する
*/
const postToDiscord = embeds => {
const jsonData = {
token: DISOCRD_WEBHOOK_TOKEN,
embeds: embeds
}
const options = {
method: "post",
headers: {"Content-type": "application/json"},
payload: JSON.stringify(jsonData)
}
UrlFetchApp.fetch(DISCORD_WEBHOOK_URL, options)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment