Last active
August 29, 2015 14:23
-
-
Save smaldini/cd92a8d6c271a12d3ace to your computer and use it in GitHub Desktop.
Gitter to Slack relay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpClient<Buffer, Buffer> client = NetStreams.httpClient(); | |
final String postUri = "https://hooks.slack.com/services/xxxxx"; | |
final NioEventLoopGroup group = new NioEventLoopGroup(12, | |
new NamedDaemonThreadFactory("reactor-tcp-io")); | |
client.get("https://stream.gitter.im/v1/rooms/xxxxxx/chatMessages", ch -> { | |
ch | |
.header("Authorization", "Bearer xxxxxx") | |
.header("Accept", "application/json"); | |
return Streams.never(); | |
}) | |
.flatMap(p -> p | |
.filter(m -> m.remaining() > 2) | |
.decode(new JsonCodec<>(Map.class)) | |
.log("after-consume") | |
.window(10, 1, TimeUnit.SECONDS) | |
.flatMap(msgs -> | |
NetStreams.httpClient(spec -> | |
spec.options(new NettyClientSocketOptions().eventLoopGroup(group)) | |
) | |
.post(postUri, ch -> | |
ch | |
.header("content-type", "application/x-www-form-urlencoded") | |
.writeWith( | |
msgs.map(msg -> "*"+((Map)msg.get("fromUser")).get("displayName")+"*"+" _at "+msg.get("sent")+"_ : "+msg.get("text").toString().replaceAll("\n","\\\\n")) | |
.reduce("", (prev, next) -> prev.isEmpty() ? next : (prev + "\\\\n" + next)) | |
.log("before-form") | |
.map(text -> { | |
try { | |
return "payload=" + URLEncoder | |
.encode("{ \"text\" : \"" + text + "\"" + " }", "UTF-8"); | |
} catch (UnsupportedEncodingException uee) { | |
uee.printStackTrace(); | |
return null; | |
} | |
}) | |
.log("new-message") | |
.map(new StringCodec()) | |
) | |
) | |
.stream() | |
.flatMap(Streams::after) // complete after all replies consumed and closed | |
) | |
.log("after-windows") | |
.after()) | |
.awaitSuccess(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment