Skip to content

Commit

Permalink
Fix slackapi#341 Add logger access to listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Feb 19, 2020
1 parent e0efcbf commit 5ff4e85
Show file tree
Hide file tree
Showing 21 changed files with 37 additions and 73 deletions.
5 changes: 1 addition & 4 deletions bolt-docker-examples/echo-command-app/src/main/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package example

import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import org.slf4j.LoggerFactory

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_BOT_TOKEN=xoxb-***
// export SLACK_SIGNING_SECRET=123abc***
val app = App()

app.command("/echo") { req, ctx ->
val text = "You said ${req.payload.text} at <#${req.payload.channelId}|${req.payload.channelName}>"
val res = ctx.respond { it.text(text) }
logger.info("respond result - {}", res)
ctx.logger.info("respond result - {}", res)
ctx.ack()
}

Expand Down
4 changes: 2 additions & 2 deletions bolt-kotlin-examples/src/main/kotlin/examples/app_home/app.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import java.time.ZonedDateTime
fun main() {

val addEventHandler = false
val logger = LoggerFactory.getLogger("main")

// export SLACK_BOT_TOKEN=xoxb-***
// export SLACK_SIGNING_SECRET=123abc***
Expand All @@ -20,6 +19,7 @@ fun main() {
val app = App(config)

if (addEventHandler) {
val logger = LoggerFactory.getLogger("main")
val handler: AppHomeOpenedHandler = object : AppHomeOpenedHandler() {
override fun handle(payload: AppHomeOpenedPayload?) {
logger.info("AppHomeOpenedHandler - $payload")
Expand All @@ -35,7 +35,7 @@ fun main() {
.viewAsString(view())
.hash(e.event.view?.hash)
}
logger.info("event - $e / res - $res")
ctx.logger.info("event - $e / res - $res")
ctx.ack()
}
val server = SlackAppServer(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import com.slack.api.app_backend.slash_commands.response.SlashCommandResponse
import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import com.slack.api.bolt.util.JsonOps
import org.slf4j.LoggerFactory
import util.ResourceLoader

fun main() {
val logger = LoggerFactory.getLogger("main")

val config = ResourceLoader.loadAppConfig()
val app = App(config)
Expand All @@ -19,7 +17,7 @@ fun main() {
ctx.ack()
}
app.attachmentAction("wopr_game") { req, ctx ->
logger.info("attachment action - {}, {}", req.payload, ctx)
ctx.logger.info("attachment action - {}, {}", req.payload, ctx)
ctx.respond(JsonOps.fromJson(secondMessage, ActionResponse::class.java))
ctx.ack()
}
Expand Down
5 changes: 1 addition & 4 deletions bolt-kotlin-examples/src/main/kotlin/examples/echo/app.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package examples.echo

import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import org.slf4j.LoggerFactory

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_BOT_TOKEN=xoxb-***
// export SLACK_SIGNING_SECRET=123abc***
val app = App()

app.command("/echo") { req, ctx ->
val text = "You said ${req.payload.text} at <#${req.payload.channelId}|${req.payload.channelName}>"
val res = ctx.respond { it.text(text) }
logger.info("respond result - {}", res)
ctx.logger.info("respond result - {}", res)
ctx.ack()
}
val server = SlackAppServer(app)
Expand Down
5 changes: 1 addition & 4 deletions bolt-kotlin-examples/src/main/kotlin/examples/ecs/app.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ package examples.ecs
import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import com.slack.api.bolt.response.Response
import org.slf4j.LoggerFactory

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_BOT_TOKEN=xoxb-***
// export SLACK_SIGNING_SECRET=123abc***
val app = App()

app.command("/echo") { req, ctx ->
val text = "You said ${req.payload.text} at <#${req.payload.channelId}|${req.payload.channelName}>"
val res = ctx.respond { it.text(text) }
logger.info("respond result - {}", res)
ctx.logger.info("respond result - {}", res)
ctx.ack()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import com.slack.api.bolt.response.Response
import com.slack.api.model.block.composition.PlainTextObject
import org.slf4j.LoggerFactory
import util.ResourceLoader

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_BOT_TOKEN=xoxb-***
// export SLACK_SIGNING_SECRET=123abc***
//val app = App()
val app = App(ResourceLoader.loadAppConfig("appConfig_MeetingArrangement.json"))

app.use { req, _, chain ->
val logger = req.context.logger
logger.info("Request - $req")
val resp = chain.next(req)
logger.info("Response - $resp")
Expand All @@ -26,7 +24,7 @@ fun main() {

app.command("/meeting") { _, ctx ->
val res = ctx.client().viewsOpen { it.triggerId(ctx.triggerId).viewAsString(view) }
logger.info("views.open result - {}", res)
ctx.logger.info("views.open result - {}", res)
if (res.isOk) ctx.ack()
else Response.builder().statusCode(500).body(res.error).build()
}
Expand All @@ -52,7 +50,7 @@ fun main() {
ctx.ack { it.responseAction("errors").errors(errors) }
} else {
// Operate something with the data
logger.info("state: $stateValues private_metadata: ${req.payload.view.privateMetadata}")
ctx.logger.info("state: $stateValues private_metadata: ${req.payload.view.privateMetadata}")
ctx.ack()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package examples.middleware

import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import org.slf4j.LoggerFactory

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_BOT_TOKEN=xoxb-***
// export SLACK_SIGNING_SECRET=123abc***
val app = App()

app.use { req, _, chain ->
val logger = req.context.logger
logger.info("Request - $req")
val resp = chain.next(req)
logger.info("Response - $resp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package examples.oauth_flow
import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import com.slack.api.bolt.response.Response
import org.slf4j.LoggerFactory
import util.ResourceLoader

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_SIGNING_SECRET=123abc***
// export SLACK_APP_CLIENT_ID=12345.12345
// export SLACK_APP_CLIENT_SECRET=123abc***
Expand All @@ -27,7 +24,7 @@ fun main() {
val p = req.payload
val text = "<@${p.userId}> said ${p.text} at <#${p.channelId}|${p.channelName}>"
val res = ctx.respond { it.text(text).responseType("in_channel") }
logger.info("respond result - {}", res)
ctx.logger.info("respond result - {}", res)
ctx.ack()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import com.slack.api.bolt.jetty.SlackAppServer
import com.slack.api.bolt.response.Response
import com.slack.api.bolt.service.builtin.AmazonS3InstallationService
import com.slack.api.bolt.service.builtin.AmazonS3OAuthStateService
import org.slf4j.LoggerFactory
import util.ResourceLoader

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_SIGNING_SECRET=123abc***
// export SLACK_APP_CLIENT_ID=12345.12345
// export SLACK_APP_CLIENT_SECRET=123abc***
Expand All @@ -36,7 +33,7 @@ fun main() {
val p = req.payload
val text = "<@${p.userId}> said ${p.text} at <#${p.channelId}|${p.channelName}>"
val res = ctx.respond { it.text(text).responseType("in_channel") }
logger.info("respond result - {}", res)
ctx.logger.info("respond result - {}", res)
ctx.ack()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package examples.oauth_flow_v2
import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
import com.slack.api.bolt.response.Response
import org.slf4j.LoggerFactory
import util.ResourceLoader

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_SIGNING_SECRET=123abc***
// export SLACK_APP_CLIENT_ID=12345.12345
// export SLACK_APP_CLIENT_SECRET=123abc***
Expand All @@ -28,7 +25,7 @@ fun main() {
val p = req.payload
val text = "<@${p.userId}> said ${p.text} at <#${p.channelId}|${p.channelName}>"
val res = ctx.respond { it.text(text).responseType("in_channel") }
logger.info("respond result - {}", res)
ctx.logger.info("respond result - {}", res)
ctx.ack()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import com.slack.api.bolt.jetty.SlackAppServer
import com.slack.api.bolt.response.Response
import com.slack.api.bolt.service.builtin.AmazonS3InstallationService
import com.slack.api.bolt.service.builtin.AmazonS3OAuthStateService
import org.slf4j.LoggerFactory
import util.ResourceLoader

fun main() {

val logger = LoggerFactory.getLogger("main")

// export SLACK_SIGNING_SECRET=123abc***
// export SLACK_APP_CLIENT_ID=12345.12345
// export SLACK_APP_CLIENT_SECRET=123abc***
Expand All @@ -37,7 +34,7 @@ fun main() {
val p = req.payload
val text = "<@${p.userId}> said ${p.text} at <#${p.channelId}|${p.channelName}>"
val res = ctx.respond { it.text(text).responseType("in_channel") }
logger.info("respond result - {}", res)
ctx.logger.info("respond result - {}", res)
ctx.ack()
}

Expand Down
5 changes: 5 additions & 0 deletions bolt/src/main/java/com/slack/api/bolt/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.google.gson.JsonElement;
import com.slack.api.Slack;
import com.slack.api.bolt.App;
import com.slack.api.bolt.response.Response;
import com.slack.api.bolt.util.JsonOps;
import com.slack.api.methods.MethodsClient;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -22,6 +25,8 @@ public abstract class Context {

protected Slack slack;

public final Logger logger = LoggerFactory.getLogger(App.class);

/**
* Organization ID for Enterprise Grid.
*/
Expand Down
4 changes: 1 addition & 3 deletions bolt/src/test/java/samples/AttachmentActionSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import com.slack.api.bolt.App;
import com.slack.api.bolt.AppConfig;
import com.slack.api.bolt.util.JsonOps;
import lombok.extern.slf4j.Slf4j;
import samples.util.ResourceLoader;
import samples.util.TestSlackAppServer;

@Slf4j
public class AttachmentActionSample {

public static void main(String[] args) throws Exception {
Expand All @@ -26,7 +24,7 @@ public static void main(String[] args) throws Exception {
// https://github.com/slackapi/java-slack-sdk/blob/master/bolt/src/test/resources/action_response/message1.json
String secondMessage = ResourceLoader.load("action_response/message1.json");
app.attachmentAction("wopr_game", (req, ctx) -> {
log.info("attachment action - {}", req.getPayload());
ctx.logger.info("attachment action - {}", req.getPayload());
ctx.respond(JsonOps.fromJson(secondMessage, ActionResponse.class));
return ctx.ack();
});
Expand Down
4 changes: 1 addition & 3 deletions bolt/src/test/java/samples/DialogSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.slack.api.bolt.App;
import com.slack.api.bolt.AppConfig;
import com.slack.api.methods.response.dialog.DialogOpenResponse;
import lombok.extern.slf4j.Slf4j;
import samples.util.ResourceLoader;
import samples.util.TestSlackAppServer;

Expand All @@ -14,7 +13,6 @@

import static java.util.stream.Collectors.toList;

@Slf4j
public class DialogSample {

static final List<Option> allOptions = Arrays.asList(
Expand All @@ -36,7 +34,7 @@ public static void main(String[] args) throws Exception {
.triggerId(req.getPayload().getTriggerId())
.dialogAsString(dialog)
);
log.info("dialog.open - {}", apiResponse);
ctx.logger.info("dialog.open - {}", apiResponse);
if (apiResponse.isOk()) {
return ctx.ack();
} else {
Expand Down
6 changes: 3 additions & 3 deletions bolt/src/test/java/samples/EventsSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public static void main(String[] args) throws Exception {
App app = new App(config);

app.event(MessageEvent.class, (event, ctx) -> {
log.info("new message by a user - {}", event);
ctx.logger.info("new message by a user - {}", event);
ctx.client().chatPostMessage(r -> r
.text("Hi there!")
.channel(event.getEvent().getChannel()));
return ctx.ack();
});
app.event(MessageBotEvent.class, (event, ctx) -> {
log.info("bot message - {}", event);
ctx.logger.info("bot message - {}", event);
return ctx.ack();
});
app.event(MessageDeletedEvent.class, (event, ctx) -> {
log.info("message deleted - {}", event);
ctx.logger.info("message deleted - {}", event);
return ctx.ack();
});

Expand Down
4 changes: 1 addition & 3 deletions bolt/src/test/java/samples/EventsSample_WatchingYou.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import com.slack.api.methods.response.reactions.ReactionsAddResponse;
import com.slack.api.model.event.MessageEvent;
import com.slack.api.model.event.ReactionAddedEvent;
import lombok.extern.slf4j.Slf4j;
import samples.util.ResourceLoader;
import samples.util.TestSlackAppServer;

@Slf4j
public class EventsSample_WatchingYou {

public static void main(String[] args) throws Exception {
Expand All @@ -20,7 +18,7 @@ public static void main(String[] args) throws Exception {
String channel = req.getEvent().getChannel();
String ts = req.getEvent().getTs();
ReactionsAddResponse res = ctx.client().reactionsAdd(r -> r.channel(channel).timestamp(ts).name("eyes"));
log.info("reactions.add - {}", res);
ctx.logger.info("reactions.add - {}", res);
return ctx.ack();
});

Expand Down
Loading

0 comments on commit 5ff4e85

Please sign in to comment.