Fanatic Badge on Stack Overflow — Part Two: Email Notification
Send automatic emails after a long period of inactivity on Stack Overflow
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:
- Obtain the Stack Exchange access token
- Retrieve and validate the last access date
- 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.
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:
pip install requests_oauthlib
… and append the library to the requirements.txt
file (to be able to use it on Heroku):
...
requests_oauthlib==1.3.0
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:
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:
$ export STACK_EXCHANGE_CLIENT_ID='3***1'
The script will print in the console output the authorization URL, needed to access to obtain your access_token
. After running it:
python3 stack_exchange_api.py
… you should see in the console output a message like below:
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.
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:
pip install requests
Plus the requirements.txt
file:
...
requests==2.20.0
In the stack_exchange_api.py
file that we’ve created above, we’ll add a new method, get_user_details()
. Le code:
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:
$ export STACK_EXCHANGE_KEY='j4d******************Q(('
$ export STACK_EXCHANGE_ACCESS_TOKEN='MIe******************Q))'$ python3 stack_exchange_api.py
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.
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:
$ heroku addons:create sendgrid:starter
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:
$ heroku config:set SENDGRID_API_KEY=xxxx_api_key_xxxx
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:
$ pip install sendgrid
$ pip install python-http-client
… and the dependencies to the requirements.txt
file:
...
sendgrid==6.3.1
python-http-client==3.2.7
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:
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:
It’s a wrap! Push the code to Heroku and we’re done!
$ git add .
$ git commit -m "Deploy to Heroku - part two"
$ git push heroku master
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.