简体   繁体   中英

Need helping answering POST with nginx

I'm writing a slack bot that needs to respond to a HTTP POST challenge and i thought i'd to this with nginx. It's supposed to respond with a HTTP 200 but i have no clue how to implement this. Here is the documentation: https://api.slack.com/events-api#url_verification

I'm not sure if i'm supposed to do this in a script or with a web server like nginx?

However, if i were to use nginx, how would a basic config look like that can respond to the challenge above?

I'm very new to this so i am sorry if this is making no sense.

I have a hipchat bot running on my server with nginx and nodejs. Here's what I have in nginx.conf:

upstream my_bot {
    server 127.0.0.1:3300;
    keepalive 8;
}

server {
    listen 80;
    server_name your.address.com;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://my_bot;
        proxy_redirect off;
    }
}

And javascript simply listens on the port 3300 internally:

const Http = require('http')

var server = Http.createServer(function(req, res) {
    if (req.method != 'POST') {
        res.writeHead(400, {'Content-Type': 'text/plain'})
        res.end('Error')
        return
    }
    var body = ''
    req.on('data', function (data) {
        body += data
    })
    req.on('end', function () {
        try{
            message = JSON.parse(body)
        }
        catch(e) {
            /* Not a JSON. Write error */
            res.writeHead(400, {'Content-Type': 'text/plain'})
            res.end('Format Error')
            return
        }
        if (message.token != '<your token here>') {
            /* Not valid token. Write error */
            res.writeHead(400, {'Content-Type': 'text/plain'})
            res.end('Token Error')
            return
        }
        /* Do your stuff with request and respond with a propper challenge field */
        res.writeHead(200, {'Content-Type': 'application/json'})
        res.end(JSON.stringify({challenge: message.challenge}))
    })
})
server.listen(3300)

To have this script running on my server as daemon I'm using pm2

You can run any other back-end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM