Tweeting with Python
Tags: python, twitter, beginners, tutorial
Published: 2020-08-29 15:59:25

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](https://www.python.org/downloads/)
2. [Twitter developer account](https://developer.twitter.com/)

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](https://www.python.org/downloads/).

### Setting up a twitter developer account
First, you need to apply for a [twitter developer account](https://developer.twitter.com/). 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](http://docs.tweepy.org/en/latest/) library to communicate with the Twitter API

* ***Create your python project*** I recommend creating a project in a separate [virtual environment](https://dev.to/towernter/setting-up-a-python-virtual-environment-on-windows-1l4p).

* ***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.
```python
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. 
```python
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.
```python
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)
```python
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
```python
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
```python
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
```python
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](https://twitter.com/towernter)