Want a more modern method for simple logging and notifications? Twitter is very popular, but it normally too "noisy" for me to follow for social messages. Add a bit of tech and now I have a reason to check Twitter at least once a day, or follow the events on my phone.
I had read articles in both
Linux Journal and
The MagPi Magazine about engineering minds using Twitter to receive notifications when certain events occur. I decided to take this a step further and create a tiny Twitter logger script for all of my Raspberry Pis. I now receive notifications for all kinds of events, such as:
- Daily "alive" notifications
- Reboots, power cycles
- Changes in dynamic IP address
- GPIO event triggers (think security alarm, garage door, sprinkler system, etc)
- And anything else that fancies me at the moment
Code
How complicated is the Python? Easy. Only 3 lines (ignoring the line wrapping and commented debug code):
import twitter
api = twitter.Api(consumer_key='XXX',
consumer_secret='XXX',
access_token_key='XXX',
access_token_secret='XXX')
status = api.PostUpdate('This is my status update')
# print status.text
To use this code you will need to first generate your Twitter App API Access Keys and also install the python-twitter Python package.
Twitter App API Access Keys
Step 1 - Twitter Account: So how does one get started? Well first, you need a Twitter account. I assume you already have one, or at least don't need help creating one.
Step 2 - Mobile Phone: Unfortunately, your code will only be able to
read status updates, unless you tie a mobile phone number to your account. Once you have validated your mobile phone, you will then be able to
write/post status updates as well. Go to your account
Settings and select the
Mobile section. Add your mobile phone number, and wait for the validation code to appear on your phone. (note: I did attempt to try a couple of free
burner mobile phone numbers, none of which ever received the validation code)
"You must add your mobile phone to your Twitter profile before granting your application write capabilities. Please read https://support.twitter.com/articles/110250-adding-your-mobile-number-to-your-account-via-web for more information"
Step 3 - Twitter App: Next, we need to create a Twitter App. This is nothing more than a method to get access to your api keys. Browse to
https://apps.twitter.com/ and click the "
Create New App" button.
Fill in the Name, Description and Website fields, then accept the agreement and continue.
The important field is the Name field as it has to be unique among all Twitter Apps. I just used my twitter account name, as I assumed no one else would be using that for an app name.
The Website field has to be populated, but won't be used. Put some temporary website and continue.
The Callback URL can be ignored.
Step 4 - Access Level: After the Twitter App has been created, find the
Application Settings section and modify the
Access level option by clicking the
Modify App Permission link. Change the access level from
Read Only to
Read and Write. If you receive a warning about your mobile phone number, go back to step 2.
Step 5 - Access Keys: Finally, find the
Application Settings section again and click the
Manage Keys and Access Tokens link. You will find the
consumer keys at the top of the page. Under the
Your Access Token section, click the
Create My Access Token button to generate the access tokens. If it isn't obvious the keys match the code variables as such:
consumer_key = "Consumer Key (API Key)"
consumer_secret = "Consumer Secret (API Secret)"
access_token_key = "Access Token"
access_token_secret = "Access Token Secret"
Now with your access keys, you can begin using the code above to update your twitter status with various notifications.
python-twitter
The
python-twitter package "provides a pure Python interface for the Twitter API." To install the python-twitter package, simply use
pip.
$ sudo pip install python-twitter
That was easy.
Check Status Code
The previous code showed how we can post a new update, but what about getting our current status, or someone else's?
import twitter
api = twitter.Api(...)
# get my timeline
statuses = api.GetUserTimeline()
# get a specific user's timeline:
# statuses = api.GetUserTimeline(screen_name=SCREEN_NAME)
# show latest message only:
print statuses[0].text
# show all returned messages:
for s in statuses:
print s.text
Catch Errors
There are a few important errors that you will probably run across, and should handle:
Authentication failure: (
check your keys, especially for accientally copied space characters)
twitter.error.TwitterError: [{u'message': u'Could not authenticate you', u'code': 32}]
Too long of a message: (
shorten your message)
twitter.error.TwitterError: [{u'message': u'Status is over 140 characters.', u'code': 186}]
Duplicate message: (
don't repeat messages or add a unique id, like time)
twitter.error.TwitterError: [{u'message': u'Status is a duplicate.', u'code': 187}]
User does not exist: (
check the user name you are trying to query)
twitter.error.TwitterError: [{u'message': u'Sorry, that page does not exist', u'code': 34}]
test
Cronjob
I updated the twitter script to accept a message from the command line, and then added it to my cronjob:
@daily /usr/bin/python /usr/local/bin/twitter alive
@reboot /usr/bin/python /usr/local/bin/twitter reboot
Tips
#1 To be able to group and sort messages, I would recommend using the power of the hashtag. Pick a unique identifier (maybe your username) and append a category like this:
system startup #oeey_garage_pi
#2 To avoid the "Status is a duplicate" you may also wish to append a unique ID to all messages. I like to use the current linux epoc time, like this:
system startup #oeey_garage_pi (1415571010)
The updated posting code would look like this:
import time
...
status = api.PostUpdate('{message} #{category} ({id})'.format(
message=message,
category=category,
id=int(time.time())
Issues
InsecurePlatformWarning:
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Solution: either ignore the warning or upgrade to at least Python 2.7.9 (or change code to use pyOpenSSL).
Other Libraries
If Python is not your thing, there a number of other
Twitter libraries for various other programming platforms.
That's All Folks
Good luck, and have a good time playing with Python and Twitter.