A silly bot for the social network mastodon

Lertsenem ce7c3c88b1 Bumping version 6 years ago
.gitignore 3820fe5ae8 Adding gitignore 6 years ago
LICENSE 2675f107f0 Adding WTFPL 6 years ago
README.md 11575096ca RST to MD translation of README 6 years ago
main.py ce7c3c88b1 Bumping version 6 years ago


KRS-One Bot for Mastodon

This is a python script using the Mastodon.py library for a silly purpose.

Basically, the script will search the current Federated Timeline and Notifications Timeline, and will answer "THAT'S THE SOUND OF DA POLICE!" to any toot ending with "toot toot" (there's a regexp to account for punctuation).

Yeah, it's silly. It's also a pretty simple introduction to basic Mastodon.py usage.

I want to play with the bot!

You will need a Mastodon account, and you'll find one running at @KRSOneBot@mastodon.lertsenem.com. Enjoy yourself.


To run this script, you will of course need python 3. The following modules are also required:

  • Mastodon.py, for Mastodon API interactions
  • BeautifulSoup4, for very simple HTML parsing


For a simpler installation, I recommend using a virtualenv.

$> apt install python3-virtualenv

(You'll need to adapt that command if your package manager is not apt)

$> mkdir krsonebot
$> cd krsonebot
$> git clone https://git.lertsenem.com/lertsenem/mastodon_krsonebot krsonebot
$> virtualenv -p python3 venv
$> . venv/bin/activate
(venv)$> pip install Mastodon.py beautifulsoup4
(venv)$> python krsonebot/main.py --help

usage: main.py [-h] [-V] [-v] [-i ID_FILE] [-t TOKEN_FILE] [-d DATA_FILE]
               [-l LOGIN] [-p PASSWORD]

positional arguments:
  fullhandle            full mastodon handle

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show bot version
  -v, --verbose         more verbose logs
  -i ID_FILE, --id-file ID_FILE
                        the id file
  -t TOKEN_FILE, --token-file TOKEN_FILE
                        the token file
  -d DATA_FILE, --data-file DATA_FILE
                        the data file
  -l LOGIN, --login LOGIN
                        the login to use, needed the first time
  -p PASSWORD, --password PASSWORD
                        the password to use, needed the first time


First, you'll need a mastodon account, on the instance of your choice.

Next, you can run the krsonebot with your handle ('user@instance') as an argument.

(venv)$> python krsonebot/main.py user@instance

The first time you will be prompted for your credentials, which will then be saved in mstdn_krsonebot.* files. Those files holds secrets for a full access to your account, keep them secret. You can also revoke the bot access to your account any time you want by going into your account settings and reviewing the applications permissions.

For a repeated usage, on a dedicated account for example, I recommend setting a cron task to run every minutes or so:

*/1 * * * * myuser cd path/to/krsonebot/ && ./venv/bin/python krsonebot/main.py

Note that after the first time the handle to use is saved in one of the mstdn_krsonebot.* files, so you don't need to precise it in the command line.