response = Map();
//Number of dates to display
dateLimits = {"1","2","3","4","5","6","7","8","9","10"};
//Static timeslots that will be initiatlly available for all days
staticTimeSlots = {"10:00","12:00","15:00","17:00"};
//Dates to exclude from the slots
datesToExclude = {"02-04-2019"};
//Days to exclude from the slots
daysToExclude = {"Sunday","Saturday"};
daynumbertowords = {"1":"Sunday","2":"Monday","3":"Tuesday","4":"Wednesday","5":"Thursday","6":"Friday","7":"Saturday"};
monthWordToNumber = {"Jan":"01","Feb":"02","Mar":"03","Apr":"04","May":"05","Jun":"06","Jul":"07","Aug":"08","Sep":"09","Oct":"10","Nov":"11","Dec":"12"};
//'primary' will retrieve all events from the default calendar of the connected google account 
cal = invokeurl
[
	url :"https://www.googleapis.com/calendar/v3/calendars/primary/events"
	type :GET
	connection:"googlecalendar5"
];
filledSlots = {};
for each  event in cal.get("items")
{
	start = event.get("start");
	dateformat = start.get("dateTime").toList("T");
	dateunits = dateformat.get("0").toList("-");
	revampeddateformat = dateunits.get("2") + "-" + dateunits.get("1") + "-" + dateunits.get("0");
	revampeddatetimeformat = revampeddateformat + "T" + dateformat.get("1");
	datetime = revampeddatetimeformat.toList("+");
	filledSlots.insert(datetime.get("0"));
}
dateList = {};
maxdatelimit = dateLimits.largest();
for each  datelimit in dateLimits
{
	nextdate = zoho.currentdate.addDay(datelimit);
	datesplit = nextdate.toList('-');
	scheduleDate = datesplit.get("0") + "-" + monthWordToNumber.get(datesplit.get("1")) + "-" + datesplit.get("2");
	dayInNumbers = scheduleDate.toDate().getDayOfWeek();
	dayInWords = daynumbertowords.get(dayInNumbers.toString());
	if(!daysToExclude.contains(dayInWords))
	{
		if(!datesToExclude.contains(scheduleDate))
		{
			dateList.insert(scheduleDate);
		}
	}
}
freeSlots = Map();
for each  datelimit in dateList
{
	dateSlotMap = {};
	for each  timeSlot in staticTimeSlots
	{
		scheduledDateTime = datelimit + "T" + timeSlot + ":00";
		if(!filledSlots.contains(scheduledDateTime))
		{
			dateSlotMap.insert(timeSlot);
		}
		if(!dateSlotMap.isEmpty())
		{
			freeSlots.put(datelimit,dateSlotMap);
		}
	}
}
name = {"name":"name","replies":{{"text":"Welcome to Zylker Realtors. I'm Appointment bot"},"Let me schedule an appointment with our representatives for you","I'll need some details from you for that..To start with, your name please?"}};
email = {"name":"email","replies":{{"text":"And your email id ?","field_name":"siq_email","validate":{"format":"email","error":{"Please enter a valid email address"}}}}};
timeSlots = {"type":"date-timeslots","label":"Date & Time Slots","tz":"false","slots":freeSlots,"skippable":false};
slotSelected = {"name":"slot_selected","replies":{"OK! What would be the best time to schedule a chat with one of our experts?"},"skippable":false,"input":timeSlots};
appointmentInfo = Collection();
appointmentInfo.insert(name);
appointmentInfo.insert(email);
//If slots are availble and a slot has been selected by the visitor, an event will be created in google calendar
//If no slots are available, a sorry message will be displayed and a mail will be sent to business owner with the visitor details.
if(!freeSlots.isEmpty())
{
	appointmentInfo.insert(slotSelected);
	response.put("action","context");
	response.put("context_id","createevent");
}
else
{
	response.put("action","context");
	response.put("context_id","noslots");
}
response.put("questions",appointmentInfo);
return response;