I made a Mastodon bot with GoToSocial

I've been running my own GoToSocial ActivityPub server for a while and it's been great. It's Mastodon compatible, super easy to set up and works well as a single-account server despite being in alpha.

👋
Thanks for reading. If you like my work, leave a comment letting me know and follow me on Mastodon.

A fantastic use of any social media service is running bots that automatically post content[1]. I've wanted a bot for the for the HuggingFace Daily Papers page[2] for a while and finally got around to making one that's available here. It uses Google's new Gemini LLM to generate Tweet style summaries for the post body and hashtags.

HF daily Papers Post

I'm documenting the process in case in case it's useful to anyone that wants to do the same. My setup runs GoToSocial in a Docker container with account creation disabled since it's intended to be a single user instance. However, the general steps should be the same for a multi-user instance.

Creating a new account

If you are using the docker service, you will need to connect to the running container. Assuming the container name is gotosocial, you can connect to it with the following command.

docker exec -it gotosocial sh

In the running container, you should have access to the gotosocial CLi. You can create a new account with the following command

./gotosocial admin account create --username <username> --email <email> --password <password>

Next, you need to activate the account

./gotosocial admin account activate --username <username>

Generating an Access Token

While you'd normally need to go through a somewhat complex authentication flow to generate a token for the user, a much simpler method is to use the Access Token Generator by @takahashim to generate your access token.

You can validate your access token by running the following command to verify your user details.

curl -X GET https://<your_instance>/api/v1/accounts/verify_credentials \
  -H "Authorization: Bearer <access_token>"

You should get a JSON response with your user details.

{"id":"...","username":"<username>","acct":"<username>" ... }

Setup the account

At this point you should be able to log into a Mastodon client of your choice to customize your bot's profile and settings. GoToSocial's web interface will allow you to change most things except mark it a a bot account (as of the time of writing). However, that's easy to change via API

curl -X PATCH https://<your_instance>/api/v1/accounts/update_credentials \
    -H 'Authorization: Bearer <your api token>' \
    -F 'bot=true' \
    -F 'locked=false'

Posting content

You can now use the access token to post content to your bot's account by posting to the /api/v1/statuses endpoint[3]. The following command will post a status to your bot's account.

⚠️
Check for bot visibility rules on your server, some servers don't want bots on the local timeline posts should be marked as unlisted instead of public
curl https://<your_instance>/api/v1/statuses \
    -H 'Authorization: Bearer <access_token>" \
    -F 'status=Hello World' \
    -F 'visibility=unlisted'

The equivalent in JavaScript using the fetch API would be

const formData = new FormData();
formData.append("status", "Hello World");
formData.append("visibility", "unlisted");

fetch("https://<your_instance>/api/v1/statuses", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${access_token}`,
  },
  body: formData,
});

Now you just need to build your bot and have it begin posting content. If you're interested in the bot I built, you can find the source code here.


  1. Afolabi, O. (2022) 10 of the most useful Twitter bots you can follow, MUO. MakeUseOf. Available at: https://www.makeuseof.com/most-useful-twitter-bots/ (Accessed: February 10, 2024). ↩︎

  2. Daily Papers (no date) Huggingface.co. Available at: https://huggingface.co/papers (Accessed: February 10, 2024). ↩︎

  3. API documentation (no date) Gotosocial.org. Available at: https://docs.gotosocial.org/en/latest/api/swagger/ (Accessed: February 10, 2024). ↩︎

Subscribe to Another Dev's Two Cents

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe