Commit 8b47d9b4 authored by Luc Didry's avatar Luc Didry

First commit. Working

parents
Pipeline #6761 skipped
local/
giphy_mat_hooker.conf
script/hypnotoad.pid
[![](https://framagit.org/assets/gitlab_logo-cdf021b35c4e6bb149e26460f26fae81e80552bc879179dd80e9e9266b14e894.png)](https://framagit.org)
![English:](https://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/Flag_of_the_United_Kingdom.svg/20px-Flag_of_the_United_Kingdom.svg.png) **Framasoft uses GitLab** for the development of its free softwares. Our Github repositories are only mirrors.
If you want to work with us, **fork us on [framagit.org](https://framagit.org)**. (no registration needed, you can sign in with your Github account)
![Français :](https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Flag_of_France.svg/20px-Flag_of_France.svg.png) **Framasoft utilise GitLab** pour le développement de ses logiciels libres. Nos dépôts Github ne sont que des miroirs.
Si vous souhaitez travailler avec nous, **forkez-nous sur [framagit.org](https://framagit.org)**. (l'inscription n'est pas nécessaire, vous pouvez vous connecter avec votre compte Github)
* * *
# Giphy Mat Hooker
The `Mat` stand for [Mattermost](http://www.mattermost.org/).
This service allows you to create [slash commands](http://docs.mattermost.com/developer/slash-commands.html) on Mattermost to search gifs on <http://giphy.com/>.
## License
Giphy Mat Hooker is licensed under the terms of the AGPLv3. See the LICENSE file.
# Installation
## Dependencies
* Carton : Perl dependencies manager, it will get what you need, so don't bother for dependencies (but you can read the file `cpanfile` if you want).
```shell
sudo cpan Carton
```
## The real installation
After installing Carton :
```shell
git clone https://git.framasoft.org/framasoft/giphymathooker.git
cd giphymathooker
carton install
cp giphy_mat_hooker.conf.template giphy_mat_hooker.conf
vi giphy_mat_hooker.conf
```
The configuration file is self-documented.
## Usage
### Launch manually
This is good for test, not for production.
```
carton exec hypnotoad script/giphy_mat_hooker
# stop it
carton exec hypnotoad -s script/giphy_mat_hooker
```
Yup, that's all, it will listen at "http://127.0.0.1:8080".
For more options (interfaces, user, etc.), change the configuration in `giphy_mat_hooker.conf` (have a look at http://mojolicio.us/perldoc/Mojo/Server/Hypnotoad#SETTINGS for the available options).
### Systemd
```
sudo su
cp utilities/giphymathooker.service /etc/systemd/system/
vi /etc/systemd/system/giphymathooker.service
systemctl daemon-reload
systemctl enable giphymathooker.service
systemctl start giphymathooker.service
```
## Nginx
You may want to put GHM behind a reverse proxy. Have a look at `utilities/giphymathooker.nginx` for an example.
# How to use in Mattermost?
First, have a look at the [Mattermost documentation about slash commands](http://docs.mattermost.com/developer/slash-commands.html).
Then, create your slash command, using the URL of your installation (say https://giphy.example.org), choose `GET` or `POST` method, it doesn't matter, GMH will answer to both.
Finally, put the token provided by Mattermost in your GMH configuration file. Or not, if you want anybody to be able to use it if he provides the mattermost parameters.
requires "Mojolicious";
requires "Mojolicious::Plugin::DebugDumperHelper";
# carton snapshot format: version 1.0
DISTRIBUTIONS
CPAN-Meta-2.150005
pathname: D/DA/DAGOLDEN/CPAN-Meta-2.150005.tar.gz
provides:
CPAN::Meta 2.150005
CPAN::Meta::Converter 2.150005
CPAN::Meta::Feature 2.150005
CPAN::Meta::History 2.150005
CPAN::Meta::Merge 2.150005
CPAN::Meta::Prereqs 2.150005
CPAN::Meta::Spec 2.150005
CPAN::Meta::Validator 2.150005
requirements:
CPAN::Meta::Requirements 2.121
CPAN::Meta::YAML 0.008
Carp 0
ExtUtils::MakeMaker 6.17
JSON::PP 2.27200
Parse::CPAN::Meta 1.4414
Scalar::Util 0
perl 5.008
strict 0
version 0.88
warnings 0
IO-Socket-IP-0.37
pathname: P/PE/PEVANS/IO-Socket-IP-0.37.tar.gz
provides:
IO::Socket::IP 0.37
requirements:
IO::Socket 0
Socket 1.97
Test::More 0.88
Module-Build-0.4218
pathname: L/LE/LEONT/Module-Build-0.4218.tar.gz
provides:
Module::Build 0.4218
Module::Build::Base 0.4218
Module::Build::Compat 0.4218
Module::Build::Config 0.4218
Module::Build::Cookbook 0.4218
Module::Build::Dumper 0.4218
Module::Build::Notes 0.4218
Module::Build::PPMMaker 0.4218
Module::Build::Platform::Default 0.4218
Module::Build::Platform::MacOS 0.4218
Module::Build::Platform::Unix 0.4218
Module::Build::Platform::VMS 0.4218
Module::Build::Platform::VOS 0.4218
Module::Build::Platform::Windows 0.4218
Module::Build::Platform::aix 0.4218
Module::Build::Platform::cygwin 0.4218
Module::Build::Platform::darwin 0.4218
Module::Build::Platform::os2 0.4218
Module::Build::PodParser 0.4218
requirements:
CPAN::Meta 2.142060
CPAN::Meta::YAML 0.003
Cwd 0
Data::Dumper 0
ExtUtils::CBuilder 0.27
ExtUtils::Install 0
ExtUtils::Manifest 0
ExtUtils::Mkbootstrap 0
ExtUtils::ParseXS 2.21
File::Basename 0
File::Compare 0
File::Copy 0
File::Find 0
File::Path 0
File::Spec 0.82
File::Temp 0.15
Getopt::Long 0
Module::Metadata 1.000002
Parse::CPAN::Meta 1.4401
Perl::OSType 1
Pod::Man 2.17
TAP::Harness 3.29
Test::More 0.49
Text::Abbrev 0
Text::ParseWords 0
perl 5.006001
version 0.87
Mojolicious-6.62
pathname: S/SR/SRI/Mojolicious-6.62.tar.gz
provides:
Mojo undef
Mojo::Asset undef
Mojo::Asset::File undef
Mojo::Asset::Memory undef
Mojo::Base undef
Mojo::ByteStream undef
Mojo::Cache undef
Mojo::Collection undef
Mojo::Content undef
Mojo::Content::MultiPart undef
Mojo::Content::Single undef
Mojo::Cookie undef
Mojo::Cookie::Request undef
Mojo::Cookie::Response undef
Mojo::DOM undef
Mojo::DOM::CSS undef
Mojo::DOM::HTML undef
Mojo::Date undef
Mojo::EventEmitter undef
Mojo::Exception undef
Mojo::Headers undef
Mojo::HelloWorld undef
Mojo::Home undef
Mojo::IOLoop undef
Mojo::IOLoop::Client undef
Mojo::IOLoop::Delay undef
Mojo::IOLoop::Server undef
Mojo::IOLoop::Stream undef
Mojo::JSON undef
Mojo::JSON::Pointer undef
Mojo::Loader undef
Mojo::Log undef
Mojo::Message undef
Mojo::Message::Request undef
Mojo::Message::Response undef
Mojo::Parameters undef
Mojo::Path undef
Mojo::Reactor undef
Mojo::Reactor::EV undef
Mojo::Reactor::Poll undef
Mojo::Server undef
Mojo::Server::CGI undef
Mojo::Server::Daemon undef
Mojo::Server::Hypnotoad undef
Mojo::Server::Morbo undef
Mojo::Server::PSGI undef
Mojo::Server::PSGI::_IO undef
Mojo::Server::Prefork undef
Mojo::Template undef
Mojo::Transaction undef
Mojo::Transaction::HTTP undef
Mojo::Transaction::WebSocket undef
Mojo::URL undef
Mojo::Upload undef
Mojo::UserAgent undef
Mojo::UserAgent::CookieJar undef
Mojo::UserAgent::Proxy undef
Mojo::UserAgent::Server undef
Mojo::UserAgent::Transactor undef
Mojo::Util undef
Mojo::WebSocket undef
Mojolicious 6.62
Mojolicious::Command undef
Mojolicious::Command::cgi undef
Mojolicious::Command::cpanify undef
Mojolicious::Command::daemon undef
Mojolicious::Command::eval undef
Mojolicious::Command::generate undef
Mojolicious::Command::generate::app undef
Mojolicious::Command::generate::lite_app undef
Mojolicious::Command::generate::makefile undef
Mojolicious::Command::generate::plugin undef
Mojolicious::Command::get undef
Mojolicious::Command::inflate undef
Mojolicious::Command::prefork undef
Mojolicious::Command::psgi undef
Mojolicious::Command::routes undef
Mojolicious::Command::test undef
Mojolicious::Command::version undef
Mojolicious::Commands undef
Mojolicious::Controller undef
Mojolicious::Lite undef
Mojolicious::Plugin undef
Mojolicious::Plugin::Charset undef
Mojolicious::Plugin::Config undef
Mojolicious::Plugin::Config::Sandbox undef
Mojolicious::Plugin::DefaultHelpers undef
Mojolicious::Plugin::EPLRenderer undef
Mojolicious::Plugin::EPRenderer undef
Mojolicious::Plugin::HeaderCondition undef
Mojolicious::Plugin::JSONConfig undef
Mojolicious::Plugin::Mount undef
Mojolicious::Plugin::PODRenderer undef
Mojolicious::Plugin::TagHelpers undef
Mojolicious::Plugins undef
Mojolicious::Renderer undef
Mojolicious::Routes undef
Mojolicious::Routes::Match undef
Mojolicious::Routes::Pattern undef
Mojolicious::Routes::Route undef
Mojolicious::Sessions undef
Mojolicious::Static undef
Mojolicious::Types undef
Mojolicious::Validator undef
Mojolicious::Validator::Validation undef
Test::Mojo undef
ojo undef
requirements:
ExtUtils::MakeMaker 0
IO::Socket::IP 0.37
JSON::PP 2.27103
Pod::Simple 3.09
Time::Local 1.2
Mojolicious-Plugin-DebugDumperHelper-0.02
pathname: L/LD/LDIDRY/Mojolicious-Plugin-DebugDumperHelper-0.02.tar.gz
provides:
Mojolicious::Plugin::DebugDumperHelper 0.02
requirements:
ExtUtils::MakeMaker 0
Mojolicious 6.11
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
{
####################
# Hypnotoad settings
####################
# see http://mojolicio.us/perldoc/Mojo/Server/Hypnotoad for a full list of settings
hypnotoad => {
# array of IP addresses and ports you want to listen to
listen => ['http://127.0.0.1:8080'],
},
######################
# Application settings
######################
# Giphy API key.
# The default is the dev key from Giphy, which is rate-limited.
# See https://github.com/Giphy/GiphyAPI to know how to get
# a production API key
#apikey => 'dc6zaTOxFJmzC',
# Mattermost token
# When you create a slash command in mattermost, you get a token.
# Put it here if you want that service to be available to your team
# only.
# If you do not provide a token, it will check for parameters that
# Mattermost usually send with a slash command.
#token => 'something'
};
package GiphyMatHooker;
use Mojo::Base 'Mojolicious';
use Mojo::Upload;
sub startup {
my $self = shift;
$self->plugin('Config', {
default => {
apikey => 'dc6zaTOxFJmzC',
}
});
$self->plugin('DebugDumperHelper');
# Router
my $r = $self->routes;
# Normal route to controller
$r->any('/' => sub {
my $c = shift;
# This is only to check that the request comes (apparently) from mattermost
# I know that I should test the token, but it would prevent other users from
# framateam to use this service
# To enable token authent, just set a token in the configuration file
if ((!defined($c->config('token')) &&
defined($c->param('token')) &&
defined($c->param('channel_id')) &&
defined($c->param('channel_name')) &&
defined($c->param('command')) &&
defined($c->param('team_domain')) &&
defined($c->param('team_id')) &&
defined($c->param('user_id')) &&
defined($c->param('user_name')) &&
defined($c->param('response_url')))
|| (defined $c->config('token') && $c->param('token') eq $c->config('token'))
) {
my $text = $c->param('text');
my $url = Mojo::URL->new('https://api.giphy.com/v1/gifs/search');
$url->query(
q => $text,
api_key => $c->config('apikey'),
limit => 25
);
$c->ua->get($url => sub {
my ($ua, $tx) = @_;
if (my $res = $tx->success) {
$c->render(
json => {
"response_type" => "in_channel",
"text" => $res->json->{data}->[rand(25)]->{images}->{original}->{url}
}
);
}
});
$c->render_later;
} else {
$c->rendered(401);
}
});
}
1;
*
!.gitignore
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the Mojolicious real-time web framework!</title>
</head>
<body>
<h2>Welcome to the Mojolicious real-time web framework!</h2>
This is the static document "public/index.html",
<a href="/">click here</a> to get back to the start.
</body>
</html>
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
use Mojolicious::Commands;
# Start command line interface for application
Mojolicious::Commands->start_app('GiphyMatHooker');
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
my $t = Test::Mojo->new('GiphyMatHooker');
$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);
done_testing();
% layout 'default';
% title 'Welcome';
<h2><%= $msg %></h2>
This page was generated from the template "templates/example/welcome.html.ep"
and the layout "templates/layouts/default.html.ep",
<%= link_to 'click here' => url_for %> to reload the page or
<%= link_to 'here' => '/index.html' %> to move forward to a static page. To
learn more, you can also browse through the documentation
<%= link_to 'here' => '/perldoc' %>.
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
upstream giphymathooker {
server 127.0.0.1:8442;
}
server {
listen 80;
listen [::]:80;
server_name giphymathooker.example.org;
access_log /var/log/nginx/giphymathooker.access.log;
error_log /var/log/nginx/giphymathooker.error.log;
location / {
proxy_set_header Host $host;
proxy_pass http://giphymathooker;
proxy_redirect http://giphymathooker https://giphymathooker.example.org;
}
}
[Unit]
Description=Mattermost giphy search service
Documentation=https://git.framasoft.org/framasoft/giphymathooker
Requires=network.target
After=network.target
[Service]
Type=forking
User=www-data
RemainAfterExit=yes
WorkingDirectory=/var/www/giphymathooker/
PIDFile=/var/www/giphymathooker/script/hypnotoad.pid
ExecStart=/usr/local/bin/carton exec hypnotoad script/giphy_mat_hooker
ExecStop=/usr/local/bin/carton exec hypnotoad -s script/giphy_mat_hooker
ExecReload=/usr/local/bin/carton exec hypnotoad script/giphy_mat_hooker
[Install]
WantedBy=multi-user.target
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