Skip to content

Instantly share code, notes, and snippets.

Last active Jul 9, 2019
What would you like to do?
Python script to toot on your mastodon instance from the cli
#!/usr/bin/env python3
# pip3 install
# use in the python cli
# ./ "your message here"
# or just ./ to be prompted
# sable cantus
import sys
import os
import pkg_resources
from pkg_resources import DistributionNotFound
from mastodon import Mastodon
# change this to be your mastodon server url
server = ''
# get your access_token from the Development section
token = '<your-token-here>'
# make sure is installed
sys.exit('dependency needed: $ pip3 install')
# check for toots at the cli
if len(sys.argv) > 1:
m = []
message = ''
for i in range(len(sys.argv)):
m.pop(0) # removes the script name from the list
for word in m:
message = message + word
message = message + ' '
# get the message from the user
message = input('your message: ')
# don't post if there is no text
if len(message) == 0:
sys.exit("no message data entered")
# create an api instance
mastodon = Mastodon(
access_token = token,
api_base_url = server
# call the instance and toot, returning the toot DICT
tooter = mastodon.toot(message)
# print out the uri of the toot for confirmation
print("tooted: ", tooter['uri'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment