-
Notifications
You must be signed in to change notification settings - Fork 4
/
shooker.php
164 lines (122 loc) · 4.41 KB
/
shooker.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*------------------------------------------------------------------------
* shooker.php
* Created by: John Wenzler ([email protected])
* Available under the MIT License
* FOR MORE INFO AND EXAMPLES, VISIT:
* https://github.com/jwenzler/Shooker
*
------------------------------------------------------------------------*/
class Shooker {
//No error by default
private $error = false;
private $errorMessages = array();
//Token to make sure the request is coming from Slack
private $token = null;
//Incoming Slack endpoint
private $incomingURL = null;
//Text to send back to Slack client
private $responseText = null;
//Trigger array
private $triggers;
function __constructNoToken() {
$this->triggers = new stdClass;
}
function setupIncoming($incomingURL) {
$this->incomingURL = $incomingURL;
}
function setupOutgoing($token) {
$this->token = $token;
}
function addTrigger($triggerWord) {
$trigger = new ShookerTrigger($triggerWord);
$this->triggers->{$triggerWord} = $trigger;
return $trigger;
}
function sendMessage($message, $username, $icon) {
$data = array(
'text' => $message
);
if (isset($username)) {
$data['username'] = $username;
}
if (isset($icon)) {
$data['icon_emoji'] = $icon;
}
$options = array(
'http' => array(
'header' => 'Content-type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($this->incomingURL, false, $context);
return $result;
}
//Calculate response text and send it back to the Slack client
function listen() {
//Retrieve the text inbound to the webhook
$inText = $_POST['text'];
//Retrieve the user name for the sent in message
$inUser = $_POST['user_name'];
//Retrieve the channel name message was sent in
$inChannel = $_POST['channel_name'];
//Get the first word sent in (should match trigger)
$firstWord = explode(" ",$inText);
$firstWord = $firstWord[0];
$inText = substr($inText, strlen($firstWord)+1);
if (isset($this->triggers->{$firstWord})) {
//Retrieve the trigger
$trigger = $this->triggers->{$firstWord};
//Make sure there is an attached action to the trigger word
if (sizeof($trigger->actions) > 0) {
//Loop through and complete any actions attached to trigger
foreach ($trigger->actions as $action) {
//Check for token, this is a requirement for security reasons
if (isset($this->token)) {
//Make sure we have a POSTed token and that the token matches the token we have supplied
if (isset($_POST['token']) && $_POST['token'] == $this->token) {
$this->responseText = $action($inText, $inUser, $inChannel);
} else {
$this->error = true;
array_push($this->errorMessages, "Mismatched tokens, please make sure the token you supplied matches the one setup with Slack webhook");
}
} else {
$this->error = true;
array_push($this->errorMessages, "Please set a token with the constructor or using setToken function");
}
}
} else {
$this->error = true;
array_push($this->errorMessages, "No action provided for the given trigger word: ".$firstWord);
}
$ret = new stdClass;
//If we are free from errors, return the response text we calculated
if (!$this->error) {
//Create a return for the Slack client
$ret->text = $this->responseText;
//Otherwise we have an error, return all applicable messages
} else {
//Add error info
$ret->text = "`Error(s): ".implode(", ",$this->errorMessages)."`";
}
//Convert to a JSON string
$ret = json_encode($ret);
echo $ret;
}
}
}
//class Trigger class
class ShookerTrigger {
private $triggerWord;
public $actions = array();
//Create a trigger with given triggerword
function __construct($triggerWord) {
$this->triggerWord = $triggerWord;
}
function addAction($fxn) {
array_push($this->actions, $fxn);
}
}
?>