Last active
July 2, 2024 15:58
-
-
Save YumNumm/68d7d5d0a80549aff3707747bac57cee to your computer and use it in GitHub Desktop.
Cloudflare Email Workersで、Slack通知を行うスクリプト cf. https://developers.cloudflare.com/email-routing/email-workers/
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
export default { | |
async email(message, env, ctx) { | |
await message.forward("[email protected]"); | |
const SLACK_WEBHOOK_URL = | |
"https://hooks.slack.com/services/[credential]"; | |
const data = { | |
username: message.from, | |
channel: "#mail", | |
text: `<!channel> ${message.from}からのメール`, | |
attachments: [ | |
{ | |
fallback: `${message.from} → ${message.to}`, | |
color: "#0000D0", | |
fields: [ | |
{ | |
title: "送信元", | |
value: message.from, | |
}, | |
{ | |
title: "送信先", | |
value: message.to, | |
}, | |
{ | |
title: "件名", | |
value: message.headers.get("subject") ?? "件名なし", | |
}, | |
], | |
}, | |
], | |
}; | |
const result = await fetch(SLACK_WEBHOOK_URL, { | |
headers: { | |
"Content-Type": "application/json; charset=utf-8", | |
}, | |
method: "POST", | |
body: JSON.stringify(data), | |
}); | |
console.log(result); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment