/
react.php
59 lines (44 loc) · 1.22 KB
/
react.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
<?php
/**
* Created by PhpStorm.
* User: bensoer
* Date: 11/09/15
* Time: 1:48 AM
*/
/**
* this file deals with reactions from incoming slack calls
*/
require_once('./Database.php');
require_once('./config.inc');
$post = file_get_contents('php://input');
$json;
try{
$json = json_decode($post);
}catch(Exception $e){
//die silently
}
$channel = $json->channel_name;
$triggerWord = $json->trigger_word;
$database = new Database();
$responses = $database->getResponsesToRequest($triggerWord);
$randomNumber = rand(0, count($responses)-1);
$randomResponse = $responses[$randomNumber];
//now call slack to send back the response;
$response["text"] = $randomResponse;
$json_data = json_encode($response);
$post = file_get_contents(SLACKINBOUNDHOOK,null,stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'user_agent' => 'notslackbot',
'method' => 'POST',
'header' => "Content-type: application/json\r\n".
"Connection: close\r\n" .
"Content-length: " . strlen($json_data) . "\r\n",
'content' => $json_data,
),
)));
if($post){
http_response_code(200);
}else{
http_response_code(500);
}