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.
pipenv install python-dotenv tweepy
First, go to https://apps.twitter.com/ to create and app which we will use to access the Twitter API.
Fill out the bot name, description, and a website (it can be anything if you don’t have a website yet.)
Tick the Developer Agreement check-box and create your twitter application.
Now, go to the Permissions tab and make sure your app has Read and Write permissions.
This is the most important part, go to Keys and Access Tokens and copy your Consumer Key and your Consumer Secret
Scroll down until you see the section Your Access Tokens and click on Create my access token
Copy those tokens and save them in a safe place, remember our
.env entry? don’t worry, we’ll use it them here anyway.
Store your keys in a secure
.env text file so we can load them as environment variables later
consumer_key = JtK10FrJWuAkjtpDSO4lZrhHu
Now let’s use our snippet to load environment variables keys into our code without compromising our sensitive data
from os import getenv
Access our Twitter app is easy importing
tweepy, we also want to return an API object to handle requests
# import tweepy first
Once we have access our application we’re ready to send tweets through our twitter app with
Let’s check our twitter account to see if it works
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
Example snippets extracted from the official documentation
for follower in tweepy.Cursor(api.followers).items():
# Iterate through all of the authenticated user's friends
Handling Rate Limit
# In this example, the handler is time.sleep(15 * 60),