How to Send a Tweet With Python Using Tweepy

Overview

Here are some simple steps to send a tweet with a Python script to give you an overview on how to authenticate in Twitter using OAuth and simple code snippets to manipulate Twitter data to build a bot.

Requirements

  • python-dotenv
  • tweepy

Installation (pipenv)

pipenv install python-dotenv tweepy

Create an app

First, go to https://apps.twitter.com/ to create and app which we will use to access the Twitter API.

Create app

Fill out the bot name, description, and a website (it can be anything if you don’t have a website yet.)

Fill out forms

Tick the Developer Agreement check-box and create your twitter application.

App created

Now, go to the Permissions tab and make sure your app has Read and Write permissions.

Permissions

This is the most important part, go to Keys and Access Tokens and copy your Consumer Key and your Consumer Secret

Consumer Keys

Scroll down until you see the section Your Access Tokens and click on Create my access token

Access Tokens

Copy those tokens and save them in a safe place, remember our .env entry? don’t worry, we’ll use it them here anyway.

Securing your keys

Store your keys in a secure .env text file so we can load them as environment variables later

consumer_key = JtK10FrJWuAkjtpDSO4lZrhHu
consumer_secret = j32pVGFXGUYqcufMH87Npe7bG2vQqL3DYmKHKd8OD9DMfYe6kY
access_token = 968177792255971328-Oib8WU1WwjvMX6GqcVkxA3cVcbCVs88
access_secret = zcwiPwdOQcJBakxHaN2AVGZrxGrtYIA6DnpXcM0mDo3CG

Now let’s use our snippet to load environment variables keys into our code without compromising our sensitive data

from os import getenv
from dotenv import load_dotenv, find_dotenv

# load env keys
load_dotenv(find_dotenv())

consumer_key = getenv('consumer_key')
consumer_secret = getenv('consumer_secret')
access_token = getenv('access_token')
access_secret = getenv('access_secret')

Twitter authentication

Access our Twitter app is easy importing tweepy, we also want to return an API object to handle requests

# import tweepy first
import tweepy

# authentication with OAuth using our keys
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

# return an API using our previous access
api = tweepy.API(auth)

Sending a test tweet

Once we have access our application we’re ready to send tweets through our twitter app with api.update_status(message)

import tweepy
from os import getenv
from dotenv import load_dotenv, find_dotenv

# load env keys
load_dotenv(find_dotenv())

consumer_key = getenv('consumer_key')
consumer_secret = getenv('consumer_secret')
access_token = getenv('access_token')
access_secret = getenv('access_secret')

# OAuth authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

# Send a tweet
api.update_status('Congrats! You did it!')
print('Tweet sent.')

Let’s check our twitter account to see if it works

Yup!

Yup! It worked! but of course this is only the beginning, there’s a lot more you can do with tweepy, check out the API documentation bellow 👇 and some snippets that you can use in your code

Snippets

Example snippets extracted from the official documentation

Follow all

for follower in tweepy.Cursor(api.followers).items():
follower.follow()

Pagination

# Iterate through all of the authenticated user's friends
for friend in tweepy.Cursor(api.friends).items():
# Process the friend here
process_friend(friend)

# Iterate through the first 200 statuses in the friends timeline
for status in tweepy.Cursor(api.friends_timeline).items(200):
# Process the status here
process_status(status)

Handling Rate Limit

# In this example, the handler is time.sleep(15 * 60),
# but you can of course handle it in any way you want.

def limit_handled(cursor):
while True:
try:
yield cursor.next()
except tweepy.RateLimitError:
time.sleep(15 * 60)

for follower in limit_handled(tweepy.Cursor(api.followers).items()):
if follower.friends_count < 300:
print follower.screen_name

Check out the API Reference