1

我正在尝试学习一些基本的 Slack API 知识,并开始为我正在构建的应用程序构建一个 webhook。

我有以下代码:

$scope.postToSlack = function(){
    $http({
        url: 'https://hooks.slack.com/services/xxx/xxxx/xxxxx',
        method: "POST",
        payload:{"text": "This is a line of text in a channel.\nAnd this is another line of text."}
    })
    .then(function(response) {
        console.log(response)
    }, 
    function(response) {
        console.log(response)
    });
}

但是不断收到 500 错误,说没有收到有效负载。

关于为什么这不起作用的任何想法?

4

2 回答 2

1

尝试使用数据而不是有效负载(假设您在服务器上设置了整个 Access-Control-Allow-Origin 东西)。

$scope.postToSlack = function(){
    $http({
        url: 'https://hooks.slack.com/services/xxx/xxxx/xxxxx',
        method: "POST",
        data: 'payload=' + JSON.stringify({"text": "blah"})
    })
    .then(function(response) {
        console.log(response)
    }, 
    function(response) {
        console.log(response)
    });
}

仅供参考 - 您可能想在服务器端执行此操作。只是一个建议:

  1. 不用担心整个跨域发帖疯了

  2. 您不会与全世界分享您的 slack webhook 的 url。

也许它没有开放或者你只是在玩,但只是想我会提早提到它。

于 2016-01-12T22:13:13.543 回答
0

以下为我工作 -

$http({
    url: slackWebHookUrl,
    method: "POST",
    data: 'payload=' + JSON.stringify({"text": message,"channel" : slackChannelname, "username" : slackUsername}),
    headers: {"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"}
})
.then(function(response) {
    console.log("Slack response :" + JSON.stringify(response));
}, 
function(error) {
    console.log("Slack error :" + JSON.stringify(error));
});
于 2018-02-16T19:16:50.387 回答