Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save doitian/2ec2f58cbb7978a396d1cd28d73a7236 to your computer and use it in GitHub Desktop.
Save doitian/2ec2f58cbb7978a396d1cd28d73a7236 to your computer and use it in GitHub Desktop.
Send Google Calendar events to Discord via Webhook
function htmlToMarkdown(input: string) {
const linkRegex = /<a\s+[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi;
return input.replace(linkRegex, '[$2]($1)');
}
const TZ = 8*60*60;
function formatDiscordTimestamp(input: string) {
const ts = moment.utc(input, "MMMM D, YYYY at hh:mmA", "MMMM D, YYYY").unix() - TZ;
return `<t:${ts}>`;
}
const payload = {
username: 'Google Calendar',
embeds: [
{
title: GoogleCalendar.eventFromSearchStarts.Title,
description: htmlToMarkdown(GoogleCalendar.eventFromSearchStarts.Description),
color: 1733607,
fields: [
{
name: 'Starts',
value: formatDiscordTimestamp(GoogleCalendar.eventFromSearchStarts.Starts),
inline: true,
},
],
},
],
};
if (GoogleCalendar.eventFromSearchStarts.Where.trim() !== '') {
payload.embeds[0].fields.push({
name: 'Where',
value: GoogleCalendar.eventFromSearchStarts.Where,
inline: true,
});
}
if (GoogleCalendar.eventFromSearchStarts.VideoCallUrl.trim() !== '') {
payload.embeds[0].fields.push({
name: 'Google Meeting',
value: GoogleCalendar.eventFromSearchStarts.VideoCallUrl,
inline: true,
});
}
MakerWebhooks.makeWebRequest.setBody(JSON.stringify(payload));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment