Slack没有有效载荷接收到nodejs

时间:2014-07-22 05:21:51

标签: node.js curl slack-api

所以使用curl我可以成功发送post请求到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 个答案:

答案 0 :(得分:5)

您需要使用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();

答案 1 :(得分:4)

我认为它不是payload而是form。 此代码成功调用了Incoming Webhooks。

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);
  }
});

答案 2 :(得分:1)

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

 got('https://hooks.slack.com/services/[your configured url]', {
              method: 'POST',
              body: JSON.stringify({
                "text": "message" + variable
              })
            });

答案 3 :(得分: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);
});