Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package in.ashwanthkumar.slack.webhook.service;
 
import static in.ashwanthkumar.utils.lang.StringUtils.isNotEmpty;
import static in.ashwanthkumar.utils.lang.StringUtils.startsWith;
public class SlackService {
    private final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    public void push(String webHookUrlSlackMessage textString usernameString imageOrIconString destinationList<SlackAttachmentattachmentsthrows IOException {
        Map<StringObjectpayload = new HashMap<StringObject>();
        if (isNotEmpty(username)) {
            payload.put("username"username);
        }
        if (startsWith(imageOrIcon"http")) {
            payload.put("icon_url"imageOrIcon);
        } else if (isNotEmpty(imageOrIcon)) {
            payload.put("icon_emoji"imageOrIcon);
        }
        if (isNotEmpty(destination)) {
            payload.put("channel"destination);
        }
        if (!attachments.isEmpty()) {
            payload.put("attachments"attachments);
        }
        payload.put("text"text.toString());
        execute(webHookUrlpayload);
    }
    public void push(String webHookUrlSlackMessage textString usernameString imageOrIconString destinationthrows IOException {
        push(webHookUrltextusernameimageOrIcondestinationnew ArrayList<SlackAttachment>());
    }
    public void execute(String webHookUrlMap<StringObjectpayloadthrows IOException {
        String jsonEncodedMessage = new Gson().toJson(payload);
        HashMap<ObjectObjectpayloadToSend = Maps.newHashMap();
        payloadToSend.put("payload"jsonEncodedMessage);
        .buildPostRequest(new GenericUrl(webHookUrl), new UrlEncodedContent(payloadToSend))
                .execute();
    }
New to GrepCode? Check out our FAQ X