Pi Lab Twitter Bot

Checkout the new Pi Lab Twitter Bot!

I have setup the bot to post the current uptime and CPU temperature of each node every 30 minutes.

There are tons of articles covering how to create a Twitter Bot so I will not completely recreate the wheel here. I used an article from Dototot. I did notice that Twitter has changed a few things since the article was created, but I was able to get past the differences pretty easily.

Once you have the Twitter Developer Account and app created here are the basic steps to get the bot running.

Step 1:

Make sure everything is up to date.

sudo apt-get update
sudo apt-get upgrade

Step 2:

Install the necessary packages.

Python 2.7:

sudo apt-get install python-pip
sudo pip install tweepy

Python 3:

sudo apt-get install python3-pip
sudo pip install tweepy

Step 3:

Create the Twitter Bot.

mkdir ~/twitterbot
cd ~/twitterbot
sudo nano TwitterBot.py

Step 4:

This is a slightly modified version of my current bot code that you can use.

#!/usr/bin/env python

import tweepy, time, sys, os

CONSUMER_KEY = '***************YOUR DATA*****************'
CONSUMER_SECRET = '***************YOUR DATA*****************'
ACCESS_KEY = '***************YOUR DATA*****************'
ACCESS_SECRET = '***************YOUR DATA*****************'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

#----------------------------------------
# Gives a human-readable uptime string
def uptime():

     try:
         f = open( "/proc/uptime" )
         contents = f.read().split()
         f.close()
     except:
        return "Cannot open uptime file: /proc/uptime"

     total_seconds = float(contents[0])

     # Helper vars:
     MINUTE  = 60
     HOUR    = MINUTE * 60
     DAY     = HOUR * 24

     # Get the days, hours, etc:
     days    = int( total_seconds / DAY )
     hours   = int( ( total_seconds % DAY ) / HOUR )
     minutes = int( ( total_seconds % HOUR ) / MINUTE )
     seconds = int( total_seconds % MINUTE )

     # Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
     string = ""
     if days > 0:
         string += str(days) + " " + (days == 1 and "day" or "days" ) + ", "
     if len(string) > 0 or hours > 0:
         string += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", "
     if len(string) > 0 or minutes > 0:
         string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes" ) + ", "
     string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" )

     return string;

with open('/sys/class/thermal/thermal_zone0/temp', 'r') as myfile:
  data = round(int(myfile.read())/1000, 1)

temp = str(data)

api.update_status(status='Uptime: '+uptime()+ '\nCPU temperature: '+temp+'C')

Step 5:

Ensure that the Twitter Bot is executable.

sudo chmod +x TwitterBot.py

Step 6:

Automate the Twitter Bot.

sudo crontab -e
*/60 * * * * python /home/pi/twitterbot/TwitterBot.py

That's it! You should now have a Twitter Bot setup and posting to Twitter every hour.

I have also setup the Pi Lab Twitter Bot to post when I promote the Pi Lab Dev Node to Production.

Update: I have updated the instructions/setup to work with Python 2.7 and Python 3. These steps should also work for Raspbian as well as Ubuntu Server on a Raspberry Pi.

I have updated the Pi Lab Twitter Bot to a new Node-Red setup. Check it out here.

Posted on