How to receive Stack Overflow notifications on your phone and smartwatch (v 2.0)

StackOveflow Pebble UpdateA while back I described how you can receive Stack Overflow notifications on your phone and smartwatch by reading RSS feed provided by StackOvedlow API (v1) and connecting it to PushOver push notification service (and their Android app and ultimately Pebble Smartwatch via awesome IFTTT.

Since then Stack Exchange retired version 1 of their API (and besides it didn’t provided all the information needed, e.g. total reputation points) so I was looking for an alternative. Fortunately StackOverflow API v2 provides very extensive set of functions. One call I was looking for is

api.stackexchange.com/2.2/users/[userid]?site=[site]

Where [userid] is your Stack Exchange User ID and [site] is the Stack Exchange site you’re interested in. For example for me call to

https://api.stackexchange.com/2.2/users/961695?site=stackoverflow

provides a nice JSON output:

{"items":[
   {"badge_counts":{"bronze":45,"silver":12,"gold":4},
   ...
  "reputation_change_year":3937,
   ...
   "reputation_change_day":45,
   "reputation":20018,
   ...
   "display_name":"Yuriy Galanter"],
   ...
}

Unfortunately at the time of this post IFTTT didn’t support webhooks to read JSON data, so I decided to try something on my own. To periodically make an API call, a Windows Service is probably in order or a scheduled task but as a proof of concept I decided to put together a simple jQuery page.

A call is pretty straightforward:

$.getJSON('http://api.stackexchange.com/2.2/users/961695?site=stackoverflow')
   .done(function(oUser) {
       var sMessage = '-----------------------'
       sMessage += '\nToday: ' + oUser.items[0].reputation_change_day;
       sMessage += '\nTotal: ' + oUser.items[0].reputation;
       sMessage += '\n-----------------------'
    })
)

In this case I am only interested daily reputation change and total reputation, so I am extracting just that from object returned by API and constructing a string message. Now all we need is make it into a push notification. Fortunately Pushover provides a very simple API that makes this possible with a single POST request. All you need to do is register your application to get the token and provide it along with user token and message in the POST data. Adding the below call inside of .done response above, after the message is constructed:

$.post("https://api.pushover.net/1/messages.json",
   {
      token: '[Your Pushover application token]',
      user: '[Your Pushover user token]',
      message: sMessage,
      title: '**SO Rep Update**'
   }

And that’s it! Place the above calls into setInterval() repetition, add some error handling (and probably storing current reputation value in a localStorage to compare with freshly retrieved one to send notification only in case of change) and you’re done.

The example above pull Stack Oveflow data. but really there’s no limits in what can be pushed to your phone and smartwatch.

Leave a Reply

Your email address will not be published. Required fields are marked *