执行以下代码时,我可以缝制以使request.write()
工作的唯一方法是手动转义引号。我试图在传递参数&之前先JSON.stringify()
,但仍然无法正常工作。
关于这里发生的事情以及如何解决它的任何想法?
我尝试了JSON.stringify(message)
以及许多其他方法。我也打算通过香草https.request
来做到这一点。
function samplePost(responseURL){
let postOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
let request = https.request(responseURL, postOptions, (res) => {
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on("end",() => {
//console.log(rawData);
});
});
//can't seem to send this along properly unless I escape all quotations
//example "{ \"text\": \"Testing this message out once again...\" }"
let message = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Are you sure you want to invite to :video_game:?\n*" + email + "* on *" + platform + "*"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "Invite"
},
"value": "invite"
},
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "No"
},
"value": "dont_invite"
}
]
}
];
request.write(message);
request.end();
}
上面的代码与slack集成。
我希望发送request.write(message)
时能输出松弛信息。
相反,我留下的是一个空白的答复。好像什么都没出错,但是也没有任何数据发送。
答案 0 :(得分:0)
对于我如何将退货消息格式化为松弛状态,这确实是一个简单的问题。
我需要确保首先将松弛块添加到以“ blocks”为关键的json对象中。其次,在发送JSON之前,我需要对其进行JSON.stringify。见下文。
function samplePost(responseURL){
let postOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
let request = https.request(responseURL, postOptions, (res) => {
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on("end", () => { });
});
let messageBlocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Are you sure you want to invite to :video_game:?\n*" + email + "* on *" + platform + "*"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "Invite"
},
"value": "invite"
},
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "No"
},
"value": "dont_invite"
}
]
}
];
//ADDED THIS
let returnMessage = {
"blocks": messageBlocks
}
request.write(JSON.stringify(messageBlocks));
request.end();
}