forked from rampatra/jbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackWebhooks.java
66 lines (58 loc) · 2.42 KB
/
SlackWebhooks.java
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
package example.jbot.slack;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import me.ramswaroop.jbot.core.slack.models.Attachment;
import me.ramswaroop.jbot.core.slack.models.RichMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
/**
* This is a Slack Webhook sample. Webhooks are nothing but POST calls to
* Slack with data relevant to your users. You can send the data
* in the POST call in either ways:
* 1) Send as a JSON string as the payload parameter in a POST request
* 2) Send as a JSON string as the body of a POST request
*
* @author ramswaroop
* @version 1.0.0, 21/06/2016
*/
@Component
public class SlackWebhooks {
private static final Logger logger = LoggerFactory.getLogger(SlackWebhooks.class);
/**
* The Url you get while configuring a new incoming webhook
* on Slack. You can setup a new incoming webhook
* <a href="https://my.slack.com/services/new/incoming-webhook/">here</a>.
*/
@Value("${slackIncomingWebhookUrl}")
private String slackIncomingWebhookUrl;
/**
* Make a POST call to the incoming webhook url.
*/
@PostConstruct
public void invokeSlackWebhook() {
RestTemplate restTemplate = new RestTemplate();
RichMessage richMessage = new RichMessage("Just to test Slack's incoming webhooks.");
// set attachments
Attachment[] attachments = new Attachment[1];
attachments[0] = new Attachment();
attachments[0].setText("Some data relevant to your users.");
richMessage.setAttachments(attachments);
// For debugging purpose only
try {
logger.debug("Reply (RichMessage): {}", new ObjectMapper().writeValueAsString(richMessage));
} catch (JsonProcessingException e) {
logger.debug("Error parsing RichMessage: ", e);
}
// Always remember to send the encoded message to Slack
try {
restTemplate.postForEntity(slackIncomingWebhookUrl, richMessage.encodedMessage(), String.class);
} catch (RestClientException e) {
logger.error("Error posting to Slack Incoming Webhook: ", e);
}
}
}