Fanatic Badge on Stack Overflow — Part Two: Email Notification

Send automatic emails after a long period of inactivity on Stack Overflow

Alexandru Somai
codersDoRead 👨🏻‍💻📚
5 min readMar 9, 2018

--

In this step-by-step guide we’ll set-up a scheduler to notify us, via email, if we haven’t logged into Stack Overflow for at least twelve hours. Thus, we’ll know that something went wrong with our automatic login process, and we’ll be able to save our login streak.

This blog post is a continuation of the first part, in which we’ve set-up an automatic login process on Stack Overflow:

How can we do this?

We’ll use the Stack Exchange API to retrieve the last access date on Stack Overflow. If the difference between today’s date and the last access date is greater than twelve hours, we’ll be notified via email.

The steps are the following:

  1. Obtain the Stack Exchange access token
  2. Retrieve and validate the last access date
  3. Send a notification email if more than twelve hours have passed since our access date on site

The complete source code is available here.

Step 1. Obtain Stack Exchange access token

First things first. To use the Stack Exchange Authentication API, you must register an application on their platform. For this, you must complete the following form: https://stackapps.com/apps/oauth/register.

Register the Application on Stack Exchange

Two items must be exactly the same as in the above screenshot:

  • Set OAuth Domain to stackexchange.com
  • Activate the Enable Client Side OAuth Flow checkbox

After submitting the form, you should receive a Client Id and a Key. Write these two down, because we’ll use them later when making calls to the Authentication API.

In our script, we’re going to use the implicit OAuth 2.0 flow.

The first step is to open a new window at https://stackexchange.com/oauth/dialog with the query string parameters: client_id, scope and redirect_uri. We’ll make use of the requets_oauthlib; therefore, install it:

… and append the library to the requirements.txt file (to be able to use it on Heroku):

In the project that we’ve created in the first part of this tutorial, let’s create a new file called stack_exchange_api.py. The file shall define a method called get_authorization_url(), as below:

Obtain the authorization URL from Stack Exchange API

Before running it, we must set our client_id as environment variable. You’ve got the Client Id after registering your app on Stack Exchange:

The script will print in the console output the authorization URL, needed to access to obtain your access_token. After running it:

… you should see in the console output a message like below:

The Authorization URL on Stack Exchange

Access, from the browser, the URL that you got in the console output. After authorizing the application, you will be redirected to the redirect_uri, with the access_token in the hash.

The Access Token on Stack Exchange

This is your Access Token that you’ll use in the next step. Write it down.

Step 2. Validate the last access date

Retrieve user details

Using the Key and the Access Token that we’ve got at step one, we’ll access the /me method. For making the request programmatically, we’ll use requests lib. You know the standard procedure:

Plus the requirements.txt file:

In the stack_exchange_api.py file that we’ve created above, we’ll add a new method, get_user_details(). Le code:

Obtain user details from Stack Exchange API

If we run this method, it will return (and print) a JSON containing the details of our stackoverflow user. The field of interest from the returned object is last_access_date. It’s the date, expressed in unix epoch time, when we accessed the Stack Overflow page last time.

Set the Key and Access Token as environment variables, before running the script:

Validate last access date

Next, we will create another method for validating the last_access_date. It will check whether the user has logged in for the last delta_time hours or not.

Check whether the user has logged into Stack Overflow site in the last delta_hours period

We will call this method on a regular basis (let’s say hourly), checking if we’ve logged into Stack Overflow in the last twelve hours (delta_time=12). If the function returns False, we’ll trigger an email to notify us that something went wrong with the login process. All these in the next step.

Step 3. Email notification

We will use SendGrid to send emails from the Heroku platform.

SendGrid is an add-on for providing scalable email delivery and analytics for apps.

In the working directory, firstly install the add-on:

Next, obtain a SendGrid API key. Follow the steps documented here: https://devcenter.heroku.com/articles/sendgrid#obtaining-an-api-key.

After you’ve got the API key, you have to set it as environment variable:

In the working directory, install the sendgrid library. Note that sendgrid requires the dependency python-http-client , so let’s install that one, as well:

… and the dependencies to the requirements.txt file:

In our project, we’ll create a new helper class that will provide a basic API to send emails: send_email(subject, content). Let’s create a new file sendgrid_helper.py, with the following implementation:

Helper to send emails from Heroku platform using SendGrid

The above, will send a notification to our Stack Overflow email.

The final touch: write a new hourly scheduler that will verify if we’ve logged into Stack Overflow. If we haven’t logged in for at least twelve hours, it will notify us via email:

Notify via email if we haven’t logged into Stack Overflow for at least twelve hours

It’s a wrap! Push the code to Heroku and we’re done!

This blog post is an improvement to the first part, where we’ve set-up a basic automatic login process into Stack Overflow. If we haven’t logged into Stack Overflow for at least twelve hours, we’ll retrieve an email notification knowing that something went wrong.

If you’ve found this guide useful, or if you have any questions or suggestions, let me know in the comments below. Any improvements to the source code are welcomed here.

--

--