Every time I create a new post in this blog I like to toot or tweet about it. Instead of manually doing this, I have my Mac Mini run this shell script on a cron job, which does it automatically for me.
The way it works is pretty simple:
- Download my latest blog feed as a json file.
- Check for any new blog posts.
- Compares with a local CSV file that records previous toots.
- Posts new toots to Mastodon.
This is used by kau.sh to auto post new blog posts to his Mastodon.
This script is written in Kotlin and can be run from anyone's command line
brew install kotlin
kotlin tootbot.main.kts
Should be obvious
val mastodonInstance = "mastodon.social"
//val mastodonInstance = "hachyderm.io"
Head on over to your Mastodon Profile -> Developer settings page and create an application. After creating a new application you should find "Your access token".
// Settings page link:
// https://mastodon.social/settings/applications
// https://hachyderm.io/settings/applications
val mastodonToken = "BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX_AAAAAA"
Your blog should make available a json feed which the script will automatically download and parse for new blog posts. If you use Hugo my blog theme Henry has a template you can use.
val jsonFeedUrl = "https://your-blog/feed.json"
After posting a toot to Mastodon, this script will write the status to a CSV file locally called "toots.csv". It simply jots a map of your blog post id and the Mastodon status number.
It additionally also uses this file to prevent duplicate tooting of the same blog post. So remember to not clear this file, if you've started using this script.
val tootsFile = "./toots.csv".toPath()
There are times I want to forcefully toot a blog post that otherwise wouldn't get picked up by the script (e.g. a blog post posted before today). For these cases I like adding the blog post id in this variable, which will make sure to toot the blog post anyway.
val forceToot = listOf(
"blog/2023-02-02-own-your-online-presence/index.md",
)
For now, I run this script on a cron job from command line, on a Mac mini server that I use. I could dockerize it and make it even more easy but this works for me and is pretty simple, so no current plans for that.