Skip to content

Instantly share code, notes, and snippets.

@MasWag
Created January 18, 2023 03:25
Show Gist options
  • Save MasWag/7d5a7b00b4ed6b7df47f1ad797b9d543 to your computer and use it in GitHub Desktop.
Save MasWag/7d5a7b00b4ed6b7df47f1ad797b9d543 to your computer and use it in GitHub Desktop.
(The document is automatically generated by ChatGPT) An org-mode document that can be tangled to a script to post to mastodon.

Mastodon Posting Script

This script allows you to post a message to Mastodon from the command line. It reads the message from the command line argument or standard input. It uses the Mastodon.py library to interact with the Mastodon API.

Note: Obtaining the Access Token

In order to use this script, you will need to register an application with your Mastodon instance and obtain an access token. Here are the general steps you will need to follow:

  1. Log in to your Mastodon account on the web.
  2. Go to the “settings” page.
  3. Click on the “Development” tab.
  4. Click on the “New Application” button.
  5. Fill in the form with the required information, such as the name of your application, its website, and a short description.
  6. Choose the permissions you want your application to have.
  7. Click the “Submit” button.

Once your application is registered, you will be given a client ID and client secret, as well as the access_token.

import sys
from Mastodon import Mastodon

# Create a Mastodon API client
mastodon = Mastodon(
    access_token = 'YOUR_ACCESS_TOKEN',
    api_base_url = 'https://your.mastodon.instance'
)

# Get the message to post
if len(sys.argv) > 1:
    message = sys.argv[1]
else:
    message = input("Enter the message to post: ")

# Post the message
mastodon.toot(message)

You can run the script by passing the message to post as the first argument, or it will ask you to enter the message if no argument passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment