Last active
March 30, 2021 06:12
-
-
Save 5t111111/1ea24afb9f84562639496c609e0dd803 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface UploadPayload { | |
public_id: string; | |
version: number; | |
width: number; | |
height: number; | |
format: string; | |
resource_type: string; | |
created_at: string; | |
tags: string[]; | |
pages: number; | |
bytes: number; | |
type: string; | |
etag: string; | |
placeholder: boolean; | |
url: string; | |
secure_url: string; | |
access_mode: string; | |
original_filename: string; | |
notification_type: string; | |
} | |
interface EagerPayload { | |
notification_type: string; | |
eager: { | |
crop: string; | |
width: number; | |
height: number; | |
bytes: number; | |
url: string; | |
secure_url: string; | |
}[]; | |
public_id: string; | |
batch_id: string; | |
} | |
interface Message { | |
text: string; | |
attachments: { | |
text: string; | |
}[]; | |
} | |
const SLACK_TOKEN = Deno.env.get("SLACK_TOKEN")!; | |
const SLACK_CHANNEL_ID = Deno.env.get("SLACK_CHANNEL_ID")!; | |
function convertPayloadToMessage(eventPayload: any): Message { | |
if (eventPayload.notification_type === "upload") { | |
const payload = eventPayload as UploadPayload; | |
if (payload.resource_type === "image") { | |
return { | |
text: ":rice_scene: Cloudinaryに画像がアップロードされました", | |
attachments: [ | |
{ | |
text: payload.secure_url, | |
}, | |
], | |
}; | |
} else if (payload.resource_type === "video") { | |
return { | |
text: ":movie_camera: Cloudinaryに動画がアップロードされました", | |
attachments: [ | |
{ | |
text: payload.public_id, | |
}, | |
{ | |
text: (payload as any).eager[0].secure_url, | |
}, | |
], | |
}; | |
} else { | |
return { | |
text: | |
":question: Cloudinaryに画像・動画以外のファイルがアップロードされました", | |
attachments: [ | |
{ | |
text: payload.public_id, | |
}, | |
], | |
}; | |
} | |
} else if (eventPayload.notification_type === "eager") { | |
const payload = eventPayload as EagerPayload; | |
return { | |
text: ":hourglass: Cloudinaryの変換が完了しました", | |
attachments: [ | |
{ | |
text: payload.public_id, | |
}, | |
{ | |
text: payload.eager[0].secure_url, | |
}, | |
], | |
}; | |
} | |
} | |
addEventListener("fetch", (event) => { | |
event.respondWith(handleRequest(event.request)); | |
}); | |
async function handleRequest(request) { | |
if (request.method !== "POST") { | |
return new Response(null, { | |
status: 405, | |
statusText: "Method Not Allowed", | |
}); | |
} | |
const json = await request.json(); | |
const message = convertPayloadToMessage(json); | |
await fetch("https://slack.com/api/chat.postMessage", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json; charset=utf-8", | |
Authorization: `Bearer ${SLACK_TOKEN}`, | |
}, | |
body: JSON.stringify({ | |
...message, | |
channel: SLACK_CHANNEL_ID, | |
}), | |
}); | |
const responseInit = { | |
headers: { | |
"Content-Type": "application/json; charset=utf-8", | |
}, | |
}; | |
return new Response(JSON.stringify({ json }, null, 2), responseInit); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment