2014-09-01

Google Apps Script - cyber life automation

What it is all about?

Internet goodies - cloud apps like Dropbox, Google Drive, GMail, even Facebook and social media - affects our style of life, influence what we do and how we do it. But hey! New possibilities brings new responsibilities and new work around them. Often you want to publish the same content on Facebook and Twitter on any other social media be notified when something has changed on shared Dropbox folder or move stared article in Pocket/Readability to Evernote or Google Drive.
There are some apps like iftt.com or zapier which can help you automate some tedious tasks accross your different internet services, but they have some limitations. First of all they let you create integration receipts based on following formula:
IF something-happens-on-service1 THEN do-something-at-service-2


Obviously - this receipts can’t fulfill the whole spectrum of some of your needs. So.. when your needs exceed capabilities of those apps (or you don’t want to have yet-another-cloud-app on board) - go for a big gun. And a really big gun is the Google Apps Scripts!
As stated here - Google Apps Script is a JavaScript cloud scripting language that provides easy ways to automate tasks across Google products and third party services and build web applications.

Example

I don’t use any Facebook mobile app. These apps are time eaters and are very heavy for my phone. Most of all - checking nervously Facebook news is against my daily workflow. But I would like to have some sort of notification when someone comments my updates on social media. It is kind to respond quickly for such situations.
For such notification I use Pushbullet service. With Pushbullet you can be notified on any device you have installed Pushbullet app: Android, IPhone, MacOS, Windows and Linux.
And how to forward unread Facebook notifications to PushBullet?  Here is a code I use for that, written in Google Apps Script, simple and easy:
var FACEBOOK_TOKEN = "your-secret-access-token-to-facebook";
var PUSH_BULLET_TOKEN = "your-secret-access-tokent-pushbullet";

function forwardMeFacebookNotifications() {
 var response = UrlFetchApp.fetch(
   FACEBOOK_API_URL + "/me/notifications?access_token="+FACEBOOK_TOKEN,
   {
   "method" : "get",
   "contentType" : "application/json",
   }).getContentText();
 response = Utilities.jsonParse(response);
 var notifications = response.data;

 for (var i in notifications) {
   pushLinkToPushBullet(notifications[i].title, notifications[i].link);
   UrlFetchApp.fetch(
     “https://graph.facebook.com/v2.1/" + notifications[i].id +"?access_token="+FACEBOOK_TOKEN,
     {
     "method" : "post",
     "payload" : { "unread" : "0" }
     });
 }
 
}

function pushLinkToPushBullet(title, link) {
 var digest = "Basic "+Utilities.base64Encode(PUSH_BULLET_TOKEN+":");
 var options = {
   "method" : "post",
   "payload" : {
     "type" : "link",
     "title" : title,
     "url" : link
   },
   "headers" : {
     "Authorization": digest
   }
 };
 var push_bullet_url = "https://api.pushbullet.com/v2/pushes";
 UrlFetchApp.fetch(push_bullet_url, options);
}


This script consist of two simple REST calls, one for Facebook REST API and one for Pushbullet API. I wll not cover how to connect to those APIs, but you should know that for each you should obtain the access token (see 2 first line of the script) - on how to get those, just read docs and tutorials provided by each services.

How to start messing with Google App Scripts?

Before messing with your Google Apps Scripts you should connect Google Apps Script Editor with Google Drive. Go to your Google Drive, and point to Connet More Apps option as below:
Screenshot from 2014-09-01 23:10:05.png

What can you do with Google Apps Scripts?

Scripts has built-in services you can inline in your code:
  • UrlFetchApp - you can see above in the script. I allows you to call any URL with http/https protocol, especially REST services.
  • Any Google services (such ass Calendar, GMail, Drive and so on)
  • Some utilietiles like encoding/decoding Base64, JSON, XML, etc.

With these building blocks possibilites are pretty powerfull. Stay tuned! In next few posts I will reveal some cool receipts with Google Apps Scritps as a main spice.