gpt4 book ai didi

javascript - 如何防止 Google Apps 脚本在处​​理自定义斜杠命令后将 HTML 返回到 slack?

转载 作者:行者123 更新时间:2023-11-30 15:52:50 24 4
gpt4 key购买 nike

我在 Slack 中使用自定义斜杠命令通过 Google Apps 脚本将数据添加到 Google 文档,但是,当脚本完成运行(成功)时,Google 脚本将其发送到 Slack,用户会将其解释为错误:

<DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body><div><img src="//ssl.gstatic.com/docs/script/images/logo.png"></div><center>The script completed but did not return anything.</center></body></html>

如何防止将此消息发送到 Slack?

function doPost(request) {
var sheets = SpreadsheetApp.openById('id-would-go-here');
var params = request.parameters;

var nR = getNextRow(sheets) + 1;

if (params.token == "token-would-go-here") {

// FALL BACK TO DEFAULT TEXT IF NO ORDER PROVIDED
var order = params.text || "No Order Entered";
var employee = params.user_name || "Name Error";

// RECORD TIMESTAMP AND USER NAME IN SPREADSHEET
sheets.getRangeByName('date').getCell(nR,1).setValue(new Date());
sheets.getRangeByName('employee').getCell(nR,1).setValue(employee);

// RECORD UPDATE INFORMATION INTO SPREADSHEET
sheets.getRangeByName('order').getCell(nR,1).setValue(order);

var channel = "lunch-orders";

postResponse(channel,order,employee);

var eph_url = params.response_url;
var eph_response = UrlFetchApp.fetch(eph_url,200);

} else {
return;
}
}

function postResponse(channel, order, employee) {

var payload = {
"channel": "#lunch-orders",
"username": "Lunch Order",
"link_names": 1,
"attachments":[
{
"fallback": "A lunch order was placed, but the display here is a little messed up. Check the Google Doc to view the order.",
"mrkdwn_in": ["pretext"],
"color": "good",
"fields":[
{
"title":"" + employee + "'s Lunch Order",
"value": "" + order + "",
"short":false
}
]
}
]
};

var url = 'incoming-webhook-url-goes-here';
var options = {
'method': 'post',
'payload': JSON.stringify(payload)
};

var response = UrlFetchApp.fetch(url,options);
}

最佳答案

您的设置有点偏差。您不 POST 返回数据,而是使用 ContentService 返回数据。现在一个小问题是您必须将 webapp 作为匿名身份验证运行。我所做的是将 slack token 保存在脚本属性服务中,并确保发出请求的 Slack 应用程序的 token 匹配。

这是一个示例网络钩子(Hook),它只是将传递给 switch 命令的参数写入电子表格。

function doPost(e) {
var returnMessage;
var slackToken = PropertiesService.getScriptProperties().getProperty("SlackToken") ;
var postData = parseParams(e.postData.getDataAsString());
if(postData.token === slackToken){
SpreadsheetApp.openById('.....').getSheetByName("Log").appendRow([postData]);
returnMessage = "Your record has been logged."
}else{
returnMessage = "Invalid Token"
}

return ContentService.createTextOutput(JSON.stringify({text:returnMessage})).setMimeType(ContentService.MimeType.JSON);
}

function parseParams(postData){
var postObj = {}
postData.split("&").map(function(param){var thisParam = param.split("="); postObj[thisParam[0]] = thisParam[1]});
return postObj;
}

关于javascript - 如何防止 Google Apps 脚本在处​​理自定义斜杠命令后将 HTML 返回到 slack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39045708/

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