Zottels Zeug
 Fri, 10 Jan 2014 23:51:44 +0100 zuletzt bearbeitet: Fri, 10 Jan 2014 23:55:55 +0100  
As this post might be of interest for an international audience who might find it using a search engine, I'll post it in English.

Ever wondered how to write a script to post arbitrary stuff to the Red Matrix, like the output of any script or, in my case, RSS/Atom feeds? (feed2red.pl is already working, but not ready yet, I'll cover it here when it is.)

Here's how to do it in Perl. Kudos to Tazman Devil, his bash script (using curl) taught me what I had to do.

We can use the Red API for posting. That means we have to go to <server>/api/statuses/update?channel=<channel> and provide what we want to post via POST there.

So we need some kind of http client. That would be LWP::UserAgent, which I think is automatically installed with most Perl distributions, but you might have to install using your package management system first.

So, let's go:
use LWP::UserAgent;

my $red = LWP::UserAgent->new();

LWP::UserAgent has method called post to POST something at a web address. In the way I use it, it takes the URL to post to as a string and the keys and values to set as a hash reference. We need to set the key "status".

The post method will return a HTTP::Response object. We can use its methods is_error and message to handle errors.

During the following examples, I'll use red.zottel.net as the Red server and zottelszeug as the channel address—please replace that with your own values.

Ok, so let's post:
my $response = $red->post("https://red.zottel.net/api/statuses/update?channel=zottelszeug",
    [ status => "A wonderful post!" ]);
if ($response->is_error)
    {
    print STDERR "Error posting to Red: " . $response->message . "\n";
    exit(1);
    }

But wait—we have to be authenticated first! We could use OAuth here, which is complicated, or we can just provide our username and password using basic auth.

To do that, we must set the corresponding HTTP headers. We can set our default headers used by LWP::UserAgent by using its default_headers method. We give it an HTTP::Headers object, where we can conveniently set the auth headers:
my $hed = HTTP::Headers->new();
$hed->authorization_basic('<email address of Red account>', '<password>');
$red->default_headers($hed);

That's it. We're ready to post.

Here's the whole script with some variables to allow for easy adaptation:
#!/usr/bin/perl

use LWP::UserAgent;

my $redServer = 'https://yourserver.domain';
my $user = 'your@red.user'; # email address you use at Red login
my $password = 'yOuRrEdPaSsWoRd';
my $channel = 'channeltopostto'; # channel nickname as in https://yourserver.domain/channel/channeltopostto

my $status = 'I will post this message.';

my $red = LWP::UserAgent->new();
my $hed = HTTP::Headers->new();
$hed->authorization_basic($user, $password);
$red->default_headers($hed);

my $response = $red->post("$redServer/api/statuses/update?channel=$channel",
    [ status => $status ]);
if ($response->is_error)
    {
    print STDERR "Error posting to Red: " . $response->message . "\n";
    exit(1);
    }

#Red-Matrix #Perl