More Mastodon, less Twitter

I was getting more and more annoyed with twitter in the last few months (mostly because I had to click "See [some annoying feature] less often" several times a day) and the whole Musk-takeover finally pushed me over the edge. I've set up an account on Mastodon Technology and plan to use this nice, open source, federated tool more and more, and leave twitter behind.

I never really used twitter as a primary platform, but used it to automatically "promote" some of the stuff that's happening on this website. So now I have to port this tools to use Mastodon. Which turned out to be quite easy:

Mastodon API

Mastodon obviously has an API and a nice Perl client, Mastodon::Client. To access the API I had to set up an "app" and get an access token. Easy.

Blio Integration

This static website is generated using my static site generator, Blio. I run Blio via a build.pl script, that does various things besides just building the HTML. Among those things is auto-posting new posts to Twitter:

sub tweet_new_post {
    my $node = shift;

    my $status = $node->stash->{tweet};
    if (!$status && $node->stash->{autotweet}) {
        $status = "New blog post: ".$node->title;
    }
    return unless $status;
    return if $tweeted{$node->url};

    my $link = ' '.$blio->site_url.'/'.$node->url;

    my $tweet = length($status) > 250  ? substr($status,0,145) : $status;
    $tweet.=$link;
    _tweet($tweet);

    my $fh = $index_file->opena;
    say $fh $node->url;
    close $fh;
}

This checks if a node either contains an explicit tweet or has the autotweet flag set (in which case the status is generated from the node title). It checks in a very simple file-based database if this node ($node->url) has already been posted and aborts if it has (so I don't end up tweeting the same post multiple times).

Then I append the link to the new post to the tweet, after maybe shortening the status if it is too long. Now I call _tweet($tweet), which uses Net::Twitter::Lite::WithAPIv1_1 to post the message. Finally I add the current node url to the file DB.

toot!

Posting to Mastodon is called tooting, so let's do that:

   my $toot = length($status) > 450  ? substr($status,0,450) : $status;
   $toot.=$link;
   _toot($toot);

Toots can be longer than tweets, so I've adapted the truncation a bit.

sub _toot {
    my ( $status ) = @_;

    eval {
        my $conf = do $blio->output_dir->parent->file('mastodon.conf')->stringify;
        my $client = Mastodon::Client->new({
            %$conf,
            coerce_entities => 1,
        });

        my $post = $client->post_status( $status );
        return $post;
    };
    if (!$post || $@) {
        say "error while tooting: ".$@;
        return;
    }
    else {
        say "successfully tooted: ".$post->content;
        return 1;
    }
}

The file mastodon.conf contains the instance URL, access token etc and is in fact a small Perl snippet which I just load using do. Using this config, I initiate a new Mastodon::Client, and call post_status. Very easy!

Now I'm very curious if it will work when I publish this post...