user3863202
user3863202

Reputation: 31

Slack no payload received nodejs

SO using curl I can successfully send post request to 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

however when I converted it to code using 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)
        }
    }
);

it throws status 500. can anyone help?

i reviewed the token also done my research but nothing is working..

I appreciate all your help

Upvotes: 3

Views: 4157

Answers (4)

Saurabh Nanda
Saurabh Nanda

Reputation: 23

Either use form in your option object or set a body parameter along with the 'content-type' set to 'application/x-www-form-urlencoded'. Here's a working example.

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

Upvotes: 1

Kelly Milligan
Kelly Milligan

Reputation: 588

wanted to chime in, as I found this while also trying to do the same thing. I ended up doing this:

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

Upvotes: 1

vanx2
vanx2

Reputation: 51

I think it is not payload but form. This code succeed in calling 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);
  }
});

Upvotes: 5

Ty Shaikh
Ty Shaikh

Reputation: 181

You need to use the https library, since the server requests are on a different port. You current code is sending the request to port 80 instead of port 443. This is some sample code I built for an integration.

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

Upvotes: 6

Related Questions