Search code examples
google-apidialogflow-esfulfillment

Dialogflow - Fulfillment - Add a list of Intendees


i'm trying to make a small chatbot which can take appointment. I didn't find a code allowing me to add the intendees dynamically.

My agent have an array of email :

agent.parameters.invites[0] => email 1
agent.parameters.invites[1] => email 2 ...

this is my function :

    function createCalendarEvent (dateTimeStart, dateTimeEnd, room, calendarId, agent, organisateur,objet) {  return new Promise((resolve, reject) => {
    calendar.events.list({  // List all events in the specified time period
      auth: serviceAccountAuth,
      calendarId: calendarId,
      timeMin: dateTimeStart.toISOString(),
      timeMax: dateTimeEnd.toISOString()
    }, (err, calendarResponse) => {
      // Check if there exists any event on the calendar given the specified the time period
      if (err || calendarResponse.data.items.length > 0) {
          if (err) {agent.add(err.toString());}
        reject(err || new Error('Heure demandée en conflit avec un autre RDV.'));
      } else {

        calendar.events.insert({ auth: serviceAccountAuth,
          calendarId: calendarId,
          resource: {summary: objet + ' - salle : '+ room + ' - organisé par  ' + agent.parameters.orga,
            start: {dateTime: dateTimeStart},
            end: {dateTime: dateTimeEnd},
            description: objet,
            location: room,
            //source : {title : "JPV objet"}
            //organizer : {'email': organisateur},
            attendees: [{'email': agent.parameters.invites[0],"organizer": true}, {'email':agent.parameters.invites[1]}],
            sendUpdates :'all', 
            sendNotifications: true,
          }
        }, (err, event) => {
          err ? reject(err) : resolve(event);
        }
        );

      }
    });
  });
}

can you help me ?

Regards,


Solution

  • From what you're showing, agent.parameters.invites is an array, and you need an array that you're passing as part of the attendees parameter.

    In JavaScript, a great way to do this is to use the Array.map() function, which calls a function on each value in an array and returns a new array with the result of calling each function. The function is called with the value of that element from the source array and (optionally) the index.

    I haven't tested this, but something like this should work

    let attendees = agent.parameters.invites.map( (value, index) => {
      return {
        email: value,
        organizer: (index === 0)
      }
    });
    

    and then using this attendees array as the value of the attendees parameter in your call.

    The function in this case takes each value and the index, and returns a new object with the value set for the email parameter and the organizer set to true if this is the first item (and false otherwise).