8000 GCF Slack: add javadoc + region tags (#2486) · georg78sf/java-docs-samples@daa2ce4 · GitHub
[go: up one dir, main page]

Skip to content

Commit daa2ce4

Browse files
author
Ace Nassri
authored
GCF Slack: add javadoc + region tags (GoogleCloudPlatform#2486)
Forgot these in GoogleCloudPlatform#2394 - cc @averikitsch @grant as FYI.
1 parent fc484eb commit daa2ce4

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

functions/snippets/src/main/java/com/example/functions/SlackSlashCommand.java

Lines changed: 41 additions & 3 deletions
9265
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
public class SlackSlashCommand implements HttpFunction {
3939

40+
// [START functions_slack_setup]
4041
private Kgsearch kgClient;
4142
private static final String API_KEY = System.getenv("KG_API_KEY");
4243
private static final String SLACK_SECRET = System.getenv("SLACK_SECRET");
@@ -50,8 +51,16 @@ public SlackSlashCommand() throws IOException, GeneralSecurityException {
5051

5152
verifier = new SlackSignature.Verifier(new SlackSignature.Generator(SLACK_SECRET));
5253
}
54+
// [END functions_slack_setup]
5355

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) {
5564

5665
// Check for headers
5766
HashMap<String, List<String>> headers = new HashMap(request.getHeaders());
@@ -65,14 +74,25 @@ boolean isValidSlackWebhook(HttpRequest request, String requestBody) throws IOEx
6574
headers.get("X-Slack-Signature").get(0),
6675
1L);
6776
}
77+
// [END functions_verify_webhook]
6878

79+
// [START functions_slack_format]
80+
/**
81+
* Helper method to copy properties between {@link JsonObject}s
82+
*/
6983
void addPropertyIfPresent(
7084
JsonObject target, String targetName, JsonObject source, String sourceName) {
7185
if (source.has(sourceName)) {
7286
target.addProperty(targetName, source.get(sourceName).getAsString());
7387
}
7488
}
7589

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+
*/
7696
String formatSlackMessage(JsonObject kgResponse, String query) {
7797
JsonObject attachmentJson = new JsonObject();
7898
JsonArray attachments = new JsonArray();
@@ -119,15 +139,31 @@ String formatSlackMessage(JsonObject kgResponse, String query) {
119139

120140
return gson.toJson(responseJson);
121141
}
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+
*/
123151
JsonObject searchKnowledgeGraph(String query) throws IOException {
124152
Kgsearch.Entities.Search kgRequest = kgClient.entities().search();
125153
kgRequest.setQuery(query);
126154
kgRequest.setKey(API_KEY);
127155

128156
return gson.fromJson(kgRequest.execute().toString(), JsonObject.class);
129157
}
130-
158+
// [END functions_slack_request]
159+
160+
// [START functions_slack_search]
161+
/**
162+
* Receive a Slash Command request from Slack.
163+
* @param request Cloud Function request object.
164+
* @param response Cloud Function response object.
165+
* @throws IOException if Knowledge Graph request fails
166+
*/
131167
@Override
132168
public void service(HttpRequest request, HttpResponse response) throws IOException {
133169

@@ -157,7 +193,9 @@ public void service(HttpRequest request, HttpResponse response) throws IOExcepti
157193
JsonObject kgResponse = searchKnowledgeGraph(query);
158194

159195
// Format response to Slack
196+
// See https://api.slack.com/docs/message-formatting
160197
BufferedWriter writer = response.getWriter();
161198
writer.write(formatSlackMessage(kgResponse, query));
162199
}
200+
// [END functions_slack_search]
163201
}

0 commit comments

Comments
 (0)
0