Google Calendar Integration
For appointment settings Noojee has the ability to integrate with Google Calendar
Calendar api.
Logged into console.developers.google.com
APIs and auth
Credentials
Created new Oauth 2.0 credential.
Selected 'Web Application'.
Entered
XXXX.clouddialer.com.au as the Authorized javascript origin.
When I started Nj Admin I was already logged in with my google account which suggests that oath will only work for the users own calendar.
<p><input id="btnRun" type="button" value="Run" /></p>
<pre id="output">
Appointment: #input.appointmentdatetime</pre>
<p><iframe frameborder="0" height="600" scrolling="no" src="https://www.google.com/calendar/embed?src=<some calendar owners email address>&ctz=Australia/Sydney" style="border: 0" width="800"></iframe></p>
// this is the default javascript for a screen layout that will appear in the screen layout javascript editor
//
// this file can be found at templates/scripts/dialer/defaultScreenLayout.js
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = 'XXXXXXX';
var gapi_script = null;
// Refer to the JavaScript quickstart on how to setup the environment:
// https://developers.google.com/google.../quickstart/js
// Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any
// stored credentials.
var calEvent = {
'summary': 'Test Event',
'location': '<location>',
'description': '<description>',
'start': {
'dateTime': "2015-09-14T14:00:00+10:00",
'timeZone': 'Australia/Melbourne'
},
'end': {
'dateTime': "2015-09-14T15:00:00+10:00",
'timeZone': 'Australia/Melbourne'
}
// ,
// 'recurrence': [
// 'RRULE:FREQ=DAILY;COUNT=2'
//]
,
'attendees': [
{'email': 'fred@noojee.com.au'},
{'email': 'john@noojee.com.au'}
],
'reminders': {
'useDefault': false,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 10}
]
}
};
$.njDialerPlugin.onConnect = function() {
// lead connected
};
$.njDialerPlugin.onPreview = function() {
// lead loaded in preview mode
if (gapi_script == null)
{
gapi_script = document.createElement('script');
document.head.appendChild(gapi_script);
}
// alert("timezone", Intl.DateTimeFormat().resolvedOptions().timeZone);
// validate the form when the user clicks the 'validate button'
$(function() {
// Change #btnValidate to match the id you gave your button.
$('#btnRun').click(function() {
// add your action here.
// get the selected date/time.
var appointmentDateTimeString = jQuery.njDialerPlugin.getFieldValue("appointmentdatetime");
var appointmentDateTime = new Date(appointmentDateTimeString);
calEvent.start.dateTime = appointmentDateTime.toISOString();
appointmentDateTime.setHours(appointmentDateTime.getHours()+1);
calEvent.start.endTime = appointmentDateTime.toISOString();
try
{
handleAuthClick();
debugger;
var request = gapi.client.calendar.events.insert({
'calendarId': 'primary',
'resource': calEvent
});
request.execute(function(event) {
if (event.hasOwnProperty('code'))
alert("Create failed: " + event.message);
else
alert('Event created: ' + event.htmlLink);
loadCalendarApi();
});
// alert("validation failed");
}
catch (e)
{
alert("Insert failed" + e);
}
});
});
};
$.njDialerPlugin.onVerify = function(disposition) {
// validate lead and return true if ok to save
return true;
};
$.njDialerPlugin.onSave = function(disposition) {
// lead is being saved, an opportunity to notify external services
};
$.njDialerPlugin.onHangup = function() {
// do something here
};
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES,
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadCalendarApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Initiate auth flow in response to user clicking authorize button.
*
* @param {Event} event Button click event.
*/
function handleAuthClick(event) {
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
return false;
}
/**
* Load Google Calendar client library. List upcoming events
* once client library is loaded.
*/
function loadCalendarApi() {
gapi.client.load('calendar', 'v3', listUpcomingEvents);
}
/**
* Print the summary and start datetime/date of the next ten events in
* the authorized user's calendar. If no events are found an
* appropriate message is printed.
*/
function listUpcomingEvents() {
var request = gapi.client.calendar.events.list({
'calendarId': 'primary',
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'orderBy': 'startTime'
});
request.execute(function(resp) {
var events = resp.items;
appendPre('Upcoming events:');
if (events.length > 0) {
for (i = 0; i < events.length; i++) {
var event = events[i];
var when = event.start.dateTime;
if (!when) {
when = event.start.date;
}
appendPre(event.summary + ' (' + when + ')')
}
} else {
appendPre('No upcoming events found.');
}
});
}
/**
* Append a pre element to the body containing the given message
* as its text node.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}