Post status on Mastodon

TL;DR

Where we see that it is extremely simple to post a status on a social based on Mastodon.

Here, of course, we’re using Perl:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/env perl
use 5.024;
use warnings;
use experimental qw< postderef signatures >;
no warnings qw< experimental::postderef experimental::signatures >;
use HTTP::Tiny;
use Data::Dumper;

my $uri   = $ENV{MASTODON_URI} // 'https://octodon.social/api/v1/statuses';
my $token = $ENV{MASTODON_TOKEN} or die 'no token';
say Dumper mastodon_post_status($uri, $token, shift // 'whatevah!');

sub mastodon_post_status ($uri, $token, $status, $visibility = 'private') {
   return HTTP::Tiny->new->post_form(
      $uri,
      { # form data
         status => $status,
         visibility => $visibility,
      },
      { # options
         headers => { Authorization => "Bearer $token" },
      }
   );
}

Local version here.

It’s a kind of exercise in minimalism, most probably you should try and use Mojo::UserAgent instead but it’s working.

It should work on any Mastodon instance, I tried it with Octodon, which is where I hang out as polettix.

To use the script, you need to at least set the OAuth 2 token, that you can get from the (web) application. You can also do it from the command line: Obtaining client app access. Just set it in the environment:

# this is optional and can be ignored (it's commented for a reason)
#MASTODON_URI='...'

# this you MUST populate
MASTODON_TOKEN='...'

perl postatus 'Hello... myself, this is private by default'

That’s it for today!


Comments? Octodon, , GitHub, Reddit, or drop me a line!