Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package in.ashwanthkumar.slack.webhook;
  
  
  import java.util.List;
  
  import static in.ashwanthkumar.utils.lang.StringUtils.isEmpty;


Slack provides programmatic access to Slack web hooks
 
 public class Slack {
     private String webhookUrl;
     private String channel;
     private String user;
     private String icon;
     private SlackService slackService = new SlackService();
 
     public Slack(String webhookUrl) {
         if (isEmpty(webhookUrl)) {
             throw new IllegalArgumentException("Webhook url is not provided");
         } else if (!webhookUrl.startsWith("https://hooks.slack.com/services/")) {
             throw new IllegalArgumentException("Slack Webhook url starts with https://hooks.slack.com/services/");
         }
         this. = webhookUrl;
     }

    
Used for tests
 
     Slack(String webhookUrlSlackService mockService) {
         this. = webhookUrl;
          = mockService;
     }

    
Send the message to a particular channel

Parameters:
channel Channel to send
 
     public Slack sendToChannel(String channel) {
         this. = "#" + channel;
         return this;
     }

    
Send the message to a particular user

Parameters:
sendToUser User to send
 
     public Slack sendToUser(String sendToUser) {
         this. = "@" + sendToUser;
         return this;
     }

    
Change the display name

Parameters:
user Display name
 
     public Slack displayName(String user) {
         this. = user;
         return this;
     }

    
Change the bot icon

Parameters:
imageOrIcon Icon Image URL or emoji code from http://www.emoji-cheat-sheet.com/
 
     public Slack icon(String imageOrIcon) {
         this. = imageOrIcon;
         return this;
     }

    
Publishes messages to Slack Webhook

Parameters:
message Message to send
Throws:
java.io.IOException
 
     public void push(SlackMessage messagethrows IOException {
         if (message != null) {
             .push(message);
         }
     }

    
Publish message as SlackAttachment

Parameters:
attachment SlackAttachment to send
Throws:
java.io.IOException
 
     public void push(SlackAttachment attachmentthrows IOException {
         if (attachment != null) {
             .push(new SlackMessage(), , Lists.of(attachment));
        }
    }

    
Publish message as SlackAttachment

Parameters:
attachments SlackAttachment to send
Throws:
java.io.IOException
    public void push(List<SlackAttachmentattachmentsthrows IOException {
        .push(new SlackMessage(), attachments);
    }
New to GrepCode? Check out our FAQ X