From 2009aba6ec50ad5533817f21f7f45919d115af81 Mon Sep 17 00:00:00 2001 From: William Dunne Date: Mon, 3 Jul 2017 01:26:41 +0300 Subject: [PATCH] Enabling support for non-Spring launching, documentation --- README.md | 32 +++++++++++++++++++ .../me/ramswaroop/jbot/core/slack/Bot.java | 2 +- .../ramswaroop/jbot/core/slack/SlackDao.java | 16 +++++++--- .../jbot/core/slack/SlackService.java | 10 +++++- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 717a075f..e96404bc 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,38 @@ extends [Bot](/jbot-core/src/main/java/me/ramswaroop/jbot/core/slack/Bot.java) f bots as you want, just make the class extend [Bot](/jbot-core/src/main/java/me/ramswaroop/jbot/core/slack/Bot.java) class and it gets all the powers of a Slack Bot. +### Usage as a non-Boot application + +```java +public class SlackBot extends Bot { + private String slackToken; + + public SlackBot(String rtmUrl, String rtmToken) { + this.slackToken = rtmToken; + this.slackService = new SlackService(); + SlackDao slackDao = new SlackDao(); + slackDao.setRtmUrl(rtmUrl); + this.slackService.setSlackDao(slackDao); + } + + public String getSlackToken() { + return this.slackToken; + } + + public Bot getSlackBot() { + return this; + } +} + +public class Application { + public static void main(String[] args) { + SlackBot slackBot = new SlackBot(args[0], args[1]); + slackBot.startWebSocketConnection(); + //something to stop the application terminating + } +} +``` + ### Building a [Slack Integration](https://api.slack.com/) with JBot You can integrate your services into Slack by any of the following ways: diff --git a/jbot/src/main/java/me/ramswaroop/jbot/core/slack/Bot.java b/jbot/src/main/java/me/ramswaroop/jbot/core/slack/Bot.java index de3b8452..d7360e1e 100644 --- a/jbot/src/main/java/me/ramswaroop/jbot/core/slack/Bot.java +++ b/jbot/src/main/java/me/ramswaroop/jbot/core/slack/Bot.java @@ -394,7 +394,7 @@ private BotWebSocketHandler handler() { * and after which your bot becomes live. */ @PostConstruct - private void startWebSocketConnection() { + public void startWebSocketConnection() { slackService.startRTM(getSlackToken()); if (slackService.getWebSocketUrl() != null) { WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), handler(), slackService.getWebSocketUrl()); diff --git a/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackDao.java b/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackDao.java index 0e4f4029..64fc10d2 100644 --- a/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackDao.java +++ b/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackDao.java @@ -30,9 +30,6 @@ public class SlackDao { private static final Logger logger = LoggerFactory.getLogger(SlackDao.class); - /** - * Endpoint for RTM.start() - */ @Value("${rtmUrl}") private String rtmUrl; /** @@ -84,7 +81,7 @@ public RTM deserialize(JsonParser p, DeserializationContext ctxt) { httpMessageConverters.add(jsonConverter); restTemplate.setMessageConverters(httpMessageConverters); - ResponseEntity response = restTemplate.getForEntity(rtmUrl, RTM.class, slackToken); + ResponseEntity response = restTemplate.getForEntity(getRtmUrl(), RTM.class, slackToken); if (response.getBody() != null) { rtm.setWebSocketUrl(response.getBody().getWebSocketUrl()); rtm.setDmChannels(response.getBody().getDmChannels()); @@ -100,4 +97,15 @@ public RTM deserialize(JsonParser p, DeserializationContext ctxt) { return rtm; } + + /** + * Endpoint for RTM.start() + */ + public String getRtmUrl() { + return rtmUrl; + } + + public void setRtmUrl(String rtmUrl) { + this.rtmUrl = rtmUrl; + } } diff --git a/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackService.java b/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackService.java index 9431f9e1..e2e6bc9e 100644 --- a/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackService.java +++ b/jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackService.java @@ -30,7 +30,7 @@ public class SlackService { * @param slackToken */ public void startRTM(String slackToken) { - RTM rtm = slackDao.startRTM(slackToken); + RTM rtm = getSlackDao().startRTM(slackToken); currentUser = rtm.getUser(); dmChannels = rtm.getDmChannels(); webSocketUrl = rtm.getWebSocketUrl(); @@ -73,4 +73,12 @@ public String getWebSocketUrl() { public void setWebSocketUrl(String webSocketUrl) { this.webSocketUrl = webSocketUrl; } + + public SlackDao getSlackDao() { + return slackDao; + } + + public void setSlackDao(SlackDao slackDao) { + this.slackDao = slackDao; + } }