Python Tutorial 49 – Twitter Post Automation Using Tweepy and Twitter API

There are three main components to automate the Twitter post-blast, which are the Python script, media library, and schedule module. In this Python Tutorial, I would walk you through how to code the Twitter post automation script that connects with the Google Sheets and add schedule modules.

There are three main components to automate the Twitter post-blast, which are the Python script, media library, and schedule module. In this Python Tutorial, I would walk you through how to code the Twitter post automation script that connects with the Google Sheets and add schedule modules.

Python modules: tweepy, gspread, schedule, Tweet API access token

Twitter App Authentication

Compared to my previous article regarding Twitter bot scraping influencers and grabbing sentiment data, Twitter posts need one more step to authenticate your app. This is the user authentication setting. For more details regarding Twitter developer accounts, please refer to this article.

twitter post automation

There are three key steps we need to update in the authentication process. First thing first, we select the app permission in a read and write mode, instead of only read mode. This is essentially different from the Twitter bot.

Then, we need to input the redirect URL and website URl. You can refer to my inputs or set up a localhost URI based on the needs.

Manage to-be-posted content upfront on Google Sheet

In one Google sheet tab, we are able to prepare all elements a Tweet needs, such as post body, photos, landing URLs. And if you have other channels blasting the same piece of post, you also can centralize to manage there 

For more details regarding how to install and import gspread, please check out previous Python Tutorials. Basically, we need to import gspread, create a Google credential account in the Google APIs Console, and get access to the Google Sheet you manage content.

In terms of the scope of Google API, the automation needs google drive, and google sheet read and write.

Import gspread

from googleapiclient.discovery import build

from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive']

creds = ServiceAccountCredentials.from_json_keyfile_name('yourGooglecredential.json',scope)

client = gspread.authorize(creds)

sh = client.open('sheetname')

worksheet = sh.get_worksheet(cell position)

Update_status & update_status_with_media

In the Tweepy post update, there are two methods we can use. One is the update_status which is posting a tweet where it includes text, links. The other is, which is attaching a photo separately instead of using the detected images from the URL link. Below is a screencap from my Twitter posts. I personally prefer to use update_status, because users can click through the photo to the website.

twitter post automation

Schedule Module

Python schedule module helps developers to automate executing the tasks as long as the device is in running status, like a laptop. So if you are a social marketer on day, you can use this module to help you blast the content automatically. The only cons of this approach are your device must be open. 

# After every 10mins easy2digital() is called. 

schedule.every(10).minutes.do(easy2digital)

# After every hour easy2digital() is called.

schedule.every().hour.do(geeks)

# Every day at 12am or 00:00 time easy2digital() is called.

schedule.every().day.at("00:00").do(easy2digital)

# After every 5 to 10mins in between run easy2digital()

schedule.every(5).to(10).minutes.do(easy2digital)

# Every monday easy2digital() is called

schedule.every().monday.do(easy2digital)

# Every tuesday at 18:00 easy2digital() is called

schedule.every().tuesday.at("18:00").do(easy2digital)  

# Loop so that the scheduling task

# keeps on running all the time.

while True:

    # Checks whether a scheduled task 

    # is pending to run or not

    schedule.run_pending()

    time.sleep(1)

Full Python Script of Twitter Post Automation

If you are interested in the full Python script of Python Tutorial 49 – Twitter Post Automation Using Tweepy and Twitter API, please subscribe to our newsletter by adding the message “Python Tutorial 49”. We would send you the script immediately to your mailbox.

Contact us

I hope you enjoy reading Python Tutorial 49 – Twitter Post Automation Using Tweepy and Twitter API. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

  • Support my channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • Share the article to your social network with the hashtag #easy2digital
  • Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
  • You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)