How to create a Twitter Bot using Python and Zapier
Tags: 100daysofcode, bots, python, automation
Published: 2021-04-11 22:05:46

<!-- wp:heading {"level":3} -->
<h3>Getting Started with Python and Zapier to make&nbsp;Bots</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>We will use Python and Zapier to make a Twitter bot that tweets a joke&nbsp;hourly.</h4>
<!-- /wp:heading -->

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

<!-- wp:paragraph -->
<p><a href="https://zapier.com/" rel="noreferrer noopener" target="_blank">Zapier</a> is a tool that lets you automate tasks and create workflows by integrating various apps. Some things you could do using Zapier</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Make a price tracker which notifies you when the price is less than X $</li><li>Get an alert in Slack/Discord when you get an email from a specific email id</li><li>Tweet an update whenever some RSS Feed is updated</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Before proceeding, let’s discuss some of the limitations with Zapier’s support for Python</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>With the free version, we can only run short scripts which take &lt;1s to execute</li><li>We can not install external modules. Only the following external modules are available</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>&nbsp;<code>requests</code>: an easy-to-use <a href="https://github.com/kennethreitz/requests/blob/v2.8.1/docs/user/quickstart.rst#make-a-request" rel="noreferrer noopener" target="_blank"><strong>HTTP client</strong></a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>StoreClient</code>&nbsp;: a built-in utility for <a href="https://zapier.com/help/create/code-webhooks/store-data-from-code-steps-with-storeclient" rel="noreferrer noopener" target="_blank"><strong>storing and retrieving data between Zap runs</strong></a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>What will be building?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We will build a Twitter bot. The Twitter bot is going to fetch a random joke using <a href="https://github.com/15Dkatz/official_joke_api" rel="noreferrer noopener" target="_blank">the official joke API</a>. We will also add a feature that lets a user specify the list of hashtags to include in each tweet.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The bot will tweet the joke along with the hashtags hourly.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Steps to build the&nbsp;bot</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Step1: Python script to make a request to the&nbsp;API</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We will use the requests library and make a GET request to the jokes API. Below is the code snippet to make the request</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*pUwNzt-SD-MnKEVBaj2hzw.png" alt="Code to make the request"/><figcaption>Code to make the&nbsp;request</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*dfZ1YSHmnBi9tCpYdoEEdw.png" alt="Code snippet to format Tweet"/><figcaption>Formatting Tweet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><li>We will create an empty list to store the question, answer, hashtags, and some dot characters.</li><li>The join() method will be used to create the final tweet</li><li>Before using the hashtags, we need to add the hash character and create a string with all the hashtags</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Below is a sample tweet</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*skis_vQw68L-1Z-oDSXYcw.png" alt="A Sample Tweet"/><figcaption>Sample Tweet</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now that we are done with the tweet, we can move on to the next step.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Step2: Setup Zapier Account and Create a&nbsp;Zap</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Apps created in Zapier are known as Zaps</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Go to <a href="https://zapier.com/" rel="noreferrer noopener" target="_blank">https://zapier.com/</a> and Sign up for free</li><li>After you have signed up, log in to your account</li><li>Click on ‘Make A Zap’ in the left side-bar</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Step3: Setup a&nbsp;trigger</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Every Zap requires a trigger. A trigger is basically an event that will run the Zap. Some possible triggers are</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Receiving an email</li><li>Receiving a PR on Github</li><li>RSS feed being updates</li><li>Receiving a message on Slack</li><li>Receiving an order in WooCommerce</li><li>A Schedule, i.e hourly, daily, weekly, monthly</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Since we want to tweet daily, we will select the schedule trigger and select daily. It is the first item on the right column</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*ioi_B63b6rWcUjcPQu5LhQ.png" alt="Screenshot of Triggers"/><figcaption>Screenshot of&nbsp;Triggers</figcaption></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><li>For the ‘Trigger Event’ option, chose ‘Every Hour’ and click on ‘Continue’</li><li>Next, you can select if you wish to run the trigger on weekends</li><li>After you click continue, your trigger should be created</li><li>You can click on Test Trigger to run the trigger.</li></ul>
<!-- /wp:list -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*V-Tqsdos-zIevNrrEr1U3A.png" alt="Success Message after running trigger"/><figcaption>Trigger Success</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You should see the above message if your trigger ran successfully.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Step4: Adding the Python&nbsp;Script&nbsp;</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Click on the ‘+’ button or the “Add Action” Button</li></ul>
<!-- /wp:list -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*QqFHvg2HEZhEJkqwOwONYg.png" alt="Screenshot of Action"/><figcaption>Screenshot of&nbsp;Action</figcaption></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><li>Search for “code” and Select “Code by Zapier”. It is the first item in the search result in the screenshot</li><li>For the option “Action Event”, chose “Run Python”</li></ul>
<!-- /wp:list -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*Gz12cMD5TOFl4-nc5uHDeg.png" alt="Screenshot of Code by Zapier"/><figcaption>Screenshot of Code by&nbsp;Zapier</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To convert it into a list, we can use the eval() function. The same works for dictionary values.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Click on Continue and then click on ‘Test and Review’. This will run the code.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Now we can copy the python script we previously created with some modifications</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*QY629GOJj3GoN2vDUum8NQ.png" alt=""/><figcaption>The entire Python&nbsp;Script</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The output or the data to be returned must be stored in a dictionary named “output”</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*iyTcvJZZ99KiDVZrMzxShg.png" alt=""/><figcaption>A successful run of the&nbsp;script</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Step5: Add the Twitter&nbsp;App</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Click on the ‘+’ button and add a new action.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*SKN2YaHHjzAaUnEi07BVNg.png" alt=""/><figcaption>Select the Twitter&nbsp;App</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Search for ‘Twitter’ and select the Twitter APP</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For the ‘Action Event’, select ‘Create Tweet’</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*8thAXcq9ie71oF4Uze8J9w.png" alt=""/><figcaption>Twitter APP</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You will have to connect your Twitter account and authorize Zapier.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After you authorize Zapier, you should see the following Screen</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*C5qGG_eyoADdaK_7RU9j-g.png" alt=""/><figcaption>Screenshot of Twitter&nbsp;App</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Select ‘Run Python’ and then select ‘Tweet’. This is the tweet variable we created in our Python Script.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once you are done, click on continue and test the action.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*GbZBpCJMD3M52kKbXej9Ow.png" alt=""/><figcaption>A successful run of the&nbsp;App</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>If your test was successful, your Twitter account should have tweeted a joke 😄&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, you can click on “Turn on Zap” and your Zap should be running.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Conclusion</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Congratulations! Your Zap is now running and your Twitter account will tweet a joke hourly&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Some other things you could do&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Instead of using an API, use some RSS Feed</li><li>Setup a scraper and scrape jokes from some website</li><li>Add a check to ensure the tweet doesn’t cross Twitter’s tweet word limit</li></ul>
<!-- /wp:list -->