Skip to content

Commit

Permalink
Enabling support for non-Spring launching, documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
William Dunne committed Jul 2, 2017
1 parent 782ffab commit 2009aba
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion jbot/src/main/java/me/ramswaroop/jbot/core/slack/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
16 changes: 12 additions & 4 deletions jbot/src/main/java/me/ramswaroop/jbot/core/slack/SlackDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down Expand Up @@ -84,7 +81,7 @@ public RTM deserialize(JsonParser p, DeserializationContext ctxt) {
httpMessageConverters.add(jsonConverter);
restTemplate.setMessageConverters(httpMessageConverters);

ResponseEntity<RTM> response = restTemplate.getForEntity(rtmUrl, RTM.class, slackToken);
ResponseEntity<RTM> response = restTemplate.getForEntity(getRtmUrl(), RTM.class, slackToken);
if (response.getBody() != null) {
rtm.setWebSocketUrl(response.getBody().getWebSocketUrl());
rtm.setDmChannels(response.getBody().getDmChannels());
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 2009aba

Please sign in to comment.