如何在使用bolt js提交slack中的视图后响应视图



Hi我实现了一个简单的快捷方式来从输入中获取数据,但在提交视图后,我无法获得响应负载,或者它无法更新视图以确认用户。下面是我的简单代码。

const { App } = require("@slack/bolt");
const app = new App({
token: process.env.BOT_TOKEN,
socketMode: true,
signingSecret: process.env.SIGNIGN_SECRET,
appToken: process.env.APP_TOKEN,
}); 
app.shortcut({ callback_id: 'open_modal', type: 'message_action' }, async ({ shortcut, ack, client }) => {
try {
await ack();
console.log("Ack:")

const result = await client.views.open({
trigger_id: shortcut.trigger_id,
callback_id: "new-task-modal",
view: {
type: "modal",
title: {
type: "plain_text",
text: "My App",
},
submit: {
type: "plain_text",
text: "Submit",
emoji: true,
},
close: {
type: "plain_text",
text: "Cancel",
},
blocks: [
{
"type": "input",
"element": {
"type": "plain_text_input",
"action_id": "plain_text_input-action"
},
"label": {
"type": "plain_text",
"text": "Label",
"emoji": true
}
}
],
},
});
} catch (error) {
console.log(error);
}
});
app.view({ callback_id: 'new-task-modal', type: 'message_action' }, async({ shortcut, ack, view, body }) => {
try {
console.log("New task modal",view.state.values );
await ack();

let result = await client.views.update({
view_id: body.view.id,
view: {
type: "modal",
title: {
type: "plain_text",
text: "My App",
emoji: true,
},
close: {
type: "plain_text",
text: "Cancel",
},
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: "This is a plain text section block.",
emoji: true,
},
},
],
}
});

} catch (error) {
console.log(error)
}
})

app.shortcut()函数可以很好地创建一个视图,但在提交该视图后,它无法访问app.view()函数来更新视图,因此有任何方法可以更新当前视图。

client.views.open调用中的callback_id应该在view字段下,而不是在根级别上。

const result = await client.views.open({
trigger_id: shortcut.trigger_id,
- callback_id: "new-task-modal",
view: {
+   callback_id: "new-task-modal",
type: "modal",
title: {
type: "plain_text",
text: "My App",
},
...

最新更新