I just updated the footnote fortune file for Patronizers. Yes, my Patronizers get a Unix fortune file containing all the footnotes from my nonfiction books. I thought it was daft, but apparently a few readers actually use the dang thing. My exhausted brain wondered, “How hard would it be to build a Mastodon bot that posted one of these every few hours?” Turns out: not hard at all.
First, install toot (https://toot.bezdomni.net/). FreeBSD packages it as py311-toot.
Then register an account for your bot, using the regular Mastodon web interface. I registered @quotebot@io.mwl.io. (Yes, I have my own fedi instance. My main account is @mwl@io.mwl.io. No, you can’t have an account on it.)
$ toot login
Enter instance URL [https://mastodon.social]: https://io.mwl.io
This authentication method requires you to log into your Mastodon instance in
your browser, where you will be asked to authorize toot to access your
account. When you do, you will be given an authorization code which you need
to paste here.
Login URL:
https://io.mwl.io/oauth/authorize/?response_type=code&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=read+write+follow&client_id=FPzkCcqnGBLNO5Vo4V95CvfilcyRlMIrOSN1ncgxZmI
Open link in default browser? [Y/n]: n
This server’s default browser is Lynx. For whatever reason it can’t display the entire authorization code. Lynx is used for low-vision accessibility testing, so I suspect that the masto interface has an accessibility problem. I copied the link, opened it in my desktop’s Firefox, and copied the authorization code.
Authorization code: i6OsrQq77knbO4Gq.....
✓ Successfully logged in.
I can now toot from the command line.
$ toot post "test from toot cli"
Toot posted: https://io.mwl.io/@quotebot/113249574738108310
Go look in the web interface, and you’ll see the post. Easy enough.
Posting from a program is easy enough.
$ quote-source | toot post
Now I need a quote source. I could use something database-driven but I happen to have the mwlfortune file handy, so I’ll stick it in a mwlquotes
directory. I’d like more than the footnotes so here’s a sample of another quotes file. Each quote is plain text, separated by a percent sign. I won’t be methodically adding to this, but if I’m digging through something old and see a suitable line I’ll add it.
Someone had brought cake. Someone was a bastard.
%
The only universal configuration language is despair.
Now build the fortune data files.
$ strfile -c '%' mwlfortune
"mwlfortune.dat" created
There were 582 strings
Longest string: 421 bytes
Shortest string: 6 bytes
$ strfile -c '%' bodyquotes
"bodyquotes.dat" created
There were 2 strings
Longest string: 54 bytes
Shortest string: 49 bytes
If you give the directory as an argument to fortune(1), it will pick a fortune at random from the combined files.
$ fortune /home/mwl/mwlquotes
Yes, that's megabytes--you know, the unit below gigabytes. Yes,
megabytes can apply to disks.
Try it a couple more times and you’ll see we get random quotes.
Dumping this into our bot is pretty simple.
$ fortune mwlquotes/ | toot post
Initial tests show a problem, though. Fortunes respect terminal standards, and include mid-sentence newlines. Fediverse posts do not. We need to get rid of the newlines. I wound up with this bot script.
#!/bin/sh
fortune /usr/local/share/mwlquotes/ | tr '\n' ' ' | toot post
Why put this in a script? So I can edit it easily later.
Now put this in my personal cron. Most folks said posting every six hours would be reasonable, so that’s where I’m starting.
13 */6 * * * /usr/local/scripts/quotebot.sh
That’s it. Every six hours, at thirteen minutes past the hour, the bot followers get a random quote from one of my books. Took about two hours to fully implement, including writing this post.