← Back to list

Create Automatic Twitter Posts with Python and Tweepy

Create Automatic Twitter Posts with Python and Tweepy

Panda · 2024-06-03 18:15 · 1 claps · 2.2 min read
#python #tweepy #twitterbots #python-twitter-bot #coding
Open on Medium ↗
Wiki topics: 💻 · Programming

Create Automatic Twitter Posts with Python and Tweepy

GET STARTED

In this article, you will learn how you can automate your Twitter / X posts with python and tweepy. All you need is a Twitter Developer account and the latest python version.

To start, we first need to install tweepy.

pip install tweepy

Tweepy is a python library for interacting with the official Twitter API.

Create a config.py and insert the lines below.

CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
BAERER_TOKEN = ""

TWITTER DEVELOPER PORTAL

Go to https://developer.twitter.com and create a developer account. Inside the dashboard, go to apps and create a new app or select the app you want to use. Click on your app on the left side and click on edit at the User authentication settings

Inside the settings, select Read and write under App permissions.

Select Web App under Type of App.

Add a website into the App info and click on Save.

Click on the tab Keys and token at the top and generate a Key for all options.

Save these keys into your config.py

SEND TWEET VIA CODE

To send tweets with code, create a file main.py

Inside the main.py we need to import the config and tweepy.

import config
import tweepy

Now we need to connect to our Twitter app we’ve created earlier.

# Authenticate to Twitter
auth = tweepy.OAuthHandler(config.CONSUMER_KEY, config.CONSUMER_SECRET)
auth.set_access_token(config.ACCESS_TOKEN, config.ACCESS_TOKEN_SECRET)

# Create an API object
api = tweepy.API(auth)

# Create an Client object
client = tweepy.Client(
  config.BAERER_TOKEN,
  config.CONSUMER_KEY,
  config.CONSUMER_SECRET,
  config.ACCESS_TOKEN,
  config.ACCESS_TOKEN_SECRET,
  wait_on_rate_limit=True
)

Now we need to log in into our account.

try:
    api.verify_credentials()
    print("Authentication OK")
except Exception as e:
    print(e)

To create a new tweet we use the create_tweet function from our client object. Insert these line into the try-except.

client.create_tweet(text=postText)

Now our code should look like this:

import config
import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler(config.CONSUMER_KEY, config.CONSUMER_SECRET)
auth.set_access_token(config.ACCESS_TOKEN, config.ACCESS_TOKEN_SECRET)

# Create an API object
api = tweepy.API(auth)

# Create an Client object
client = tweepy.Client(
  config.BAERER_TOKEN,
  config.CONSUMER_KEY,
  config.CONSUMER_SECRET,
  config.ACCESS_TOKEN,
  config.ACCESS_TOKEN_SECRET,
  wait_on_rate_limit=True
)

try:
    api.verify_credentials()
    print("Authentication OK")
    client.create_tweet(text=postText)
except Exception as e:
    print(e)

Thanks for reading❤️

My social Media:

Twitter: https://twitter.com/byPandaDev

Instagram: https://www.instagram.com/bypandadev/

YouTube: https://www.youtube.com/@byPandaDev


메타데이터
post_id
9092972ba034
slug
create-automatic-twitter-posts-with-python-and-tweepy-9092972ba034
url
https://medium.com/@bypanda/create-automatic-twitter-posts-with-python-and-tweepy-9092972ba034
canonical_url
https://medium.com/@bypanda/create-automatic-twitter-posts-with-python-and-tweepy-9092972ba034
author_url
https://medium.com/@bypanda
status
ok
fetched_at
2026-07-23 17:24:54