我正在构建一个 Slack 机器人应用程序。我希望每次用户打开与机器人的对话时,机器人都会自动说“你好!请问我一个问题”。我想我应该使用事件 API:https ://api.slack.com/events来监听用户操作,但我应该捕获哪个事件?
问问题
21 次
1 回答
0
使用 AppHomeOpenedEvent 类。
当您在 Slack 的应用程序部分中单击您的机器人或应用程序时。您单击主页选项卡的那一刻,下面的代码在起作用
以下是 Slack Bolt SDK Java 中的示例代码
app.event(AppHomeOpenedEvent.class, (req, ctx) -> {
var logger = ctx.logger;
var userId = req.getEvent().getUser();
try {
// Call the conversations.create method using the built-in WebClient
var modalView = view(v -> v
.type("home")
.blocks(asBlocks(
section(s -> s.text(markdownText(mt ->
mt.text("*Welcome home, <@" + userId + "> :house:*")))),
section(s -> s.text(markdownText(mt ->
mt.text("About the simplest modal you could conceive of :smile:\\n\\nMaybe <https://api.slack.com/reference/block-kit/interactive-components|*make the modal interactive*> or <https://api.slack.com/surfaces/modals/using#modifying|*learn more advanced modal use cases*>.")))),
divider(),
context(c -> c.elements(asContextElements(
markdownText("Psssst this modal was designed using <https://api.slack.com/tools/block-kit-builder|*Block Kit Builder*>")
)))
))
);
var result = ctx.client().viewsPublish(r -> r
// The token you used to initialize your app
.token(AppConfigLoader.load().getSingleTeamBotToken())
.userId(userId)
.view(modalView)
);
// Print result
logger.info("result: {}", result);
} catch (IOException | SlackApiException e) {
logger.error("error: {}", e.getMessage(), e);
}
return ctx.ack();
});
于 2022-02-23T05:57:42.947 回答