2017-04-22 8 views
0

私はHTTP POSTのチャレンジに応答する必要があるスラックボットを書いています。それはHTTP 200で応答するはずですが、私はこれを実装する方法の手がかりがありません。ここにドキュメントがあります:https://api.slack.com/events-api#url_verificationnginxでPOSTに答えるのを助ける必要があります

私はスクリプトやnginxのようなWebサーバーでこれを行うことになっていますか分かりませんか?

しかし、もしnginxを使っていたら、基本的な設定はどのようになって上記の問題に対応できますか?

私はこれにはとても新しいので、これは意味をなさないと申し訳ありません。

答えて

1

私はnginxとnodejsを使ってサーバー上でヒップホップボットを実行しています。 は、ここで私は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; 
    } 
} 

とJavaScriptは、単に内部ポート3300でリッスン:

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) 

このスクリプトは私がpm2を使用していデーモンとして私のサーバー上で実行しているために

他のバックエンドを実行することもできます。

関連する問題