Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling support for non-Spring launching, documentation #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}