DEV Community

Cover image for Tweeting with Python
Tawanda Nyahuye
Tawanda Nyahuye

Posted on

Tweeting with Python

Besides the usual apps, twitter has an API that allows you to send tweets or manipulate twitter data with it. In this article, we are going to send tweets using the Twitter API and Python. Assuming you already have a twitter account, here are other things you need:

  1. Python
  2. Twitter developer account

Setting up python on your computer is straight forward, all you need is the download the latest version suitable for the operating system from here.

Setting up a twitter developer account

First, you need to apply for a twitter developer account. Fill the forms accordingly and put in mind that the reasons you give for why you need the developer account affects the approval time of your developer account. The approval of your developer account may take a day or more.

Creating an app

Once your developer account is ready, go to your twitter developer account and do the following:

  1. Go developer portal then apps and create a new app. Twitter will ask some questions to describe what your app will be used for, website and others(answer accordingly and for the website, all you need is just a valid URL)
  2. Generate API keys under keys and tokens and save them well labeled in a separate file on your computer.
  3. Give your app read and write access rights under permissions

Writing the code

In this tutorial, we will be using Python's tweepy library to communicate with the Twitter API

  • Create your python project I recommend creating a project in a separate virtual environment.

  • Install tweepy pip install tweepy or pip install git+https://github.com/tweepy/tweepy.git@2efe385fc69385b57733f747ee62e6be12a1338b (recommended)

In your Python script: Import the tweepy library that we have installed earlier.

import tweepy

Initialize the API keys that we generated earlier (you should have them ready saved in a separate file) if not you can still generate them from your developer account.

consumer_key = "your consumer key"
consumer_secret = "your consumer secret key"
access_token = "your access token"
access_token_secret = "your access token secret"

Create an authentication to your twitter account. Your app authenticates with your twitter account using your API keys.

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
API = tweepy.API(auth)

Add a function to tweet some text (your text should be <128 characters)

def tweet_some_text(tweet):
    API.update_status(tweet)

If you want to tweet text with a media file like an image use the following function

def tweet_with_media(filename, tweet):
    API.update_with_media(file_name, status=tweet)

Add a main method to your script (where we are going to call our tweeting functions) replace my_meme.jpg with your image and location if not in the same folder with your script

if __name__ == "__main__":
    tweet_some_text("Tweeting from backend, isn't that cool?")
    tweet_with_media("my_meme.jpg", "This is a meme")

Full code

import tweepy

consumer_key = "your consumer key"
consumer_secret = "your consumer secret key"
access_token = "your access token"
access_token_secret = "your access token secret"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
API = tweepy.API(auth)

def tweet_some_text(tweet):
    API.update_status(tweet)

def tweet_with_media(filename, tweet):
    API.update_with_media(file_name, status=tweet)

if __name__ == "__main__":
    tweet_some_text("Tweeting from backend, isn't that cool?")
    # tweet_with_media("my_meme.jpg", "This is a meme")

If anything goes wrong I will be here

Discussion (7)

Collapse
bogdaaamn profile image
Bogdan Covrig

Great article, thank you!!

Looking forward to libraries implementing the new version of Twitter API 👀👀 Looks like tweepy is still v1.

Collapse
titanhero profile image
Lex • Edited on

Do you know rainbowstream? Is a very cool Twitter client in python for your Terminal, it will gonna like you a lot if you test it 😉😁👍✌️

Collapse
towernter profile image
Tawanda Nyahuye Author

Thanks, I'll check it out

Collapse
jessicagarson profile image
Jessica Garson

This is so cool! Thanks for this.

Collapse
towernter profile image
Tawanda Nyahuye Author

You're welcome

Collapse
jokke_moose profile image
Moose-man

Hi,
Very interested to see the same example with the bearer token!
Any idea where I could get some tutorial on this?
Johan.