11package com .hubspot .slack .server .example ;
22
3- import com .hubspot .horizon .shaded .org .jboss .netty .buffer .ChannelBuffer ;
4- import com .hubspot .horizon .shaded .org .jboss .netty .channel .Channel ;
5- import com .hubspot .horizon .shaded .org .jboss .netty .channel .ChannelHandlerContext ;
6- import com .hubspot .horizon .shaded .org .jboss .netty .channel .ExceptionEvent ;
7- import com .hubspot .horizon .shaded .org .jboss .netty .channel .MessageEvent ;
8- import com .hubspot .horizon .shaded .org .jboss .netty .channel .SimpleChannelHandler ;
9- import com .hubspot .horizon .shaded .org .jboss .netty .handler .codec .http .HttpRequest ;
103import com .hubspot .slack .client .SlackClient ;
114import com .hubspot .slack .client .examples .BasicRuntimeConfig ;
125import com .hubspot .slack .client .jackson .ObjectMapperUtils ;
2013import com .hubspot .slack .client .models .interaction .SlackInteractiveCallback ;
2114import com .hubspot .slack .client .models .response .views .ModalViewCommandResponse ;
2215import com .hubspot .slack .client .models .views .ModalViewPayload ;
16+ import io .netty .buffer .ByteBuf ;
17+ import io .netty .channel .ChannelHandlerContext ;
18+ import io .netty .channel .SimpleChannelInboundHandler ;
19+ import io .netty .handler .codec .http .HttpContent ;
20+ import io .netty .handler .codec .http .HttpObject ;
2321import java .io .IOException ;
2422import java .net .URLDecoder ;
2523import java .nio .charset .StandardCharsets ;
26- import java .util .Arrays ;
24+ import java .util .Collections ;
2725import org .slf4j .Logger ;
2826import org .slf4j .LoggerFactory ;
2927
30- public class SlackMessageHandler extends SimpleChannelHandler {
28+ public class SlackMessageHandler extends SimpleChannelInboundHandler < HttpObject > {
3129
3230 private static final Logger LOG = LoggerFactory .getLogger (SlackMessageHandler .class );
3331
@@ -38,36 +36,42 @@ public SlackMessageHandler() {
3836 }
3937
4038 @ Override
41- public void messageReceived (ChannelHandlerContext ctx , MessageEvent e ) {
42- HttpRequest request = (HttpRequest ) e .getMessage ();
43- ChannelBuffer content = request .getContent ();
44- try {
45- String jsonContent = URLDecoder
46- .decode (content .toString (StandardCharsets .UTF_8 ), "utf-8" )
47- .substring (8 );
48- SlackInteractiveCallback callback = ObjectMapperUtils
49- .mapper ()
50- .readValue (jsonContent , SlackInteractiveCallback .class );
51- LOG .info ("Received raw JSON: {}" , jsonContent );
52- LOG .info ("Deserialized Callback: {}" , callback );
53- if (callback instanceof BlockActions ) {
54- sendResponse ((BlockActions ) callback );
39+ protected void channelRead0 (
40+ ChannelHandlerContext channelHandlerContext ,
41+ HttpObject msg
42+ ) throws Exception {
43+ if (msg instanceof HttpContent ) {
44+ HttpContent httpContent = (HttpContent ) msg ;
45+ ByteBuf content = httpContent .content ();
46+
47+ try {
48+ String jsonContent = URLDecoder
49+ .decode (content .toString (StandardCharsets .UTF_8 ), StandardCharsets .UTF_8 )
50+ .substring (8 );
51+ SlackInteractiveCallback callback = ObjectMapperUtils
52+ .mapper ()
53+ .readValue (jsonContent , SlackInteractiveCallback .class );
54+ LOG .info ("Received raw JSON: {}" , jsonContent );
55+ LOG .info ("Deserialized Callback: {}" , callback );
56+ if (callback instanceof BlockActions ) {
57+ sendResponse ((BlockActions ) callback );
58+ }
59+ } catch (IOException ex ) {
60+ LOG .error ("Could not decode message" , ex );
5561 }
56- } catch (IOException ex ) {
57- LOG .error ("Could not decode message" , ex );
5862 }
5963 }
6064
6165 @ Override
62- public void exceptionCaught (ChannelHandlerContext ctx , ExceptionEvent e ) {
63- LOG . error ( "Got an error" , e . getCause ());
64- Channel ch = e . getChannel ( );
65- ch .close ();
66+ public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause )
67+ throws Exception {
68+ LOG . error ( "Got an error" , cause . getCause () );
69+ ctx .close ();
6670 }
6771
6872 private void sendResponse (BlockActions blockActions ) {
6973 for (BlockElementAction action : blockActions .getElementActions ()) {
70- LOG .info ("You interaced with: {}" , action );
74+ LOG .info ("You interacted with: {}" , action );
7175 }
7276
7377 ModalViewCommandResponse response = slackClient
@@ -76,7 +80,7 @@ private void sendResponse(BlockActions blockActions) {
7680 blockActions .getTriggerId (),
7781 ModalViewPayload .of (
7882 Text .of (TextType .PLAIN_TEXT , "Hi " + blockActions .getUser ().getUsername ()),
79- Arrays . asList (
83+ Collections . singletonList (
8084 Section
8185 .of (Text .of (TextType .MARKDOWN , "Thanks for clicking on _something_!" ))
8286 .withAccessory (DatePicker .of ("my-date-picker" ))
0 commit comments