-
Notifications
You must be signed in to change notification settings - Fork 100
Sending text message alerts using Google Voice
If we want to continue to leverage nflgame's ability to gather statistics from games currently being played, the next logical step is to devise a way to send alerts. Alerts are typically sent via email or text message (SMS)—in this tutorial we will focus on sending the latter kind of alert.
Before moving on, we have to outline the dependencies required in order
to use nflgame.alert
to send texts via Google Voice.
First, you'll need to have a Google Voice account,
which is used to send text messages. Signing up for one is free (sending text
messages is also free). Second, you'll need to install
pygooglevoice which is used to talk
to Google Voice. To install pygooglevoice, download the latest
release (called
"Python source code"), then extract it. You then have to run python2 setup.py install
in the directory where setup.py
is located. (i.e., where you
extracted the downloaded file.)
Finally, you'll need at least version 1.0.6 of nflgame, which is the first
version to contain the nflgame.alert
module.
(Warning: Google Voice caps the number of texts you can send in some
interval. The wisdom of the Internet seems to report 200 text messages in 24
hours, but this is merely a rough estimate. Sending a lot of text messages in
rapid succession could also prevent you from sending more. In lieu of Google
Voice, nflgame.alert
also provides a way of sending SMS messages using
an SMTP server such as your GMail account—but you'll need to know the
cell phone carrier of the recipient. There is more explanation and
examples in nflgame.alert's API
documentation.)
There are only two functions that you need from the nflgame.alert
module:
nflgame.alert.google_voice_login
and nflgame.alert.sms
. The first function
only needs to be called once and allows your Python program to log into and
access your Google Voice account. The second function sends an SMS message to
any phone number.
So how about an example?
>>> import nflgame.alert
>>> nflgame.alert.google_voice_login('YOUR GMAIL ADDRESS', 'YOUR PASSWORD')
>>> nflgame.alert.sms('YOUR PHONE NUMBER', 'Hello, world!')
If you run the above code after adding in your information, you should receive
a text message momentarily with a message that reads Hello, world!
. Cool, eh?
Also, if you noticed—we didn't do anything with NFL statistics! You could
use nflgame.alert
to send alerts for any reason whatsoever. But in this
tutorial, we'll stick to NFL updates.
Back in the days before Digital Video Recorders were prevalent, if you missed an NFL game, you were probably out of luck. In my circle of friends, if someone was missing a Patriots game, someone else who got to see it would often send text messages periodically updating the friend who couldn't watch the game.
Now we can do that automatically with the nflgame.live
and nflgame.alert
modules. (Note: Please see the wiki page on the
nflgame.live
for help on using it. Notably, it requires that
pytz is installed.)
For instance, this example will check for game scores every ten minutes (600 seconds) and send you a text message with the scores of every game that is being played: (Remember to fill in your phone number and Google Voice account information.)
import nflgame
import nflgame.alert
import nflgame.live
phone = 'YOUR PHONE NUMBER'
email = 'YOUR GOOGLE EMAIL ADDRESS'
passwd = 'YOUR GOOGLE PASSWORD'
def cb(active, completed, diffs):
scores = []
for g in active:
fmt = '{0} ({1}) vs {2} ({3})'
scores.append(fmt.format(g.home, g.score_home, g.away, g.score_away))
msg = '\n'.join(scores)
nflgame.alert.sms(phone, msg)
nflgame.live.run(cb, active_interval=600)
We could also adapt our example to send a special text declaring a winner whenever a game has been completed:
import nflgame
import nflgame.alert
import nflgame.live
phone = 'YOUR PHONE NUMBER'
email = 'YOUR GOOGLE EMAIL ADDRESS'
passwd = 'YOUR GOOGLE PASSWORD'
def cb(active, completed, diffs):
for g in completed:
msg = '{0} beats {1}! Final score: {2} to {3}'
msg = msg.format(g.winner, g.loser, g.score_home, g.score_away)
nflgame.alert.sms(phone, msg)
scores = []
for g in active:
fmt = '{0} ({1}) vs {2} ({3})'
scores.append(fmt.format(g.home, g.score_home, g.away, g.score_away))
msg = '\n'.join(scores)
nflgame.alert.sms(phone, msg)
nflgame.live.run(cb, active_interval=600)
Since games only show up in completed
once and never again, you will only get
one text message for each game that finishes.