Skip to content

Commit a9fcbc0

Browse files
committed
Initial working commit
0 parents  commit a9fcbc0

9 files changed

Lines changed: 476 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.iml
2+
.idea/
3+
4+
target/

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# slack-java-webhook
2+
Java Client to Slack's Webhook feature.
3+
4+
## Usage
5+
```java
6+
Slack slack = new Slack("https://hooks.slack.com/services/...");
7+
slack.icon(":smiling_imp:") // Anything from http://www.emoji-cheat-sheet.com/ should work here
8+
.sendToUser("slackbot")
9+
.displayName("slack-java-client")
10+
.push(new SlackMessage("Text from my Slack-Java-Client"));
11+
```

pom.xml

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>in.ashwanthkumar</groupId>
5+
<artifactId>slack-java-webhook</artifactId>
6+
<version>0.0.1</version>
7+
<name>Slack Webhook integration in Java</name>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<java.version>1.5</java.version>
11+
<gson.version>2.3.1</gson.version>
12+
</properties>
13+
14+
<developers>
15+
<developer>
16+
<email>ashwanthkumar@googlemail.com</email>
17+
<name>Ashwanth Kumar</name>
18+
<url>http://www.ashwanthkumar.in/</url>
19+
<id>ashwanthkumar</id>
20+
</developer>
21+
</developers>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.google.code.gson</groupId>
26+
<artifactId>gson</artifactId>
27+
<version>2.3.1</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.google.http-client</groupId>
31+
<artifactId>google-http-client-gson</artifactId>
32+
<version>1.19.0</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<version>4.10</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.hamcrest</groupId>
43+
<artifactId>hamcrest-all</artifactId>
44+
<version>1.1</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.mockito</groupId>
49+
<artifactId>mockito-all</artifactId>
50+
<version>1.10.19</version>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
56+
<distributionManagement>
57+
<snapshotRepository>
58+
<id>sonatype-snapshots</id>
59+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
60+
</snapshotRepository>
61+
<repository>
62+
<id>sonatype-deploy</id>
63+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
64+
</repository>
65+
</distributionManagement>
66+
67+
68+
<url>https://github.com/ashwanthkumar/slack-java-webhook</url>
69+
<licenses>
70+
<license>
71+
<name>Apache2</name>
72+
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
73+
</license>
74+
</licenses>
75+
<scm>
76+
<url>https://github.com/ashwanthkumar/slack-java-webhook</url>
77+
<connection>scm:git:git@github.com:ashwanthkumar/slack-java-webhook.git</connection>
78+
</scm>
79+
80+
<build>
81+
<plugins>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-compiler-plugin</artifactId>
85+
<version>3.1</version>
86+
<configuration>
87+
<source>${java.version}</source>
88+
<target>${java.version}</target>
89+
</configuration>
90+
</plugin>
91+
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-source-plugin</artifactId>
95+
<version>2.2.1</version>
96+
<executions>
97+
<execution>
98+
<id>attach-sources</id>
99+
<goals>
100+
<goal>jar</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
106+
<plugin>
107+
<groupId>org.apache.maven.plugins</groupId>
108+
<artifactId>maven-javadoc-plugin</artifactId>
109+
<version>2.9.1</version>
110+
<executions>
111+
<execution>
112+
<id>attach-javadocs</id>
113+
<goals>
114+
<goal>jar</goal>
115+
</goals>
116+
</execution>
117+
</executions>
118+
</plugin>
119+
</plugins>
120+
</build>
121+
122+
<!-- only run the signing steps when actually deploying to Central -->
123+
<!-- add -DperformRelease to the mvn deploy command -->
124+
<profiles>
125+
<profile>
126+
<id>release-sign-artifacts</id>
127+
<activation>
128+
<property>
129+
<name>performRelease</name>
130+
<value>true</value>
131+
</property>
132+
</activation>
133+
<build>
134+
<plugins>
135+
<plugin>
136+
<groupId>org.apache.maven.plugins</groupId>
137+
<artifactId>maven-gpg-plugin</artifactId>
138+
<version>1.4</version>
139+
<executions>
140+
<execution>
141+
<id>sign-artifacts</id>
142+
<phase>verify</phase>
143+
<goals>
144+
<goal>sign</goal>
145+
</goals>
146+
</execution>
147+
</executions>
148+
</plugin>
149+
</plugins>
150+
</build>
151+
</profile>
152+
</profiles>
153+
154+
155+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package in.ashwanthkumar.slack.webhook;
2+
3+
import java.io.IOException;
4+
5+
import static in.ashwanthkumar.slack.webhook.util.StringUtils.isEmpty;
6+
7+
/**
8+
* Slack provides programmatic access to Slack web hooks
9+
*/
10+
public class Slack {
11+
private String webhookUrl;
12+
private String channel;
13+
private String user;
14+
private String icon;
15+
private SlackService slackService = new SlackService();
16+
17+
public Slack(String webhookUrl) {
18+
if (isEmpty(webhookUrl)) {
19+
throw new IllegalArgumentException("Webhook url is not provided");
20+
} else if (!webhookUrl.startsWith("https://hooks.slack.com/services/")) {
21+
throw new IllegalArgumentException("Slack Webhook url starts with https://hooks.slack.com/services/");
22+
}
23+
this.webhookUrl = webhookUrl;
24+
}
25+
26+
/**
27+
* Used for tests
28+
*/
29+
Slack(String webhookUrl, SlackService mockService) {
30+
this.webhookUrl = webhookUrl;
31+
slackService = mockService;
32+
}
33+
34+
/**
35+
* Send the message to a particular channel
36+
*
37+
* @param channel Channel to send
38+
*/
39+
public Slack setChannel(String channel) {
40+
this.channel = "#" + channel;
41+
return this;
42+
}
43+
44+
/**
45+
* Send the message to a particular user
46+
*
47+
* @param sendToUser User to send
48+
*/
49+
public Slack sendToUser(String sendToUser) {
50+
this.channel = "@" + sendToUser;
51+
return this;
52+
}
53+
54+
/**
55+
* Change the display name
56+
*
57+
* @param user Display name
58+
*/
59+
public Slack displayName(String user) {
60+
this.user = user;
61+
return this;
62+
}
63+
64+
/**
65+
* Change the bot icon
66+
*
67+
* @param imageOrIcon Icon Image URL or emoji code from http://www.emoji-cheat-sheet.com/
68+
*/
69+
public Slack icon(String imageOrIcon) {
70+
this.icon = imageOrIcon;
71+
return this;
72+
}
73+
74+
/**
75+
* Publishes messages to Slack Webhook
76+
*
77+
* @param message Message to send
78+
* @throws IOException
79+
*/
80+
public void push(SlackMessage message) throws IOException {
81+
if (message != null) {
82+
slackService.push(webhookUrl, message, user, icon, channel);
83+
}
84+
}
85+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package in.ashwanthkumar.slack.webhook;
2+
3+
4+
import in.ashwanthkumar.slack.webhook.util.StringUtils;
5+
6+
/**
7+
* Wrapper to build rich text content in Slack
8+
*/
9+
public class SlackMessage {
10+
private StringBuilder textBuffer = new StringBuilder();
11+
12+
public SlackMessage() {
13+
}
14+
15+
public SlackMessage(String text) {
16+
text(text);
17+
}
18+
19+
public SlackMessage text(String text) {
20+
textBuffer.append(text);
21+
return this;
22+
}
23+
24+
public SlackMessage link(String url) {
25+
link(url, "");
26+
return this;
27+
}
28+
29+
public SlackMessage link(String url, String text) {
30+
if (StringUtils.isNotEmpty(text)) {
31+
textBuffer.append("<").append(url).append("|").append(text).append(">");
32+
} else {
33+
textBuffer.append("<").append(url).append(">");
34+
}
35+
36+
return this;
37+
}
38+
39+
public SlackMessage bold(String text) {
40+
textBuffer.append("*").append(text).append("*");
41+
return this;
42+
}
43+
44+
public SlackMessage italic(String text) {
45+
textBuffer.append("_").append(text).append("_");
46+
return this;
47+
}
48+
49+
public SlackMessage code(String code) {
50+
textBuffer.append("`").append(code).append("`");
51+
return this;
52+
}
53+
54+
public SlackMessage preformatted(String text) {
55+
textBuffer.append("```").append(text).append("```");
56+
return this;
57+
}
58+
59+
public SlackMessage quote(String text) {
60+
textBuffer.append("> ").append(text).append("\n");
61+
return this;
62+
}
63+
64+
public String toString() {
65+
return textBuffer.toString();
66+
}
67+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package in.ashwanthkumar.slack.webhook;
2+
3+
import com.google.api.client.http.GenericUrl;
4+
import com.google.api.client.http.HttpRequest;
5+
import com.google.api.client.http.HttpRequestFactory;
6+
import com.google.api.client.http.HttpTransport;
7+
import com.google.api.client.http.javanet.NetHttpTransport;
8+
import com.google.api.client.http.json.JsonHttpContent;
9+
import com.google.api.client.json.gson.GsonFactory;
10+
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static in.ashwanthkumar.slack.webhook.util.StringUtils.isNotEmpty;
16+
import static in.ashwanthkumar.slack.webhook.util.StringUtils.startsWith;
17+
18+
public class SlackService {
19+
private final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
20+
private final HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
21+
22+
public void push(String webHookUrl, SlackMessage text, String username, String imageOrIcon, String destination) throws IOException {
23+
Map<String, String> payload = new HashMap<String, String>();
24+
if (isNotEmpty(username)) {
25+
payload.put("username", username);
26+
}
27+
if (startsWith(imageOrIcon, "http")) {
28+
payload.put("icon_url", imageOrIcon);
29+
} else if (isNotEmpty(imageOrIcon)) {
30+
payload.put("icon_emoji", imageOrIcon);
31+
}
32+
if (isNotEmpty(destination)) {
33+
payload.put("channel", destination);
34+
}
35+
payload.put("text", text.toString());
36+
execute(webHookUrl, payload);
37+
}
38+
39+
public void execute(String webHookUrl, Map<String, String> payload) throws IOException {
40+
HttpRequest httpRequest = requestFactory.buildPostRequest(new GenericUrl(webHookUrl), new JsonHttpContent(new GsonFactory(), payload));
41+
httpRequest.execute();
42+
}
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package in.ashwanthkumar.slack.webhook.util;
2+
3+
public class StringUtils {
4+
public static boolean isNotEmpty(String text) {
5+
return (text != null) && !text.isEmpty();
6+
}
7+
8+
public static boolean isEmpty(String text) {
9+
return !isNotEmpty(text);
10+
}
11+
12+
public static boolean startsWith(String str, String prefix) {
13+
return isNotEmpty(str) && str.startsWith(prefix);
14+
}
15+
}

0 commit comments

Comments
 (0)