Verified Commit 4535fcfd authored by Luc Didry's avatar Luc Didry

Add /omg endpoint to use Oh My Gif

parent 311f5423
......@@ -105,3 +105,5 @@ Une commande slash vers https://giphy.example.org/money/ vous donnera ce gif:
Une commande slash vers https://giphy.example.org/steven ajoutera "Steven Universe" au début du texte recherché.
Une commande slash vers https://giphy.example.org/cat/, vous donnera un gif de [Cat as a service](http://cataas.com/). Si vous ajoutez du texte à cette commande, le texte sera ajouté au gif.
Une commande slash vers https://giphy.example.org/omg/, vous utiliserez l’instance [OhMyGif](https://github.com/PhieF/OhMyGif) <https://omg.phie.ovh/> à la place de Giphy (OhMyGif est open source, c’est plutôt cool).
......@@ -106,3 +106,5 @@ Point a slash command to https://giphy.example.org/money/, you'll get this gif:
Point a slash command to https://giphy.example.org/steven and the research will be prepended with "Steven Universe".
Point a slash command to https://giphy.example.org/cat/, you'll get a gif of a cat from [Cat as a service](http://cataas.com/). If you send text with that command, the text will be added the gif.
Point a slash command to https://giphy.example.org/omg/, you'll use the [OhMyGif](https://github.com/PhieF/OhMyGif) instance <https://omg.phie.ovh/> instead of Giphy (OhMyGif is open source, that's kind of cool).
......@@ -218,6 +218,76 @@ sub startup {
$c->rendered(401);
}
});
$r->any('/omg' => sub {
my $c = shift;
my $text = $c->param('text');
if ($c->req->method eq 'GET') {
my @params = split('&', $c->req->content->asset->slurp);
for my $param (@params) {
my @h = split('=', $param);
$c->param($h[0] => $h[1]);
$text = $h[1] if ($h[0] eq 'text');
}
}
my ($token, $channel_id, $channel_name, $command, $team_domain, $team_id, $user_id, $user_name, $response_url) =
($c->param('token'), $c->param('channel_id'), $c->param('channel_name'), $c->param('command'), $c->param('team_domain'), $c->param('team_id'), $c->param('user_id'), $c->param('user_name'), $c->param('response_url'));
if ((!defined($c->config('token')) &&
defined($token) &&
defined($channel_id) &&
defined($channel_name) &&
defined($command) &&
defined($team_domain) &&
defined($team_id) &&
defined($user_id) &&
defined($user_name) &&
defined($response_url))
|| (defined $c->config('token') && $token eq $c->config('token'))
) {
my $url = Mojo::URL->new('https://omg.phie.ovh/get.php');
$url->query(
query => $text,
);
$c->ua->get($url => sub {
my ($ua, $tx) = @_;
if (my $res = $tx->success) {
if (scalar(@{$res->json})) {
my $gif = $res->json->[rand(scalar(@{$res->json}))]->{url};
if ($gif !~ m/^http/) {
$gif = 'https://omg.phie.ovh/'.$gif;
}
$gif = $text.' ⟶ '.$gif unless (defined($c->param('notext')) && $c->param('notext'));
$c->render(
json => {
"response_type" => "in_channel",
"text" => $gif
}
);
} else {
$c->render(
json => {
"response_type" => "ephemeral",
"text" => "Sorry, no results from OhMyGif"
}
);
}
} else {
my $err = $tx->error;
my $text = ($err->{code}) ? "Sorry, $err->{code} response: $err->{message}" : "Sorry, $err->{message}";
$c->render(
json => {
"response_type" => "ephemeral",
"text" => $text
}
);
}
});
$c->render_later;
} else {
$c->rendered(401);
}
});
}
1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment