Slack没有有效载荷接收到nodejs

 蓝颜似水2002 发布于 2023-01-01 18:37

所以使用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.任何人都可以帮忙吗?

我查看了令牌也完成了我的研究,但没有任何工作..

我感谢你的帮助

1 个回答
  • 您需要使用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();
    

    2023-01-01 18:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有