Making a bot that tweets a random image

Share images from a Google Drive folder using Python and PipeDream.

📅 2 months ago

Before you start making bots, consider reading these essays and articles. Also worth browsing: resources for cleaning up your bot's language.

In this tutorial, I will walk you through creating a bot that shares random images from a folder and attaches a description to them.

Familiarity with Python will be helpful, but not entirely necessary, as the syntax is pretty beginner-friendly.

We will be using PipeDream to run our code, and Google Drive to host our images, both of which require a free account.

First, go to your Google Drive and create a folder for your project. In this folder, create another folder for all your images, and a blank spreadsheet.

Screenshot from Google Drive showing our folder structure.

Open your spreadsheet and add the following columns:

  • image_url
  • text
  • alt_text

Next, upload your images to your image folder. Then, one by one, get a link for each of them via the right-click menu. Make sure that the Anyone with the link option is selected under General access.

Screenshot of the Google Drive interface showing how to get a URL of a an image.
Screenshot of the Google Drive interface showing how to share an image publicly and retrieve its URL.

Put each URL in the image_url column. You can describe the content of each image in the alt_text column, and also fill out the text of the tweet that will share this image in the text column.

Screenshot of a spreadsheet that holds the data for our bot.

Now that we have our images, let’s look at some code that will randomly pick one and tweet it out.

First, create a new account for your bot and a developer account to go with it. At the end of this process you will have a few API keys that we will need to store in PipeDream settings.

Once your API keys are saved, head over to your workflow dashboard and create a new workflow.

Screenshot of the Pipedream workflows dashboard with the New button highlighted.

Choose Schedule as a trigger for the bot. For now let’s go with the Custom Interval option to test things out.

Screenshot of  a new workflow trigger being added in Pipedream.

Let’s use a short trigger interval, something like 30 seconds. Once we’re done testing, we can go back and change it to something more suitable.

Screenshot of  a new workflow trigger being customized in Pipedream.

Next, we’ll add a Python code block that will run every time our scheduler is triggered.

Screenshot of  a new workflow step being added in Pipedream.
Screenshot of  a new workflow step being added in Pipedream with Python code block highlighted.
Screenshot of  a new workflow step being added in Pipedream with Python code block highlighted.
Screenshot of  a new workflow step being added in Pipedream with Python code block being edited.

Replace the content of the script block with the following code.

import io
import os
import random
import requests
import urllib.request
import pandas as pd
import tweepy

data_url = "URL OF YOUR SPREADSHEET"
filename = "/tmp/image.png"

def handler(pdm: "pipedream"):
    auth = tweepy.OAuth1UserHandler(
        os.environ.get("TWITTER_API_KEY"),
        os.environ.get("TWITTER_API_SECRET"),
        os.environ.get("TWITTER_ACCESS_TOKEN"),
        os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")
    )

    csv=requests.get(data_url).content
    df=pd.read_csv(io.StringIO(csv.decode('utf-8')))
    data = df.to_dict(orient='records')

    data = [item for item in data
        if ("image_url" in item and type(item["image_url"]) == str and item["image_url"])
        and ("alt_text" in item and type(item["alt_text"]) == str and item["alt_text"])
        and ("text" in item and type(item["text"]) == str and item["text"])
    ]

    api = tweepy.API(auth)
    item = random.choice(data)
    image_id = item["image_url"].split("/")[5]
    image_url = f"http://drive.google.com/uc?export=view&id={image_id}"

    urllib.request.urlretrieve(image_url, filename)

    response = api.media_upload(filename=filename)
    api.create_media_metadata(response.media_id_string, alt_text=item["alt_text"])
    tweet = api.update_status(media_ids=[response.media_id_string], status=item["text"])
    return tweet

To get the URL of your spreadsheet, head back to your spreadsheet in Google Docs, navigate to File Share Publish to web, and select the Comma-separated values (.csv) format.

This will be the value for your data_url variable.

data_url = "https://docs.google.com/spreadsheets/d/e/1234567890/pub?output=csv"

We’re now ready to test our workflow.

A tweet from our finished bot, showing one of the images we uploaded to Google Drive.

You can now go back to the scheduler block, set the timer to something like an hour, or whatever your preference is, and then deploy your workflow.

If you run into any issues, feel free to reach out! Before you do, a few helpful trouble-shooting tips:

  • Double-check your API keys. You can use print(os.environ.get("TWITTER_API_KEY")) in your script to see if they’re being picked up.
  • Make sure all your images and the spreadsheet are public.
  • You can print the response from Twitter’s API to see if there are any useful error messages.
tweet = api.update_status(media_ids=[response.media_id_string], status=item["text"])
print(tweet)

Check out the rest of Botwiki for tutorials and examples of creative bots, and join us over at botmakers.org and share what you made!

Until next time!

Stefan

Creator of Botwiki and Botmakers, Botwiki editor, and Botmakers community manager.

Enjoyed the tutorial?

Consider supporting Botwiki!

Latest from the blog

Botwiki Interview: Nora Reed
Botwiki Interview: Nora Reed

Discussing Twitter, bots, and reasons to stay hopeful.

Poll: Multi-network bots and followers
Poll: Multi-network bots and followers

Does Twitter get your bot the most followers?

Visit the blog

.