This repository has been archived by the owner on Feb 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack-sql.php
executable file
·163 lines (145 loc) · 4.44 KB
/
slack-sql.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
<?php
// SQL query script
// Original base script at:
// https://github.com/PenguinSnail/mysql-slack
// Domain the database and scouting app are running on
// Must include the path to the scouting app scripts and a trailing slash
$domain = "http://domain.site/scouting/";
// URL to your Slack Incoming Webhook
$webhook = "https://hooks.slack.com/XXXXXX";
// Grab some of the values from the slash command, create vars for post back to Slack
$command = $_POST['command'];
$slackname = $_POST['user_name'];
$text = $_POST['text'];
$token = $_POST['token'];
// MySQL vars
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "scouting";
// Check the token and make sure the request is from our team
if($token != 'XXXXXXXXXX'){
$msg = "The token for the slash command doesn't match. Check your script.";
die($msg);
}
// Extract operation from passed text
$oper = strtok($text, " ");
$oper = strtolower($oper);
// Extract remaining arguments from passed text
$words = explode(' ', $text);
$words = array_slice($words, 1);
$args = implode(' ', $words);
// Determine operation and set query
if ($oper == "query") {
if (strtok($args, " ") == "drop") {
die("I'm afraid I can't let you do that...");
} else {
$query = $args;
}
} elseif ($oper == "match") {
$args = preg_replace("/[^0-9,.]/", "", strtok($args, " "));
$query = "select * from match_" . $args;
} elseif ($oper == "team") {
$args = preg_replace("/[^0-9,.]/", "", strtok($args, " "));
$query = "select * from team_" . $args;
} elseif ($oper == "notes") {
$args = preg_replace("/[^0-9,.]/", "", strtok($args, " "));
$query = "select * from notes_" . $args;
} elseif ($oper == "list") {
$args = strtok($args, " ");
if (strpos($args, "team") !== false) {
$query = "show tables like 'team\_%'";
} elseif (strpos($args, "match") !== false) {
$query = "show tables like 'match\_%'";
} elseif (strpos($args, "note") !==false) {
$query = "show tables like 'notes\_%'";
} else {
$query = "show tables";
}
} elseif ($oper == "average") {
$args = preg_replace("/[^0-9,.]/", "", strtok($args, " "));
$query = "select (select avg(autoSwitch) from team_" . $args . "), (select avg(autoScale) from team_" . $args . "), (select avg(teleSwitch) from team_" . $args . "), (select avg(teleScale) from team_" . $args . "), (select avg(returnCubes) from team_" . $args . ")";
} else {
die("List/Match/Team/Notes/Query - List tables/Match data/Team data/Team notes/Query database");
}
// Check connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Run query
if (!$result = $conn->query($query)) {
die("ERROR: " . $conn->error);
}
if ($result == "") {
die("No data found :(");
}
// create csv and output text contents
$i = 0;
$csvcont = "";
while($row = $result->fetch_assoc()) {
if ($i == 0) {
$i++;
foreach ($row as $key => $value) {
if ($oper == "average") {
$key = substr($key, 1, -1);
preg_match( '!\(([^\)]+)\)!', $key, $match);
$key = $match[1];
}
$csvcont .= $key;
$csvcont .= ",";
$output .= str_pad($key,11," ");
$output .= " | ";
}
}
$csvcont .= "\n";
$output .= "\n";
foreach ($row as $value) {
$csvcont .= $value;
$csvcont .= ",";
$output .= str_pad($value,15," ");
$output .= " | ";
}
}
//Write to csv file
if ($args == "" ) {
$file = $oper . ".csv";
} else {
$file = $oper . "_" . $args . ".csv";
}
$filewrite = "./csv/" . $file;
file_put_contents($filewrite, $csvcont);
// Build JSON from arrays
$data = array(
"username" => "Scouting Database",
"channel" => "@" . $slackname,
"text" => $output,
"attachments" => array(
array(
"fallback" => "HTML Table",
"color" => "#0066ff",
"title" => "Webpage Table",
"title_link" => $domain . "html-table.php?oper=" . $oper . "&args=" . $args
), array(
"fallback" => "CSV Table",
"color" => "#36a64f",
"title" => "CSV File",
"title_link" => $domain . "csv/" . $file
)
)
);
$payload = json_encode($data);
// Issue cURL command
$slack_call = curl_init($webhook);
curl_setopt($slack_call, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($slack_call, CURLOPT_POSTFIELDS, $payload);
curl_setopt($slack_call, CURLOPT_CRLF, true);
curl_setopt($slack_call, CURLOPT_RETURNTRANSFER, true);
curl_setopt($slack_call, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($payload))
);
curl_exec($slack_call);
curl_close($slack_call);
die();
?>