Tweepy is a Python library that enables Python users to develop Twitter applications. Whatever you do manually on Twitter can be automated using Tweepy to make your social-media experience easier.
The Twitter API gives developers access to most of Twitter’s functionality, including:
Let’s make a simple Twitter bot that takes your tweet as an input and then posts it on your timeline.
Before anything else, make sure you have Tweepy installed on your system. To install Tweepy, run the following command:
pip install tweepy
Go to Twitter for Developers and apply for a developer account. You will have to provide some personal details and reasons why you want to make a developer account. After that, you will have to wait a few days until Twitter reviews and approves your application.
Once you have your developer account, go to the Create App tab and fill out the details. After you have filled out and submitted all the details, you will have access to a couple of Keys and Tokens, which are necessary to connect your bot to Twitter.
Here is a simple code to tweet out whatever you want using Tweepy:
import tweepy### Authorization protocolauth = tweepy.OAuthHandler("API KEY", "API SECRET KEY")auth.set_access_token("ACCESS TOKEN", "ACCESS TOKEN SECRET")### Providing access to APIAPI = tweepy.API(auth)### Taking tweet as an inputtweet = input("Dump your thoughts here: ")### Tweeting to the linked twitter accountAPI.update_status(status = (tweet))
The Twitter API uses OAuth, a widely used open authorization protocol, to authenticate all the requests. Before making any call to the Twitter API, you need to create and configure your authentication credentials. To do that, you need to pass the Keys and Tokens mentioned in Step 2.
Then, you need to call Tweepy’s API class and pass the authorization(auth
) as an argument to gain access to Twitter endpoints. This will allow you to implement Twitter’s functionalities.
Lastly, pass the string to the update_status()
method, and tweet out your thoughts!
Free Resources