Skip to content

Instantly share code, notes, and snippets.

@elektron9
Last active June 30, 2017 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elektron9/d0e7509b34acdbbf01db0b5d224cc440 to your computer and use it in GitHub Desktop.
Save elektron9/d0e7509b34acdbbf01db0b5d224cc440 to your computer and use it in GitHub Desktop.
RoboSlack Bookstore Message Example
import com.palantir.roboslack.api.MessageRequest;
import com.palantir.roboslack.api.attachments.Attachment;
import com.palantir.roboslack.api.attachments.components.Author;
import com.palantir.roboslack.api.attachments.components.Color;
import com.palantir.roboslack.api.attachments.components.Field;
import com.palantir.roboslack.api.attachments.components.Footer;
import com.palantir.roboslack.api.attachments.components.Title;
import com.palantir.roboslack.api.markdown.SlackMarkdown;
import com.palantir.roboslack.webhook.SlackWebHookService;
import com.palantir.roboslack.webhook.api.model.WebHookToken;
import com.palantir.roboslack.webhook.api.model.response.ResponseCode;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
/**
* @author abukhari
* @since v0.0.1
*/
public class SlackBookstoreExample {
public static void main(String[] args) {
WebHookToken token = WebHookToken.fromEnvironment();
MessageRequest msg2 = MessageRequest.builder()
.username("Brainy Bookstore Bot")
.iconEmoji(SlackMarkdown.EMOJI.decorate("books"))
.channel("slack-dev-msgs")
.text("Have you seen the newest addition to our literary wing? " + SlackMarkdown.EMOJI.decorate("new"))
.addAttachments(
Attachment.builder()
.fallback("book info display for 'The Further Adventures of Roboslackbot'")
.author(Author.of("K. Calstob"))
.imageUrl(url("http://i.imgur.com/GzAOSsZ.jpg"))
.color(Color.good())
.title(Title.builder()
.text("The Further Adventures of Roboslackbot")
.link(url("http://github.com/palantir/roboslack"))
.build()
)
.footer(Footer.builder()
.text("Generated By Roboslack")
.icon(url("https://platform.slack-edge.com/img/default_application_icon.png"))
.timestamp(LocalDateTime.now()
.atZone(ZoneId.systemDefault()).toEpochSecond())
.build())
.addFields(Field.builder().isShort(true).title("Science Fiction").value("").build())
.addFields(Field.builder().isShort(true).title("Horror").value("").build())
.addFields(Field.builder().isShort(true).title("Romance").value("").build())
.addFields(Field.builder().isShort(true).title("Robots").value("").build())
.build()
).build();
ResponseCode code = SlackWebHookService.with(token).sendMessage(msg2);
System.out.println(code);
}
private static URL url(String url) {
try {
return new URL(url);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e.getLocalizedMessage(), e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment