// Install Mastodon.py

In assumption you've already installed Python and pip just install Mastodon.py with the following command:

user@linux: ~$ sudo pip install mastodon.py
> _

// Create a Mastodon application

  1. Log into Mastodon
  2. Settings
  3. Development
  4. Click on "New Application"
  5. Choose a name and let the rest as it is. Click on "Submit"
  6. Click on the name of the app you've just created to get to your tokens
  7. Copy the string behind "Your access token" into a new file and name it "token.secret"

// The Code

  1. Create a new file and name it e.g. "bot.py"
  2. Add the following code
  3. Customize it
from mastodon import Mastodon

# ~~~~ Set up Mastodon ~~~~
mastodon = Mastodon(
    access_token = 'token.secret',
    api_base_url = 'https://botsin.space/' # ~~~~ Insert your instance ~~~~

mastodon.status_post('Hello world!') # ~~~~ The content of your toot ~~~~

Note: Change the instance name in the code to the instance the application was made on.

// Run the code

user@linux: ~$ python3 bot.py
> _

Note: Please remember: please don't use this code to spam the Fediverse. You could test your bot on https://botsin.space/.

// Reference code

Just click here to get the source code.