Post status on Twitter

TL;DR

Time to look about posting a new status to Twitter.

After looking at how to Post status on Mastodon (with the addition of Post status on Mastodon - with Mojo::UserAgent), it’s residually useful to look at doing the same thing on Twitter.

I say residually because I’d really like Mastodon to catch up more. The idea of not needed to rely upon a company that makes business on our data is very attractive to me. Alas, they were the first and it’s so easy to do.

Without further ado, let’s meet the code:

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 MojoX::Twitter;
use Data::Dumper;

my $api_key = $ENV{TWITTER_API_KEY} or die 'no API key';
my $api_secret_key = $ENV{TWITTER_API_SECRET_KEY} or die 'no API secret key';
my $access_token = $ENV{TWITTER_ACCESS_TOKEN} or die 'no access token';
my $access_token_secret = $ENV{TWITTER_ACCESS_TOKEN_SECRET}
   or die 'no access token secret';

my $twitter_agent = MojoX::Twitter->new(
   access_token        => $access_token,
   access_token_secret => $access_token_secret,
   consumer_key        => $api_key,
   consumer_secret     => $api_secret_key,
);
say Dumper $twitter_agent->request(
   POST => 'statuses/update',
   {status => (shift // 'whatevah')},
);

Local version here.

We meet again our old friend MojoX::Twitter, which we already met in Getting started with MojoX::Twitter (and a couple of follow-ups, to be fair).

This module leverages the excellent Mojo toolkit that is shipped with Mojolicious, which is our reason why we also saw an implementation of sending a status update to Mastodon using Mojo::UserAgent - if it’s there, why not use it?!?


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