1

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,

Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43
  • I'm not sure I understand what the problem is. What is the user saying, and how are you expecting to map that to email address? Or are they saying the email address? What is missing? Is there an error with your current code? What does the Intent look like? – Prisoner Mar 13 '19 at 13:16
  • Hi, i'm sorry for my english. well, for example, the bot ask user to say the email of attendees. with my code, the bot understand only 2 email because of : attendees: [{'email': agent.parameters.invites[0],"organizer": true}, {'email':agent.parameters.invites[1]}], I just want to know how i can make it more dynamical ? – Jean-Pierre Vuong Mar 13 '19 at 13:36

1 Answers1

0

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).

Prisoner
  • 49,922
  • 7
  • 53
  • 105