Google Sites‎ > ‎Sample Scripts‎ > ‎

Subscribe to Announcements for Sites viewers


With many thanks to Romain Vialard who provided the basis for this script here http://www.google.com/support/forum/p/apps-script/thread?tid=381db8a3c401a7cd&hl=en
(see also the update at the bottom of the page)




romain vialard
Level 6
3:22 PM
Below is a script for Google Sites to let any viewer subscribe to changes in an Announcements page.

1- Replace the text "https://sites.google.com/a/pjrprojects.co.uk/sitesguide/test-bench/test-news" by the URL of your Announcements page.
2 - Replace the text "owner@gmail.com" with your email address
2- Add links to functions subscribe() & unsubscribe() [and sendSubscriptions()] in your page as required.
3- Set a Time-driven trigger on sendNews() to send the updates to the subscribers

And done!


I have modified his original script a little so that a subscriber receives a confirmation email on subscribing / un subscribing. Posts are batched rather sent individually and I have also added a function to email a list of current subscribers to a specified email address (sendSubscriptions()). Also added an error trap to send email to OWNEREMAIL if an error occurs.

UPDATE:
When the displayed script was first written, it was possible using  Session.getActiveUser().getEmail() to get the email address of anyone provided they were signed in with a Google account. For privacy reasons Google changed this so that this now only works for a Site and users that are in the same Google Apps domain.
Go to the bottom of this page to see how Romain has got round this issue.


A little more detail for those less familiar with adding scripts to a Site

Here are some general instructions on how to add a script to a Site.

Then for this specific script:
To allow people to subscribe to the page, you need to create a link to the subscribe() function.
To allow people to unsubscribe, create a link to the unsubscribe() function.

Finally, to make the whole thing work, you need to go back to the script editor, click on Triggers>Current Script's Triggers. Click on the link to add a trigger
Uner 'Run', select 'sendNews'. Under Events select 'Time Driven' and then select the time interval that you require for sending out the subscriptions e.g. Day-Timer, Midnight to 1am
Click Save
Click Authorise, Grant access
Close the editor

There is another function sendSubscriptions() which you can use to send a list of who has subscribed to the email address specified by the variable OWNEREMAIL.

The Script

// Subscribe / Unsubscribe to News page
// Author : Phil Ridout based on an idea by Romain Vialard
// Version: 1.0, 6th November 2010

function sendChanges_(type,emailAddress)
{
  /////////////////////////////////////////////////////////////////////////////////
  //  PARAMETERS TO BE SET AS APPROPRIATE
  /////////////////////////////////////////////////////////////////////////////////
  var ANNOUNCEPAGEURL 'https://sites.google.com/a/pjrprojects.co.uk/sitesguide/test-bench/test-news';
  var OWNEREMAIL      'owner@gmail.com';
  /////////////////////////////////////////////////////////////////////////////////   
  // END OF PARAMETERS
  /////////////////////////////////////////////////////////////////////////////////
  try{
  var siteName SitesApp.getSiteByUrl(ANNOUNCEPAGEURL).getTitle();
  var newsfeed SitesApp.getPageByUrl(ANNOUNCEPAGEURL);
  var newsfeed_title newsfeed.getTitle();
  switch (type)
    {
      case 'subscriptions':
      var mailing ScriptProperties.getProperty("mailing");
        var body "Current subscribers are: "+mailing;
      MailApp.sendEmail(OWNEREMAIL"Subscribers for: "+newsfeed_title+" in "+siteNamebody{htmlBodybody});
      break;
    case 'subscribe':
      var body "You are now subscribed to Announcement page "+newsfeed_title+" in "+siteName;
      MailApp.sendEmail(emailAddress"Subscription Confirmed:"+newsfeed_titlebody{htmlBodybody});
      break;
    case 'unsubscribe':
      var body "You are now unsubscribed from Announcement page "+newsfeed_title+" in "+siteName;
      MailApp.sendEmail(emailAddress"unSubscription Confirmed:"+newsfeed_titlebody{htmlBodybody});
      break;
    case 'sendNews'
      var current_time new Date();
      
      newsfeed newsfeed.getAnnouncements();
      var last_check ScriptProperties.getProperty("last_check");
      if(last_check == null)
      {
        last_check current_time;
        ScriptProperties.setProperty("last_check"last_check);
      }
      var body "";
      
      // check each announcement in turn and if new since last time checked, add to digest to be emailed
      
      var somenews false;
      for(var 0newsfeed.lengthi++)
      {
        if(newsfeed[i].getLastUpdated().getTime(new Date(last_check).getTime())
        {
          somenews true;
          var title newsfeed[i].getTitle();
          var content newsfeed[i].getHtmlContent();
          var body body +
                     "<b> "title "</b><br>" 
                     content 
                     "<br>----------------------------------<br><br>";
        }
      }
      
      // now mail digest to subscribers
      
      if (somenews)
      {   
      var mailing ScriptProperties.getProperty("mailing");
      if(mailing != null)
      {
        var emails mailing.split(",");
        for(in emails)
        {
          if(emails[j!= "")
          {
            MailApp.sendEmail(emails[j]"Updates from "+newsfeed_titlebody{htmlBodybody});
          }
        }
      }
      //end if somenews
      ScriptProperties.setProperty("last_check"current_time);
      break;
    }//End Switch
// end try
catch(err)
{
  // if error occurs while runnning script, send email to script owner
  var body "Error "+err+" in Script Subscribe to News Pages";
  MailApp.sendEmail(OWNEREMAIL"Error in Site Script"body{htmlBodybody});
}
}
        
function sendNews()
{
  sendChanges_("sendNews","");
}

function sendSubscriptions()
{
  sendChanges_("subscriptions","");
}
        
function subscribe()
{
  var email Session.getActiveUser().getEmail();
  var mailing ScriptProperties.getProperty("mailing");
  if(mailing == null)
  {
    ScriptProperties.setProperty("mailing"email);
  }
  else
  {
    if(mailing.indexOf(email== -1)
    {
      ScriptProperties.setProperty("mailing"mailing+","+email);
    }
  }
  sendChanges_("subscribe",email);
}

function unsubscribe(){
  var email Session.getActiveUser().getEmail();
  var mailing ScriptProperties.getProperty("mailing");
  if(mailing != null){
    mailing mailing.replace(email,"");
    ScriptProperties.setProperty("mailing"mailing);
  }
  sendChanges_("unsubscribe",email);
}


Comments