Skip to content

Instantly share code, notes, and snippets.

@elektron9
Last active March 16, 2018 06:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elektron9/d029c433e4014c7f3fffcd0d882cc20f to your computer and use it in GitHub Desktop.
Save elektron9/d029c433e4014c7f3fffcd0d882cc20f to your computer and use it in GitHub Desktop.
RoboSlack Map Message Example
import com.palantir.roboslack.api.MessageRequest;
import com.palantir.roboslack.api.ParseMode;
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.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 SlackMapExample {
public static void main(String[] args) {
WebHookToken token = WebHookToken.fromEnvironment();
MessageRequest msg = MessageRequest.builder()
.username("Elvis Sightings App")
.iconEmoji(SlackMarkdown.EMOJI.decorate("globe_with_meridians"))
.channel("slack-dev-msgs")
.text("Alert: Potential Elvis Sighting! " + SlackMarkdown.LINK.decorate(url("https://www.google.com")
, "[Directions]")
+ " " + SlackMarkdown.LINK.decorate(url("https://www.google.com"), "[Add to My Maps]") + " " +
SlackMarkdown.LINK.decorate(url("https://www.google.com"), "[Flag for Review]"))
.addAttachments(
Attachment.builder()
.fallback("Map of latest Elvis sighting")
.color(Color.of("#D9A22B"))
.title(Title.of("Musée Mécanique"))
.author(Author.of("Elvis Sightings Mapper"))
.imageUrl(url("http://i.imgur.com/DUT0aaX.png"))
.thumbUrl(url("https://www.palantir.com/build/images/about/phil-eng-hero.jpg"))
.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())
.build()
)
.parse(ParseMode.NONE)
.build();
ResponseCode code = SlackWebHookService.with(token).sendMessage(msg);
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