Last active
October 14, 2019 13:48
-
-
Save wilcoschoneveld/b0818df2a31d52a7b0e2e216b5e725b7 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
async processEvent(event: GoogleCalendarEvent) { | |
// Test event for a summary in the format of '[#channel] ...' | |
const regex = /^\[(#[a-z0-9-_]+)\]/; | |
const elements = regex.exec(event.summary); | |
// This event does not match format (could have been processed before) | |
if (elements === null) { | |
return; | |
} | |
// Channel is extracted from regex, message is in event description | |
const channel = elements[1]; // first captured group | |
const message = event.description; | |
if (message) { | |
await this.postSlackMessage(channel, message); | |
} | |
// Set new summary to '[!channel] ...' (or '[?channel] ...' if no message was found) | |
const newSymbol = message ? '!' : '?'; | |
const newSummary = '[' + newSymbol + event.summary.substring(2); | |
// Patch calendar event with new summary | |
await this.calendar.events.patch({ | |
calendarId: process.env.EVENT_CALENDAR_ID, | |
eventId: event.id, | |
requestBody: { | |
summary: newSummary | |
} | |
}); | |
} | |
async postSlackMessage(channel: string, text: string) { | |
const data = { channel, text, link_names: true }; | |
const config = { headers: { Authorization: 'Bearer ' + process.env.SLACK_TOKEN }}; | |
const endpoint = 'https://slack.com/api/chat.postMessage'; | |
await this.httpService.post(endpoint, data, config).toPromise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment