google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

Google Calendar Webhooks with Node.js

Open cris-guedes opened this issue 2 years ago • 2 comments

What you're trying to do

  • the goal is listening to the customer event response

What is happening

  • I made one implementation of push notifications using the google calendar API, but all events are triggered when I make an update only in one event

  • I'm creating all events using a main account and putting the customer as attendees in the creation process, for each event I have only one customer as an attendee, the goal is listening to the customer event response but when some customer responds to the event all, other events are triggered too and my endpoint receives calls from all events that I already create

Calendar event creation code:

async createCalendarEvent(params: CalendarProvider.CreateEventParams) {
    if (!this.calendar) return
    try {
      const event = {
        summary: params.summary,
        description: buildDescription({
          eventName: params.eventName,
          sessionDuration: params.sessionDuration,
          sessionLink: params.sessionLink
        }),
        location: params.location,
        start: {
          dateTime: params.startDate,
          timeZone: params.timezone
        },
        end: {
          dateTime: params.endDate,
          timeZone: params.timezone
        },
        ...(params.recurrence && {
          recurrence: buildRecurrence(params.recurrence)
        }),
        attendees: params.participants
      }

      const calendarEvent = await this.calendar.events.insert({
        calendarId: DEFAULT_CALENDAR_ID,
        requestBody: event
      })

      const watch = await this.calendar.events.watch({
        requestBody: {
          id: uuid4(),
          type: 'web_hook',
          address: `${this.webhookBaseEndpoint}/${calendarEvent.data.id}`
        },
        calendarId: DEFAULT_CALENDAR_ID
      })

  
      return calendarEvent?.data
    } catch (error) {
      console.log(error.response.data.error)
      console.log(error)
      Sentry.captureException(error)
    }
  }

webhook endpoint code:

router.post('/calendar-webhook/:eventid', async (req, res) => {
  try {
    const eventId = req.params.eventid

    await makeSyncCalendarResponse().sync({ eventId })

    return res.sendStatus(200)
  } catch (err) {
    Sentry.captureException(err)
  }
}) 

cris-guedes avatar Feb 08 '23 20:02 cris-guedes

Hi @cris-guedes, in this example, you create an event and watch what happens to it. Next, in the webhook, you will get this event again in the function makeSyncCalendarResponse().sync({ eventId }) by eventId. Then you will know what happened to the event, whether it was deleted or changed. Let me know if I'm wrong.

But I have another question. If the user adds a new event to the Google calendar from the website, how do I find out which event was added?

DenysPoliarush avatar Mar 26 '23 15:03 DenysPoliarush

Hi @cris-guedes I am attempting to achieve a similar goal where I'd like one channel per event and have that channel only be updated when that event is changed, not all events. Did you end up finding any solution?

jakechao-zeal avatar May 23 '23 02:05 jakechao-zeal