Raspberry Pi based Twitter Bot using Python

Raspberry Pi Twitter Bot using Python

Twitter is a popular online news and social media platform, where people interact by doing ‘tweets’. Not only interaction, but one can do promotions using Twitter. And, for the promotion, we need to schedule a tweet for every minute, hour, day, or accordingly. This tweeting process can be done automatically, by using a Twitter bot.

Here, in this tutorial, we are going to demonstrate to you that how you can make a Raspberry pi based Twitter bot. Basically, Twitter bot is a software that controls a Twitter account via the Twitter API. The bot software can autonomously perform actions such as tweeting, re-tweeting, liking, following, unfollowing, or direct messaging to other accounts. We are going to use python to setup a Raspberry Pi Twitter Bot.

We have also created a Raspberry Pi Telegram Bot, if you are interested, you can check that out.


Components Required

  • Raspberry Pi
  • Power Supply


Creating a Twitter app

For making a Raspberry Pi based Twitter bot, we need Twitter API. To get a Twitter API, we need to create a new Twitter app.

For creating a new Twitter app, sign in to your Twitter account and go to this URL https://apps.twitter.com/app/new.


Fill all the require details and leave the Callback URL option blank, as we don’t need any callbacks. Once you are done, click on ‘Create’ to create your app.

Creating Twitter App for Raspberry Pi Twitter Bot


In the next screen, you will see App details, keys and tokens, and permissions.

Twitter App details for Raspberry Pi Twitter Bot


Go to the Keys and Tokens section and create your Access token and access token secret.

Twitter API for Raspberry Pi Twitter Bot


Now go to the permission section and review your permission settings. It should look like this:

Raspberry Pi Twitter Bot Permissions


Raspberry Pi Setup for Twitter Bot

Install pip, if you don’t have it in your Pi, using below command:

sudo easy_install pip


Now install twython to interact with Twitter’s API.

sudo pip install twython


Make a directory where we will keep our code and go to the directory using:

mkdir Raspibot
cd Raspibot


Now create a new file using below command:

sudo nano bot.py


Now in this file, copy and paste the below code:

#!/usr/bin/env python
import sys
from twython import Twython

CONSUMER_KEY = ' Enter Consumer API Key'
CONSUMER_SECRET = ' Enter Consumer API Key Secret Key'
ACCESS_KEY = ' Enter Access token'
ACCESS_SECRET = ' Enter Access token secret'

#Create a copy of the Twython object with all our keys and secrets to allow easy commands.


And, save the file by pressing Ctrl + X then Y and then Enter. Now we need to make the script executable, so that we can send our text.

sudo chmod +x bot.py


Now let’s test the Twitter Bot script by sending first tweet using pi.

sudo python bot.py " raspi's first tweet "


After this, check your Twitter account to see the new tweet.


Tweeting the Raspberry Pi’s CPU Temperature

In the next step, we will tweet our Raspberry Pi’s CPU temperature and also automate this process.

For this we need to make some changes in our previous code.

#!/usr/bin/env python
import os
from twython import Twython

#Define our constant variables, this is all the data we wrote down in the first part of the tutorial.
CONSUMER_KEY = 'Enter Consumer API Key'
CONSUMER_SECRET = 'Enter Consumer API Key Secret Key'
ACCESS_KEY = 'Enter Access token'
ACCESS_SECRET ='Enter Access token secret'

#Create a copy of the Twython object with all our keys and secrets to allow easy commands.

#Using our newly created object, utilize the update_status to send in the text passed in through CMD
cmd = '/opt/vc/bin/vcgencmd measure_temp'
line = os.popen(cmd).readline().strip()
temp = line.split('=')[1].split("'")[0]
api.update_status(status='My current CPU temperature is '+temp+' C')


Now run the above code using:

Python bot.py


After this, check your Twitter account to see the new tweet.

Tweeting on Twitter Account using Raspberry Pi Twitter bot


Automating Twitter Bot using Pi

One of the best way to make your Twitter Bot automatic is to use Cron Jobs. Edit the crontab by running the following command:

sudo crontab –e


Add the below text line to the bottom of the file. Then this file will repeat the all process again and again after every one minute (as set in code). You can change the time by changing the value in the text.

*/60 * * * * python /home/pi/Raspibot/bot.py

Automating Raspberry Pi Twitter Bot


Now press Ctrl + X, then Y, and then Enter to save changes.

After this, check your twitter account, and it will tweet the pi’s temperature in every one minute.

Hence, we have successfully created a twitter bot, which will tweet the raspberry pi’s temperature on twitter in every one minute.


Also, check our previously made stunning projects using Raspberry Pi:

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.