How to create a Twitter Bot using Python and Zapier

How to create a Twitter Bot using Python and Zapier

Getting Started with Python and Zapier to make Bots

We will use Python and Zapier to make a Twitter bot that tweets a joke hourly.

Here is the Twitter bot we will be building twitter.com/jokebot13

Zapier is a tool that lets you automate tasks and create workflows by integrating various apps. Some things you could do using Zapier

  • Make a price tracker which notifies you when the price is less than X $
  • Get an alert in Slack/Discord when you get an email from a specific email id
  • Tweet an update whenever some RSS Feed is updated

and many more things. For most of them, you won’t need to code in Python or JavaScript. The in-built apps provided by Zapier are more than sufficient. However, if you wish to, you can execute small Python scripts as well. We will be working with Python and Zapier in this tutorial.

Before proceeding, let’s discuss some of the limitations with Zapier’s support for Python

  • With the free version, we can only run short scripts which take <1s to execute
  • We can not install external modules. Only the following external modules are available

 requests: an easy-to-use HTTP client.

StoreClient : a built-in utility for storing and retrieving data between Zap runs.

What will be building?

We will build a Twitter bot. The Twitter bot is going to fetch a random joke using the official joke API. We will also add a feature that lets a user specify the list of hashtags to include in each tweet.

The bot will tweet the joke along with the hashtags hourly.

Steps to build the bot

Step1: Python script to make a request to the API

We will use the requests library and make a GET request to the jokes API. Below is the code snippet to make the request

Code to make the requestCode to make the request

The jokes are in the form of a question and answer so we will need to format the text before tweeting it. We will also need to add the hashtags at the end.

Code snippet to format TweetFormatting Tweet

  • We will create an empty list to store the question, answer, hashtags, and some dot characters.
  • The join() method will be used to create the final tweet
  • Before using the hashtags, we need to add the hash character and create a string with all the hashtags

Below is a sample tweet

A Sample TweetSample Tweet

Now that we are done with the tweet, we can move on to the next step. 

Step2: Setup Zapier Account and Create a Zap

Apps created in Zapier are known as Zaps

  • Go to https://zapier.com/ and Sign up for free
  • After you have signed up, log in to your account
  • Click on ‘Make A Zap’ in the left side-bar

Step3: Setup a trigger

Every Zap requires a trigger. A trigger is basically an event that will run the Zap. Some possible triggers are

  • Receiving an email
  • Receiving a PR on Github
  • RSS feed being updates
  • Receiving a message on Slack
  • Receiving an order in WooCommerce
  • A Schedule, i.e hourly, daily, weekly, monthly

Since we want to tweet daily, we will select the schedule trigger and select daily. It is the first item on the right column

Screenshot of TriggersScreenshot of Triggers

  • For the ‘Trigger Event’ option, chose ‘Every Hour’ and click on ‘Continue’
  • Next, you can select if you wish to run the trigger on weekends
  • After you click continue, your trigger should be created
  • You can click on Test Trigger to run the trigger.

Success Message after running triggerTrigger Success

You should see the above message if your trigger ran successfully. 

Step4: Adding the Python Script 

  • Click on the ‘+’ button or the “Add Action” Button

Screenshot of ActionScreenshot of Action

  • Search for “code” and Select “Code by Zapier”. It is the first item in the search result in the screenshot
  • For the option “Action Event”, chose “Run Python”

Screenshot of Code by ZapierScreenshot of Code by Zapier

To enter inputs, click on the ‘+’ button in the ‘Input Data’ Section. To access the inputs, we just need to access the dictionary ‘input_data’. All the values are stored as strings, i.e the value for the key hashtags is actually stored as a string and not a list.

To convert it into a list, we can use the eval() function. The same works for dictionary values.

  • Click on Continue and then click on ‘Test and Review’. This will run the code.

Now we can copy the python script we previously created with some modifications

The entire Python Script

The output or the data to be returned must be stored in a dictionary named “output”

Remember to click on “Test and Review” or “Retest Action” each time you want to run your code. Below is the result of executing the script

A successful run of the script

As you can see the tweet is being displayed. This output dictionary will be used in the next action which is the “Twitter App” to tweet the joke

Step5: Add the Twitter App

Click on the ‘+’ button and add a new action.

Select the Twitter App

Search for ‘Twitter’ and select the Twitter APP

For the ‘Action Event’, select ‘Create Tweet’

Twitter APP

You will have to connect your Twitter account and authorize Zapier.

After you authorize Zapier, you should see the following Screen

Screenshot of Twitter App

Select ‘Run Python’ and then select ‘Tweet’. This is the tweet variable we created in our Python Script.

Once you are done, click on continue and test the action.

A successful run of the App

If your test was successful, your Twitter account should have tweeted a joke 😄 

Now, you can click on “Turn on Zap” and your Zap should be running.

Conclusion

Congratulations! Your Zap is now running and your Twitter account will tweet a joke hourly 

Some other things you could do 

  • Instead of using an API, use some RSS Feed
  • Setup a scraper and scrape jokes from some website
  • Add a check to ensure the tweet doesn’t cross Twitter’s tweet word limit