This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Add Java samples for slash commands. #14
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b57f6b
Add Java samples for slash commands.
tswast b8738ac
Fix circle.yml versions -> version.
tswast 2c26b21
Use latest version of shared-config parent.
tswast 5203a79
Change repo name to slack-samples.
tswast eb5602e
Fix repo name.
tswast d20b962
Use common parent for the whole repo.
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2016 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
machine: | ||
java: | ||
version: oraclejdk8 | ||
|
||
test: | ||
override: | ||
- cd java && mvn clean verify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
*.class | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
# IntelliJ | ||
.idea/ | ||
*.iml | ||
|
||
# Maven | ||
target/ | ||
pom.xml.tag | ||
pom.xml.releaseBackup | ||
pom.xml.versionsBackup | ||
pom.xml.next | ||
release.properties | ||
dependency-reduced-pom.xml | ||
buildNumber.properties | ||
.mvn/timing.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Running a slash command for Slack on Google Cloud Platform - Step 1 | ||
|
||
This is the solution directory for step one in the tutorial for running a [slash command for | ||
Slack](https://api.slack.com/slash-commands) on [Google Cloud Platform](https://cloud.google.com/). | ||
|
||
This tutorial uses [App Engine flexible environment][flexible] for more details on running an app | ||
on App Engine, follow the [quickstart tutorial][flexible-quickstart]. | ||
|
||
[flexible]: https://cloud.google.com/appengine/docs/flexible/java/ | ||
[flexible-quickstart]: https://cloud.google.com/appengine/docs/flexible/java/quickstart | ||
|
||
## Running locally | ||
|
||
$ mvn jetty:run | ||
|
||
## Deploying | ||
|
||
$ mvn appengine:deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<!-- | ||
Copyright 2016 Google Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>slack-command-1-start</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
|
||
<appengine.maven.plugin>1.0.0</appengine.maven.plugin> | ||
<jetty-maven-plugin-version>9.3.7.v20160115</jetty-maven-plugin-version> | ||
|
||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.30</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
|
||
<plugin> | ||
<groupId>com.google.cloud.tools</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.maven.plugin}</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-maven-plugin</artifactId> | ||
<version>${jetty-maven-plugin-version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
runtime: java | ||
vm: true | ||
|
||
handlers: | ||
- url: /.* | ||
script: this field is required, but ignored | ||
secure: always |
56 changes: 56 additions & 0 deletions
56
java/command/1-start/src/main/java/com/example/slashcommand/GreetServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.slashcommand; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@WebServlet(name = "greet", urlPatterns = "/send-greeting") | ||
@SuppressWarnings("serial") | ||
public class GreetServlet extends HttpServlet { | ||
|
||
private String getGreeting(String greetTarget) { | ||
if (greetTarget == null || greetTarget.isEmpty()) { | ||
greetTarget = "world"; | ||
} | ||
return String.format("Hello, %s.", greetTarget); | ||
} | ||
|
||
private void sendGreeting(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
resp.setContentType("text/plain"); | ||
String commandText = req.getParameter("text"); | ||
String greeting = getGreeting(commandText); | ||
resp.getWriter().write(greeting); | ||
} | ||
|
||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
sendGreeting(req, resp); | ||
} | ||
|
||
@Override | ||
public void doPost(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
sendGreeting(req, resp); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
java/command/1-start/src/main/java/com/example/slashcommand/IndexServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.slashcommand; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@WebServlet(name = "index", urlPatterns = "") | ||
@SuppressWarnings("serial") | ||
public class IndexServlet extends HttpServlet { | ||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
req.getRequestDispatcher("/index.jsp").forward(req, resp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>Slack Slash Command Sample on Google App Engine for Java</title> | ||
|
||
<!-- | ||
Copyright 2016 Google Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | ||
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> | ||
|
||
<p>Follow the | ||
<a href="https://github.com/GoogleCloudPlatform/slack-samples/tree/master/java/command">instructions | ||
for this sample</a> to configure and modify this sample. | ||
74 changes: 74 additions & 0 deletions
74
java/command/1-start/src/test/java/com/example/slashcommand/GreetServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2016 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.slashcommand; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Unit tests for {@link GreetServlet}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class GreetServletTest { | ||
@Mock private HttpServletRequest mockRequest; | ||
@Mock private HttpServletResponse mockResponse; | ||
private StringWriter responseWriter; | ||
private GreetServlet servletUnderTest; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
|
||
// Set up a fake HTTP response. | ||
responseWriter = new StringWriter(); | ||
when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); | ||
|
||
servletUnderTest = new GreetServlet(); | ||
} | ||
|
||
@Test | ||
public void doGet_noParameters_writesHelloWorld() throws Exception { | ||
servletUnderTest.doGet(mockRequest, mockResponse); | ||
assertThat(responseWriter.toString()).isEqualTo("Hello, world."); | ||
} | ||
|
||
@Test | ||
public void doGet_withName_writesHelloName() throws Exception { | ||
when(mockRequest.getParameter("text")).thenReturn("John Doe"); | ||
servletUnderTest.doGet(mockRequest, mockResponse); | ||
assertThat(responseWriter.toString()).isEqualTo("Hello, John Doe."); | ||
} | ||
|
||
@Test | ||
public void doPost_noParameters_writesHelloWorld() throws Exception { | ||
when(mockRequest.getParameter("text")).thenReturn("Jane Doe"); | ||
servletUnderTest.doPost(mockRequest, mockResponse); | ||
assertThat(responseWriter.toString()).isEqualTo("Hello, Jane Doe."); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newLine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newLine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.