Working with Mastodon

Updated 19 Juillet 2019

Proxy configuration

Access to other federated resources can be configured via a proxy. Add the necessary settings:

/var/calculate/www/mastodon/live/.env.production

http_proxy=http://proxy.example.org:8888

Restart Mastodon:

/etc/init.d/mastodon-web restart

Using bots to organize groups

A bot mastodon-bot-autoresponder will be used to organized groups

Creating and configuring a bot

Create the mastodonbot user:

useradd -d /var/calculate/mastodonbot mastodonbot

Clone the repository containing the bot source code for the user:

su - mastodonbot

git clone https://github.com/drequivalent/mastodon-bot-autoresponder mastodon-group-bot

Set up a virtual environment for Python and enable it

cd mastodon-group-bot

python3 -m venv env

source env/bin/activate

Set the dependencies for the bot

pip install Mastodon.py beautifulsoup4==4.6.0

Create a basic script to start the daemon:

/etc/init.d/mastodon-group-bot
#!/sbin/openrc-run
# Copyright 2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

name="Mastodon Group Bot daemon"
description="Mastodon Group Bot daemon"
BOTNAME=${SVCNAME#*.}
supervisor=supervise-daemon
output_log=/var/log/mastodon-group-bot-$BOTNAME.log
error_log=/var/log/mastodon-group-bot-$BOTNAME.log
command_user=mastodonbot
command="/var/calculate/mastodonbot/mastodon-group-bot/env/bin/python3"
command_args="autoresponder.py -c config.$BOTNAME.json"
directory="/var/calculate/mastodonbot/mastodon-group-bot"
pidfile="/run/mastodon-group-bot-$BOTNAME.pid"
command_background=true

depend() {
    use net
}

start_pre() {
    checkpath -f -o mastodonbot -m 0600 /var/log/mastodon-group-bot-$BOTNAME.log
}

Set the permissions for the daemon to start:

chmod 755 /etc/init.d/mastodon-group-bot

Adding your bot to Mastodon

Register the bot as an application on your server. To do this, open your browser and login to Mastodon under the name the bot will be using. Now run tokentool.py:

su - mastodonbot

cd mastodon-group-bot

source env/bin/activate

python tokentool.py
Choose your action:
[r]: register an app to get client_id and client_secret
[a]: authenticate to get access_token
if you're setting up a bot for the first time, choose [r].
> r
App name [autoresponder]: examplebot
Instance base URL [https://mastodon.social]: https://mastodon.example.org
App registered.
client_id: eidNondeabryievrewkasfakuvcorigovpefNifZibr
client_secret: jokpinshedCauHalsishpEbodIatsilyethjuOvtyfs
While you're at it, do you want to also authenticate?
y/n> y
Visit the following link, authenticate, and bring back the code that the page gives you afterwards.
NB: that code is *not* your access token.
https://mastodon.example.org/oauth/authorize?client_id=eidNondeabryievrewkasfakuvcorigovpefNifZibr&response_type=code&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=read+write
> MT-afgOKW4wffDpGjXUEB1dm135mcSwX9zql4c3ictM
Got access token: cNsH2V8AVJrrovdePEK7GFy6PA7ywHErU1xkwuIX5UM
Testing...
Success, authenticated as newbot.

Note

To get the access code, ~~MT-afgOKW4wffDpGjXUEB1dm135mcSwX9zql4c3ictM~ click on the generated link.

Confirming access for bot

Obtaining a token for bot

At registration time, you will get:

  • client_id = eidNondeabryievrewkasfakuvcorigovpefNifZibr
  • client_secret = jokpinshedCauHalsishpEbodIatsilyethjuOvtyfs
  • access_token = cNsH2V8AVJrrovdePEK7GFy6PA7ywHErU1xkwuIX5UM

Configure your bot:

/var/calculate/mastodonbot/mastodon-group-bot/config.examplebot.json
{
    "base_url": "https://mastodon.example.org",
    "client_id": "eidNondeabryievrewkasfakuvcorigovpefNifZibr",
    "client_secret": "oeCMH9L_OeuuuV699334Rgea3o_0R3kYhfCKu9ZYCh4",
    "access_token": "cNsH2V8AVJrrovdePEK7GFy6PA7ywHErU1xkwuIX5UM",
    "message": "Hello, I'm a group bot. Follow me and then write something, and I will boost it. The admins of the group are:",
    "message_welcome": "Welcome our new member: ",
    "admins": [],
    "state_file": "/var/calculate/mastodonbot/mastodon-group-bot/state.examplebot"
}

Create a symlink pointing to examplebot:

ln -sf mastodon-group-bot /etc/init.d/mastodon-group-bot.examplebot

Launch your bot and add it to autostart:

/etc/init.d/mastodon-group-bot.examplebot start

rc-update add mastodon-group-bot.examplebot