Skip to main content

Mastobot - cheap mastodon bots

Important

Nothing in this project is set in stone except that it's for cheap bots only. Consider your use cases before adopting this library (consult §Use cases). Mastobot is suitable for bots which do not require intensive interaction, e.g. media or polls. Just write your own bot from scratch if you want juicy features. It also aims to be beginner-friendly, even (especially) those who don't understand decorators. You're welcome to contribute if you do (see HACKING.md).

Why

Here is a code excerpt, using the stock Mastodon.py library, which is undoubtedly amazing but somewhat painful to write clean code with.

class Listener(StreamListener):
    def on_notification(self, ntf):
        if ntf["type"] == "mention":
            content = html_to_text(ntf["status"]["content"])
            req = content.split(" ")[1]
            if content.startswith("GET "):
                masto.status_reply(ntf["status"], get(req))
            elif content.startswith("POST "):
                masto.status_reply(ntf["status"], post(req))
            elif content.startswith("DELETE "):
                masto.status_favourite(ntf["status"]["id"])
        # ... and everything else.

listener = Listener()
mastodon.stream_user(listener)

See, as attempts are made to expand the amount of event handlers, complexity quickly accumulates. Mastobot exists to get rid of this problem. Instead, you can save yourself from this cloggy syntax and spaghetti-prone code structure with Mastobot, because it takes care of listener registration and event handling, allowing bot devs to build modularized 2nd-level (1 level of indent) blocks of code. Moreover, Mastobot wraps awkward Mastodon.py dict objects into fancy NamedTuples, which grants you the power to access attributes via dots (obj.attr), not brackets and quoted strings (obj["attr"]). See? Three (3) characters saved!

Examples

Basic usage

from mastobot import Bot
bot = Bot(
    instance_url="https://mastodon.instance",
    access_token="your_access_token",
)

@bot.on_mention("hi")
def respond_to_hi(status):
    name = status.account.username
    return f"hey, {name}!"

bot.run()

In this example, if a user sends the bot "hi", the bot will reply with "hey, {the user's name (handle without instance domain)}!".

Advanced usage

from mastobot import *
import re
bot = Bot(
    instance_url="https://mastodon.instance",
    access_token="your_access_token",
)

def looks_like_brainsize_meme(content: str):
    lines = content.splitlines()
    brain_count = 0
    for ln in lines:
        if re.match("^:brain[0-9]+:", ln):
            count += 1

    if brain_count > 2:
        # looks like brainsize meme
        return True

    return False

# if looks_like_brainsize_meme(status_dict["content"]) == True,
# invoke compliment_meme
@bot.on_home_update(looks_like_brainsize_meme, validation=EVALUATE)
def compliment_meme(status):
    return [
            Boost,
            Favourite,
            Reply("brilliant meme!", visibility=PRIVATE),
        ]

# invoke when re.search("(:hacker_[a-z]:\s)+", status_dict["content"]) is not None
@bot.on_home_update("(:hacker_[a-z]:\s)+", validation=REGEX)
def nice_hacker_font(status):
    return Reply("nice hacker font you have", visibility=DIRECT)

bot.run()

In this one, it will favboost and reply "brilliant meme!" to a post on its home timeline that resembles a brainsize meme.

Installation

It's not on PyPI yet. Mastobot is such a good name, it took me eight seconds to come up with it. I anticipate that it will be, soon.

Install via setuptools

Clone and cd into this repo, python setup.py install --user.

Use cases

Use Mastobot when:

  • Your bot replies to or interacts with certain posts on its home timeline; or
  • Your bot answers some questions people ask it; or
  • Your bot keeps track of some users it's following.

Think of mastobot as a web server. It takes input, catches certain routes, and gives output. Similarly, bots made with mastobot will be able to listen to events and trigger some actions when a certain event occurs.

Don't use Mastobot when:

  • It is merely a shitpost bot who posts hourly, daily, etc. Use a cron job instead.
  • You need to post images or other media. Too fancy. Write your own.
  • You want to harass users. No substitute. Simply don't.

Terminology

I try to keep my terms consistent. Here is a table of them.

Term (in descending order of priority) Definition
status/post/toot self-explanatory
reply a status that is a reply to another
reblog/boost an action that reposts a status
favourite/fav a self-explanatory action
response a reply, boost, or favourite
account/acct an identity registered on mastodon or other fediverse backend with a distinct user@instance handle
user a real person or organization who owns and operates an account
bot developer/botdev a real person or organization who has made a mastodon bot with mastobot
the bot an application that the bot developer has made, designated a mastodon account for, and deployed
notification/notif an event sent from mastodon concerning the bot
mention an account addressed (@) in a status; or a notification addressed to the bot containing a status sent from another account
timeline/TL a stream of statuses visible to the bot, incl. home, local, and public(federated)
update a new status visible to the bot on a timeline

Metadata

Release files for mastobot 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mastobot 0.1.0
File Size Uploaded Provenance Actions
mastobot-0.1.0.tar.gz 14.9 kB Without provenance Details for mastobot-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for mastobot 0.1.0
File Interpreter ABI Platform Actions
mastobot-0.1.0-py3.8.egg Legacy Egg format - - Details for mastobot-0.1.0-py3.8.egg
mastobot-0.1.0-py3-none-any.whl Python 3 none any Details for mastobot-0.1.0-py3-none-any.whl

Total release size: 56.8 kB

Release files / mastobot-0.1.0.tar.gz

Download URL mastobot-0.1.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e894ed75849edb2e4b8bd5b3c3e422d69e5735fa17e0e49f404e1db842039e83
BLAKE2b-256 checksum
How to use checksums
c3fcc112aa0a76fad76fc625a92ed94dacf343131833630b7a3a9e713f5cd794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.42.1 CPython/3.8.3

Release files / mastobot-0.1.0-py3.8.egg

Download URL mastobot-0.1.0-py3.8.egg
Size 27.2 kB
Tags Egg
SHA-256 checksum
How to use checksums
44a236781f70943ee9ae245e29c96dc3c605fa0b7e3978cb721bbb65b6776318
BLAKE2b-256 checksum
How to use checksums
c70c4bce070a4782c2fdf9e898a9e60c00d55d694c6c7ff19e463542c8eb48d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.42.1 CPython/3.8.3

Release files / mastobot-0.1.0-py3-none-any.whl

Download URL mastobot-0.1.0-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b7ebc05a1a44b9ef559cd866cec0233bfa0d1d4dd55d0901ebd37384a4be46e3
BLAKE2b-256 checksum
How to use checksums
8969c7457621d0ed0b730277fdcf8c36115855cf2c4a0a653bc7c13fcf99e0db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.42.1 CPython/3.8.3

Security for mastobot 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page