-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
91 lines (68 loc) · 2.43 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Simple Rage Face fetcher
*/
namespace RageApp;
use Symfony\Component\HttpFoundation\Response;
// get autoloader
require_once __DIR__ . '/vendor/autoload.php';
if ( file_exists( __DIR__ . '/config.php' ) ) {
require_once __DIR__ . '/config.php';
}
$app = new \Silex\Application();
// slack hooks
// 1. copy the sample config file to config.php
// 2. change the sample array so that your slash command token is the key and you incoming webhook is the value
// 3. add as many as you like
$app['webhooks'] = isset( $webhooks ) ? $webhooks : [ ];
// add fetch service
$app['fetch_rage'] = function ( \Silex\Application $app ) {
$search = $app['request']->get( 'text' );
$search = str_replace( ' ', '+', $search );
// fetch from alltherage
$client = new \GuzzleHttp\Client();
$res = $client->get( 'http://alltheragefaces.com/api/search/' . $search );
$json = json_decode( $res->getBody() );
if ( $json && is_array( $json ) ) {
$img = $json[ array_rand( $json ) ];
return $img;
}
$app->abort( 200, 'Failed to fetch anything from the API :(' );
};
$app->get( '/', function ( \Silex\Application $app ) {
$img = $app['fetch_rage'];
return sprintf( '<img src="%s" alt="%s" />', $app->escape( $img->png ), $app->escape( $img->title ) );
} );
$app->post( '/', function ( \Silex\Application $app ) {
$img = $app['fetch_rage'];
// check for slack data
$token = $app['request']->get( 'token' );
if ( $token && isset( $app['webhooks'][ $token ] ) ) {
// check if user chat or channel
$channel = $app['request']->get( 'channel_name' ) === 'directmessage' ?
$app['request']->get( 'channel_id' ) :
'#' . $app['request']->get( 'channel_name' );
$payload = json_encode( [
'text' => '/rage ' . $app['request']->get( 'text' ),
'channel' => $channel,
'username' => $app['request']->get( 'user_name' ),
'icon_url' => 'http://cdn.alltheragefaces.com/img/faces/png/troll-troll-face.png',
'attachments' => [
[
'title' => $img->title,
'fallback' => $img->title,
'image_url' => $img->png,
]
]
] );
$client = new \GuzzleHttp\Client();
$promise = $client->postAsync( $app['webhooks'][ $token ], [ 'body' => $payload ] );
$promise->wait();
return '';
}
return sprintf( '<img src="%s" alt="%s" />', $app->escape( $img->png ), $app->escape( $img->title ) );;
} );
$app->error( function ( \Exception $e, $code ) {
return new Response( $e->getMessage() );
} );
$app->run();