How to make a Mastodon bot that posts your favorite quotes or lyrics

Easy as 1-2-3!

📅 August 17, 2023

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

While we don’t typically feature these kinds of bots on Botwiki, this is still a fun introduction to the world of botmaking. And with Pipedream, it’s super easy, with very little “coding” necessary.

Yo, we got you.

(Thanks to @thzinc for the updated image!)

— botwiki.org (@botwiki@mastodon.social) 2022-11-10T19:58:16.490Z

First, we’ll need to create a new Mastodon account for our bot and get our access token. We will also need to set up an account on Pipedream. Now we can connect our bot account inside Pipedream. We’ll search for “mastodon” and follow the instructions on adding our domain and token.

Next, let’s head over to the workflow dashboard and create a new workflow.

Screenshot of the Pipedream workflows dashboard with the New button highlighted.
Screenshot of a new workflow trigger being added in Pipedream.

Choose Schedule and Custom Interval as a trigger for the bot. (Here are some tips on how often your bot should post.)

Screenshot of  a new workflow trigger being added 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 being edited.

This is where we can put our quotes, and a few lines of code that will pick a random one from our list.

import random

def handler(pd: "pipedream"):

    quotes = [
        {
            "quote": "Time you enjoy wasting is not wasted time.",
            "author": "Marthe Troly-Curtin"
        },
        {
            "quote": "You only live once, but if you do it right, once is enough.",
            "author": "Mae West"
        },
        {
            "quote": "I finally figured out the only reason to be alive is to enjoy it",
            "author": "Rita Mae Brown"
        },
        {
            "quote": "Never be afraid to sit awhile and think.",
            "author": "Lorraine Hansberry"
        },
        {
            "quote": "We can do anything we want to if we stick to it long enough.",
            "author": "Helen Keller"
        }
    ]

    quote = random.choice(quotes)

    print(quote)
    return {"quote": quote}

If you’d like to use lyrics, you will want to split them into multiple lines using triple quotes. Here’s what that would look like.

import random

def handler(pd: "pipedream"):
    quotes = [
        {
            "quote": """
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
            """,
            "author": "Rick Astley"
        }
    ]

    quote = random.choice(quotes)

    print(quote)
    return {"quote": quote}

Now we just need the Mastodon step that will post our random quote.

Adding a new Mastodon workflow step.

Look for “post status”.

Adding a new Mastodon workflow step.

Under “Mastodon Account”, choose the account you added earlier, and for value, use:

{{steps.quotes.$return_value.quote.quote}}\n— {{steps.quotes.$return_value.quote.author}}

Note the \n used to add a line break in your message.

If you’d like to include more fields, like the year, or the name of a book the quote is from, you can simple add this information to the list of your quotes inside the Python block:

        {
            "quote": "Time you enjoy wasting is not wasted time.",
            "author": "Marthe Troly-Curtin",
            "source": "Phrynette Married",
            "year": 1911
        }

And your “value” will then look like this.

{{steps.quotes.$return_value.quote.quote}}\n— {{steps.quotes.$return_value.quote.author}} ({{steps.quotes.$return_value.quote.source}}, {{steps.quotes.$return_value.quote.year}})

Options for adding a Mastodon post.

Make sure to also enable the option to split long messages into a thread.

Now let’s test our workflow.

Selecting test workflow from a dropdown of options for our workflow step.
Results of successfully posting a message on Mastodon.
An example quote posted by our bot.

Looking good!

Now you can click the Deploy button in your workflow to publish it. And there you have it, your very own quote bot. Here’s the finished workflow for reference. If you get stuck at any point, feel free to join the Botmakers Slack group and ask for help.

And if you’d like to see what else you can do with Pipedream, I have a series of tutorials on my blog.

I hope you enjoyed the tutorial, and 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

Eight years of Botwiki, and 2,000 bots
Eight years of Botwiki, and 2,000 bots

Happy birthday, Botwiki!

Cheap Bots, Done Quick suspended, this time for good
Cheap Bots, Done Quick suspended, this time for good

So long, and thanks for all the bots.

Twitter shutting down free access to their API on February 9
Twitter shutting down free access to their API on February 9

I keep saying it's an end of an era quite often these days.

Visit the blog

.