I'm configuring a Spring Boot project to use this library, but it appears I've somehow configured it to start two listeners, instead of one. Debugging, I can only see one call made to SlackSession.addMessagePostedListener, but I'm having difficulty understanding what is happening after that. Any help is appreciated!
I use Spring Boot's CommandLineRunner to fetch a session, and add a listener on startup:
@Component
public class Startup implements CommandLineRunner{
private final Logger log = LoggerFactory.getLogger(Startup.class);
@Value("${slack.api.key}")
public String apiKey;
@Autowired
SlackSession slackSession;
@Override
public void run(String... args) throws Exception {
log.info("Service started with key: " + apiKey);
slackSession.connect();
slackSession.addMessagePostedListener(new HelloListener());
}
}
@Configuration
public class SlackConfig {
private final Logger log = LoggerFactory.getLogger(SlackConfig.class);
@Value("${slack.channel}")
String channel;
@Value("${slack.api.key}")
String key;
SlackSession slackSession;
@Bean
SlackSession slackSession() throws IOException {
if (null == slackSession) {
slackSession = SlackSessionFactory.createWebSocketSlackSession(key);
slackSession.connect();
log.debug("New session created: {}", slackSession);
}
return slackSession;
}
@Bean
SlackChannel slackChannel() throws IOException {
return slackSession.findChannelByName(channel);
}
}
public class HelloListener implements SlackMessagePostedListener {
private final Logger log = LoggerFactory.getLogger(HelloListener.class);
@Override
public void onEvent(SlackMessagePosted event, SlackSession session) {
log.debug("Message Posted: {}", event.getMessageContent());
if (event.getMessageContent().trim().toUpperCase().contains("HELLO <@U0CSFUZHB>")){
session.sendMessage(event.getChannel(),"Hello " + event.getSender().getUserName(), null);
}
}
}
2015-12-06 22:50:38.004 INFO 8092 --- [ main] c.r.johnny5.Johnny5Application : Starting Johnny5Application on GINA with PID 8092 (C:\workspace\johnny5-spring\target\classes started by andrew in C:\workspace\johnny5-spring)
2015-12-06 22:50:38.006 DEBUG 8092 --- [ main] c.r.johnny5.Johnny5Application : Running with Spring Boot v1.3.0.M5, Spring v4.2.1.RELEASE
2015-12-06 22:50:38.037 INFO 8092 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3c87521: startup date [Sun Dec 06 22:50:38 CST 2015]; root of context hierarchy
2015-12-06 22:50:38.690 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : connecting to slack
2015-12-06 22:50:39.391 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : HTTP/1.1 200 OK
2015-12-06 22:50:39.397 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : parsing session status : ...
2015-12-06 22:50:39.419 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack bot found : B0CKUQW1E
...
2015-12-06 22:50:39.419 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack bot found : B0CKR0ACU
2015-12-06 22:50:39.419 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack public channel found : C0CHM9QUW
...
2015-12-06 22:50:39.420 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack public channel found : C0DGK1CJY
2015-12-06 22:50:39.420 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack im channel found : D0CSFUZHT
2015-12-06 22:50:39.420 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : 28 users found on this session
2015-12-06 22:50:39.420 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : 11 channels found on this session
2015-12-06 22:50:39.420 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : retrieved websocket URL : wss://ms370.slack-msgs.com/websocket/OoPaRslZtCXXkzyC26_6m_Q0Au6D-O0O53pX3PZ0e9ChYYWbGCeKfh1c6juxvCxk1B-LWj_Xvwjjuna7y-pgCCSnWbf1GAm0OCAcuXB_ZzmBonhkFKHRnwe0J_cbZPkBobiqdlKYSh9eu73UaYwvgw==
2015-12-06 22:50:39.426 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : initiating connection to websocket
2015-12-06 22:50:39.971 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : websocket connection established
2015-12-06 22:50:39.971 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : slack session ready
2015-12-06 22:50:39.971 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : starting connection monitoring
2015-12-06 22:50:39.972 DEBUG 8092 --- [ Thread-2] c.u.s.s.impl.SlackWebSocketSessionImpl : monitoring thread started
2015-12-06 22:50:39.972 DEBUG 8092 --- [ main] c.r.johnny5.config.SlackConfig : New session created: com.ullink.slack.simpleslackapi.impl.SlackWebSocketSessionImpl@3a71c100
2015-12-06 22:50:40.022 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"hello"}
2015-12-06 22:50:40.025 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"presence_change","user":"U0CSFUZHB","presence":"active"}
2015-12-06 22:50:40.025 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"presence_change","user":"U0CSFUZHB","presence":"active"}
2015-12-06 22:50:40.258 INFO 8092 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2015-12-06 22:50:40.263 INFO 8092 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2015-12-06 22:50:40.305 INFO 8092 --- [ main] c.recursivechaos.johnny5.config.Startup : Service started with key: xoxb-
2015-12-06 22:50:40.305 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : connecting to slack
2015-12-06 22:50:40.705 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : HTTP/1.1 200 OK
2015-12-06 22:50:40.707 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : parsing session status : ...
2015-12-06 22:50:40.714 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack user found : U0CFU8BDJ
...
2015-12-06 22:50:40.717 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack user found : USLACKBOT
...
2015-12-06 22:50:40.718 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack bot found : B0CKR0ACU
2015-12-06 22:50:40.718 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack public channel found : C0CHM9QUW
...
2015-12-06 22:50:40.719 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack public channel found : C0DGK1CJY
2015-12-06 22:50:40.719 DEBUG 8092 --- [ main] c.u.s.s.i.SlackJSONSessionStatusParser : slack im channel found : D0CSFUZHT
2015-12-06 22:50:40.719 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : 28 users found on this session
2015-12-06 22:50:40.719 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : 11 channels found on this session
2015-12-06 22:50:40.720 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : retrieved websocket URL : wss://ms370.slack-msgs.com/websocket/KQDQxCuie4e9dZ0clPY43HdLKOcknSDAUBI9YDpFGqYyR5H9IeF7WtjgP72nvSnWAi-iDzv30UfTpwSpUTcrHx5QBiC8dhicMuk0kLrjREagifqr7vh7Sezd18AAoPnP_UqM0-yhaWqr7GJWB2-G1Q==
2015-12-06 22:50:40.720 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : initiating connection to websocket
2015-12-06 22:50:41.091 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : websocket connection established
2015-12-06 22:50:41.091 INFO 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : slack session ready
2015-12-06 22:50:41.091 DEBUG 8092 --- [ main] c.u.s.s.impl.SlackWebSocketSessionImpl : starting connection monitoring
2015-12-06 22:50:41.091 DEBUG 8092 --- [ Thread-3] c.u.s.s.impl.SlackWebSocketSessionImpl : monitoring thread started
2015-12-06 22:50:41.093 INFO 8092 --- [ main] c.r.johnny5.Johnny5Application : Started Johnny5Application in 3.296 seconds (JVM running for 3.508)
2015-12-06 22:50:41.174 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"hello"}
2015-12-06 22:50:45.579 DEBUG 8092 --- [ Grizzly(2)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"user_typing","channel":"C0CSLGT0A","user":"U0CAJG99V"}
2015-12-06 22:50:45.579 DEBUG 8092 --- [ Grizzly(2)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"user_typing","channel":"C0CSLGT0A","user":"U0CAJG99V"}
2015-12-06 22:50:49.232 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"message","channel":"C0CSLGT0A","user":"U0CAJG99V","text":"hello <@U0CSFUZHB>","ts":"1449463848.000125","team":"T09C5HCG1"}
2015-12-06 22:50:49.232 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"message","channel":"C0CSLGT0A","user":"U0CAJG99V","text":"hello <@U0CSFUZHB>","ts":"1449463848.000125","team":"T09C5HCG1"}
2015-12-06 22:50:49.233 DEBUG 8092 --- [ Grizzly(1)] c.r.johnny5.listener.HelloListener : Message Posted: hello <@U0CSFUZHB>
2015-12-06 22:50:49.233 DEBUG 8092 --- [ Grizzly(1)] c.r.johnny5.listener.HelloListener : Message Posted: hello <@U0CSFUZHB>
2015-12-06 22:50:49.585 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : PostMessage return: {"ok":true,"channel":"C0CSLGT0A","ts":"1449463849.000127","message":{"type":"message","user":"U0CSFUZHB","text":"Hello doctorsound","ts":"1449463849.000127"}}
2015-12-06 22:50:49.586 DEBUG 8092 --- [ Grizzly(2)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"message","user":"U0CSFUZHB","text":"Hello doctorsound","channel":"C0CSLGT0A","ts":"1449463849.000126"}
2015-12-06 22:50:49.586 DEBUG 8092 --- [ Grizzly(2)] c.r.johnny5.listener.HelloListener : Message Posted: Hello doctorsound
2015-12-06 22:50:49.590 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"message","user":"U0CSFUZHB","text":"Hello doctorsound","channel":"C0CSLGT0A","ts":"1449463849.000127"}
2015-12-06 22:50:49.590 DEBUG 8092 --- [ Grizzly(1)] c.r.johnny5.listener.HelloListener : Message Posted: Hello doctorsound
2015-12-06 22:50:49.595 DEBUG 8092 --- [ Grizzly(1)] c.u.s.s.impl.SlackWebSocketSessionImpl : PostMessage return: {"ok":true,"channel":"C0CSLGT0A","ts":"1449463849.000126","message":{"type":"message","user":"U0CSFUZHB","text":"Hello doctorsound","ts":"1449463849.000126"}}
2015-12-06 22:50:49.595 DEBUG 8092 --- [ Grizzly(2)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"message","user":"U0CSFUZHB","text":"Hello doctorsound","channel":"C0CSLGT0A","ts":"1449463849.000126"}
2015-12-06 22:50:49.595 DEBUG 8092 --- [ Grizzly(2)] c.r.johnny5.listener.HelloListener : Message Posted: Hello doctorsound
2015-12-06 22:50:49.595 DEBUG 8092 --- [ Grizzly(2)] c.u.s.s.impl.SlackWebSocketSessionImpl : receiving from websocket {"type":"message","user":"U0CSFUZHB","text":"Hello doctorsound","channel":"C0CSLGT0A","ts":"1449463849.000127"}
2015-12-06 22:50:49.595 DEBUG 8092 --- [ Grizzly(2)] c.r.johnny5.listener.HelloListener : Message Posted: Hello doctorsound