IoT Bytes

Bits and Bytes of IoT

Post a Tweet on Twitter from Raspberry Pi using Python Script

Pradeep Singh | 22nd Feb 2017

twitter

Twitter is one of the most famous social media platforms and a great tool to share information with people across the globe. There are many products in the market that can tweet information directly on your twitter account to keep you posted.

In case you are also working on any project that should tweet information based on some events, this article may help you to implement tweet feature in your project.

1. Onboard Your App on Twitter:

Before writing any app (Python Script in our case) that can tweet on your behalf; you need to onboard it on Twitter. This is an important step to get hold of different API Keys and Tokens from Twitter.

It’s a very simple and straight forward process. Follow these steps to onboard your app on twitter –

1. Go to url “https://apps.twitter.com” and sign in using your Twitter username and password.

2. After logging in, click on “Create New App” button.

3. Fill the details on “Create an application” form –

a. Name: You can use any string for this fields, as long it doesn’t conflict with any other app name on twitter. This field has a 32 character max limit.

b. Description: Provide some details for your app. App description must have at least 10 characters and may go up to 200 characters.

c. Website: If you have a website you can type its URL in this field. Don’t worry if you don’t have a website, access to this URL will not be tested by twitter; so you can use any dummy URL. Make sure the URL has correct format for ex: “https://www.iotbytes.com“.

d. Callback URL: Here we aren’t going to use call back feature so you can leave this field blank.

e. Developer Agreement: Accept the Developer Agreement by clicking on the check box.

After filling all these fields, click on “Create your Twitter application” button to complete the process.

4. On completing “Create an application” process as defined in previous step (# 3), you will be redirected to your newly create app’s settings page. On this page click on “Keys and Access Tokens” tab.

a. Here you will get “Consumer Key (API Key)” and “Consumer Secret (API Secret)“. Make a note of these values as you will need them for python script later in this article.

b. Now scroll down on this page and locate “Your Access Token” section. Here you will find “Token Actions“. Now click on “Create my access token” button to generate access tokens for your app.

c. Make a note of “Access Token” and “Access Token Secret” values. You will need these values later for the python script.

Before proceeding further, make sure you have following four Keys / Tokens from your Twitter Account –

  • Consumer Key (API Key)
  • Consumer Secret (API Secret)
  • Access Token
  • Access Token Secret

2. The Code / App (Python Script):

Before moving on to the Python script, you need to install python package for twitter. Use following pip command to install twitter package on your Raspberry Pi-

pip install twitter

Once twitter package is installed, you can download following Python script from GitHub Repository Octocat.

Before executing this script you need to set CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET variables, based on the values you created in “Step 1: Onboard Your App on Twitter” of this article.

from twitter import *

# Set the values for following variables from your Twitter Account
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

# This function will tweet on Twitter
def publish_Status_On_Twitter(Twitter_Status):
 TW = Twitter(auth=OAuth(ACCESS_TOKEN,ACCESS_TOKEN_SECRET,CONSUMER_KEY,CONSUMER_SECRET))
 TW.statuses.update(status=Twitter_Status)
 
# Tweet 
publish_Status_On_Twitter("My first tweet from Python!")

On the successful execution of this script, you should see something like this on your twitter account –

tweet

3. Delete your App from Twitter (Optional Step):

Just in case you want to delete the twitter application, to prevent your software from tweeting on your behalf; follow these steps –

1. Go to URL “https://apps.twitter.com” and sign in using your Twitter username and password.

2. After successful login, click on the App that you want to delete.

3. On the “Details” tab, scroll down and locate “Delete Application” button to delete the application.

Conclusion:

With this script, you can tweet notifications/updates from your IoT app directly onto your twitter account. This python script will work on any system that can run Python 2.7 including Raspberry Pi. In case you have any questions, please feel free to post them here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: