3

所以使用 curl 我可以成功地将发布请求发送到 slack

curl -X POST --data-urlencode 'payload={"channel": "#tech-experiment", "username": "at-bot", "text": "This is posted to #general and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}' https:/company.slack.com/services/hooks/incoming-webhook?token=dddddddd2342343

但是,当我使用 nodejs 将其转换为代码时

var request = require('request');
var http = require('http');
var server = http.createServer(function(req, response){
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.end("end");
});

option = {
    url: 'https://company.slack.com/services/hooks/incoming-webhook?token=13123213asdfda',
    payload: '{"text": "This is a line of text in a channel.\nAnd this is another line of text."}'
}

request.post(
    option,

    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }else {
            console.log('wtf')
            console.log(response.statusCode)
            console.log(response)
            console.log(error)
        }
    }
);

它抛出状态 500。有人可以帮忙吗?

我审查了令牌也做了我的研究,但没有任何工作..

感谢您的帮助

4

4 回答 4

6

您需要使用 https 库,因为服务器请求位于不同的端口上。您当前的代码将请求发送到端口 80 而不是端口 443。这是我为集成构建的一些示例代码。

var https = require( 'https' );
var options = {
    hostname : 'company.slack.com' ,
    path     : '/services/hooks/incoming-webhook?token=rUSX9IyyYiQmotgimcMr4uK8' ,
    method   : 'POST'
};

var payload1 = {
    "channel"    : "test" ,
    "username"   : "masterbot" ,
    "text"       : "Testing the Slack API!" ,
    "icon_emoji" : ":ghost:"
};

var req = https.request( options , function (res , b , c) {
    res.setEncoding( 'utf8' );
    res.on( 'data' , function (chunk) {
    } );
} );

req.on( 'error' , function (e) {
    console.log( 'problem with request: ' + e.message );
} );

req.write( JSON.stringify( payload1 ) );
req.end();
于 2014-09-22T20:45:15.713 回答
5

我认为不是payload但是form。此代码成功调用传入 Webhook。

var request = require('request');

var options = {
  uri: "https://hooks.slack.com/services/yourURI",
  form: '{"text": "This code..."}'
};
request.post(options, function(error, response, body){
  if (!error && response.statusCode == 200) {
    console.log(body.name);
  } else {
    console.log('error: '+ response.statusCode + body);
  }
});
于 2015-03-05T00:27:09.870 回答
1

要么在你的选项对象中使用表单,要么设置一个body参数以及'content-type'设置为'application/x-www-form-urlencoded'。这是一个工作示例。

var payload = JSON.stringify(payload)
request.post({
    headers: {'content-type' : 'application/x-www-form-urlencoded'},
    url:     'Your Webhook URL',
    body:    "payload="+payload
    }, function(error, response, body){
        if(error){
          console.log(error);
        }
        console.log(body);
});
于 2017-04-25T10:13:24.197 回答
1

想插话,因为我发现了这一点,同时也试图做同样的事情。我最终这样做了:

 got('https://hooks.slack.com/services/[your configured url]', {
              method: 'POST',
              body: JSON.stringify({
                "text": "message" + variable
              })
            });
于 2015-11-06T18:26:02.070 回答