Google Drive allows you to easily create and edit Documents, Presentations, Spreadsheets, Forms and Drawings. But not everyone might be familiar with the Google App Script integration functionalities similar to the Visual Basic scripting integration within Microsoft Office macro’s.
Recently, I found this tutorial to link GMail to Google Calendar written by Romain Vialard.
Based on this tutorial, I made the custom script below in order to get a free SMS’s from Google on every new unread e-mail marked as ‘Important’ within GMail. Setting up this little script will give you a brief indication of the power and possibilities of the Google App Script integration.
- In Google Calendar, register your mobile phone within the ‘Settings’ > ‘Mobile Setup’, to enable SMS notifications. Sending SMS’s from within Google is free of charges, but it might be limited to 50 messages per day.
- In Gmail, create a new label named 'NbSMS' (Notified by SMS). Once a new mail has been notified by SMS, this label will be assigned to the mail in order to make sure the notification is send out only once and the mail will be ignored by the script once this label is assigned.
- In Google Drive, create a new Spreadsheet, give it any name.
- Choose the menu ‘Tools’ > ‘Script Editor’.
- Choose ‘Create script for: Blank Project’.
- Copy and paste the following script:
function sendText() {
Logger.log('Start of sendText script');
var today = new Date();
var nowHour = today.getHours();
var startTime = 8;
var endTime = 24;
Logger.log('The SMS notification will only run between: ' + startTime + ' and ' + endTime + ', current hour: ' + nowHour);
if (nowHour <= startTime || nowHour >= endTime) {
Logger.log('Quite time, no SMS notification will be sent between: ' + startTime + ' and ' + endTime + ', current hour: ' + nowHour);
} else {
// var events = cal.getEvents(new Date(startDateAndTime), new Date(endDateAndTime));
//based on https://developers.google.com/apps-script/articles/gmail_filter_sms
var notifiedBySmsLabel = 'NbSMS';
var unreadPriority = GmailApp.getPriorityInboxUnreadCount();
var unreadsFound = 0;
Logger.log("Number of unread emails in your Priority Inbox : " +
GmailApp.getPriorityInboxUnreadCount());
if (unreadPriority > 0) {
var threads = GmailApp.getPriorityInboxThreads();
//threads.refresh();
var now = new Date().getTime();
var alreadyNotified = false;
for(i in threads){
if (!threads[i].isUnread()) {
alreadyNotified = true;
if(unreadsFound >= unreadPriority) {
break;
} else {
continue;
}
} else {
++unreadsFound;
}
var threadLabels = threads[i].getLabels();
for(y in threadLabels) {
if(threadLabels[y].getName() == notifiedBySmsLabel) {
alreadyNotified = true;
break;
}
}
if (!alreadyNotified) {
var smsText = 'Mail: '+threads[i].getFirstMessageSubject() + ', from: ' + threads[i].getMessages()[0].getFrom();
smsText = smsText + ' ' + threads[i].getMessages()[0].getPlainBody();
Logger.log('Event with SMS will be created with content: ' + smsText);
var event = CalendarApp.createEvent(smsText,
new Date(now+60000),
new Date(now+60000));
event.setDescription(smsText);
event.addSmsReminder(0);
var label = GmailApp.getUserLabelByName(notifiedBySmsLabel);
label.addToThread(threads[i]);
}
}
//threads.refresh();
Logger.log('All important messages treated and label ' + notifiedBySmsLabel + ' applied.');
}
}
}
- Choose the menu ‘Resources’ > ‘Current project’s triggers…’ and add a new trigger.
- Select the function ‘sendText()’ > Events: ‘Time-driven’ > ‘Minutes timer’ > ‘Every 5 minutes’, and save the trigger.
- Save the script.
- Click the Run ► icon. A pop-up opens asking you for your authorization to access the Gmail and Google Calendar services.
- Click the Authorize button.
- Click the Run ► icon again.
- Choose the menu ‘View’ > ‘Logs’ to see the log output.
- Debugging is possible using the bug icon. Set some breakpoints by clicking on the line number next to the script.
- After one minute, you should receive a text on your mobile device, containing the subject of the important unread emails within GMail.
- To see what other functionalities are available within GMail, Google Calendar en other Google services, see the Google App Script reference guide.
- Some other interesting tutorial to work with Google Calendar events is available in this blog post.
Update 09/04/2014: optimized the labeling to only apply the label to messages which were not yet labeled, instead of all messages in priority inbox.
Update 21/10/2015: Google no longer supports to get notified by SMS for a calendar item. So the approach above won’t work any longer (calendar item will be created, but no sms will be received).
This blog is nice and interesting. Thanks for sharing those information it is really well and good. Java SMS Script
ReplyDelete