gpt4 book ai didi

javascript - 使用 NodeJS 和 Express 接收 slack 模式提交时出现 404

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:59 25 4
gpt4 key购买 nike

我在使用 NodeJS 和 Express 接收 Slack 模式提交的内容时遇到问题。

这是我的松弛 View ,我可以通过交互式组件成功打开它

{
view: {
'type': 'modal',
'title': {
'type': 'plain_text',
'text': 'Example Modal 🚀'
},
'close': {
'type': 'plain_text',
'text': 'Cancel'
},
'callback_id': CALLBACK_IDS.MY_CALLBACK,
'blocks': [
{
'type': 'input',
'block_id': 'input_id_1',
'label': {
'type': 'plain_text',
'text': 'Example'
},
'element': {
'type': 'plain_text_input',
'action_id': 'input_id_1',
'placeholder': {
'type': 'plain_text',
'text': 'PLACEHOLDER'
},
'multiline': false
},
'optional': false
},
],
'submit': {
'type': 'plain_text',
'text': 'Save',
},
},
};

当用户提交 View 时,我收到一个有效负载

{
"type" => "view_submission",
"team" => {...
},
"user" => {...
},
"api_app_id" => "XXX",
"token" => "XXX",
"trigger_id" => "XXXX",
"view" => {
"id" => "XXX",
"team_id" => "XXX",
"type" => "modal",
"title" => {
"type" => "plain_text",
"text" => "Example Modal :rocket:",
"emoji" => true
},
"close" => {...},
"submit" => {...},
"blocks" => [...],
"private_metadata" => "",
"callback_id" => "my_callback_name",
"state" => {
"values" => {...},
"hash" => "XXX",
"clear_on_close" => false,
"notify_on_close" => false,
"root_view_id" => "XXX",
"previous_view_id" => nil,
"app_id" => "XXX",
"external_id" => "",
"bot_id" => "XXX"
}
}

我找不到使用 Node slack 库和来自 @slack/interactive-messages 的 createMessageAdapter 来拦截此消息的方法

我尝试过以下方法

const slackInteractions = createMessageAdapter(process.env.SLACK_SIGNING_SECRET);
slackInteractions
.action({ view: { callbackId: CALLBACK_IDS.MY_CALLBACK }}, myCallback)
.action({ type: 'view_submission' }, myCallback)

但是每次我的 Express 应用程序都会返回 404 Not Found

拦截 slack 模式提交的正确方法是什么?

我一直从this page中获得灵感以及 Slack API 文档中的各种其他来源

最佳答案

我设法让它工作。简而言之:

const express = require('express');
const http = require('http');
const {createEventAdapter, errorCodes} = require('@slack/events-api');
const bodyParser = require('body-parser');

const slackInteractions = createMessageAdapter(process.env.SLACK_SIGNING_SECRET);
const app = express();

slackInteractions.viewSubmission(CALLBACK_IDS.MY_CALLBACK, async (payload, respond) => {
return ''; //or some view update code
});

// all slack interactions here, before the body parser

app.use(bodyParser.json({
verify: (req, res, buf) => {
req.rawBody = buf
}
}));

app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));

// all post or get urls here, after the body parser declaration

const port = process.env.APP_SLACK_PORT || 3000;

http.createServer(app).listen(port, () => {
console.log(`server listening on port ${port}`);
});

返回空字符串将关闭窗口。如果您需要在表单提交后更新 View ,您可以在处理程序中返回更新后的 View 。

关于javascript - 使用 NodeJS 和 Express 接收 slack 模式提交时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58238583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com