37
37
38
38
public class SlackSlashCommand implements HttpFunction {
39
39
40
+ // [START functions_slack_setup]
40
41
private Kgsearch kgClient ;
41
42
private static final String API_KEY = System .getenv ("KG_API_KEY" );
42
43
private static final String SLACK_SECRET = System .getenv ("SLACK_SECRET" );
@@ -50,8 +51,16 @@ public SlackSlashCommand() throws IOException, GeneralSecurityException {
50
51
51
52
verifier = new SlackSignature .Verifier (new SlackSignature .Generator (SLACK_SECRET ));
52
53
}
54
+ // [END functions_slack_setup]
53
55
54
- boolean isValidSlackWebhook (HttpRequest request , String requestBody ) throws IOException {
56
+ // [START functions_verify_webhook]
57
+ /**
58
+ * Verify that the webhook request came from Slack.
59
+ * @param request Cloud Function request object in {@link HttpRequest} format.
60
+ * @param requestBody Raw body of webhook request to check signature against.
61
+ * @return true if the provided request came from Slack, false otherwise
62
+ */
63
+ boolean isValidSlackWebhook (HttpRequest request , String requestBody ) {
55
64
56
65
// Check for headers
57
66
HashMap <String , List <String >> headers = new HashMap (request .getHeaders ());
@@ -65,14 +74,25 @@ boolean isValidSlackWebhook(HttpRequest request, String requestBody) throws IOEx
65
74
headers .get ("X-Slack-Signature" ).get (0 ),
66
75
1L );
67
76
}
77
+ // [END functions_verify_webhook]
68
78
79
+ // [START functions_slack_format]
80
+ /**
81
+ * Helper method to copy properties between {@link JsonObject}s
82
+ */
69
83
void addPropertyIfPresent (
70
84
JsonObject target , String targetName , JsonObject source , String sourceName ) {
71
85
if (source .has (sourceName )) {
72
86
target .addProperty (targetName , source .get (sourceName ).getAsString ());
73
87
}
74
88
}
75
89
90
+ /**
91
+ * Format the Knowledge Graph API response into a richly formatted Slack message.
92
+ * @param kgResponse The response from the Knowledge Graph API as a {@link JsonObject}.
93
+ * @param query The user's search query.
94
+ * @return The formatted Slack message as a JSON string.
95
+ */
76
96
String formatSlackMessage (JsonObject kgResponse , String query ) {
77
97
JsonObject attachmentJson = new JsonObject ();
78
98
JsonArray attachments = new JsonArray ();
@@ -119,15 +139,31 @@ String formatSlackMessage(JsonObject kgResponse, String query) {
119
139
120
140
return gson .toJson (responseJson );
121
141
}
122
-
142
+ // [END functions_slack_format]
143
+
144
+ // [START functions_slack_request]
145
+ /**
146
+ * Send the user's search query to the Knowledge Graph API.
147
+ * @param query The user's search query.
148
+ * @return The Knowledge graph API results as a {@link JsonObject}.
149
+ * @throws IOException if Knowledge Graph request fails
150
+ */
123
151
JsonObject searchKnowledgeGraph (String query ) throws IOException {
124
152
Kgsearch .Entities .Search kgRequest = kgClient .entities ().search ();
125
153
kgRequest .setQuery (query );
126
154
kgRequest .setKey (API_KEY );
127
155
128
156
return gson .fromJson (kgRequest .execute ().toString (), JsonObject .class );
129
157
}
130
-
158
+ // [END functions_slack_request]
159
+
160
+ // [START functions_slack_search]
161
+ /**
162
+ * Receive a Slash Command request from Slack.
9265
163
+ * @param request Cloud Function request object.
164
+ * @param response Cloud Function response object.
165
+ * @throws IOException if Knowledge Graph request fails
166
+ */
131
167
@ Override
132
168
public void service (HttpRequest request , HttpResponse response ) throws IOException {
133
169
@@ -157,7 +193,9 @@ public void service(HttpRequest request, HttpResponse response) throws IOExcepti
157
193
JsonObject kgResponse = searchKnowledgeGraph (query );
158
194
159
195
// Format response to Slack
196
+ // See https://api.slack.com/docs/message-formatting
160
197
BufferedWriter writer = response .getWriter ();
161
198
writer .write (formatSlackMessage (kgResponse , query ));
162
199
}
200
+ // [END functions_slack_search]
163
201
}
0 commit comments