所以使用 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。有人可以帮忙吗?

我查看了 token
也做了我的研究,但没有任何效果..

我感谢您的所有帮助

最佳答案

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

关于node.js - Slack 没有收到有效负载 nodejs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24879486/

10-13 09:19