2
1

More than 3 years have passed since last update.

python responderを利用してSlack botのチャレンジパラメータを応答させる

Last updated at Posted at 2020-03-22

python3とresponderを利用して、Slack Botを作成しているので備忘録
responderを利用するためには、python3.6以上が必要です

responderをインストール

pip install responder

以下のようにpythonファイルを作成

slack.py
import responder

api = responder.API()

@api.route('/')
async def index(req, resp):
    resp.status_code = 200
    if req.method == "post":
        req_data = await req.media(format="json")
        if 'challenge' in req_data:
            token = str(req_data['challenge'])
            resp.headers["Content-type"] = "text/plain"
            resp.text = token

            return

if __name__ == '__main__':
    api.run(debug=True)

作成したファイルを実行

python slack.py

[32mINFO[0m:     Started server process [[36m39040[0m]
[32mINFO[0m:     Uvicorn running on [1mhttp://127.0.0.1:5042[0m (Press CTRL+C to quit)
[32mINFO[0m:     Waiting for application startup.
[32mINFO[0m:     Application startup complete.

標準だとローカルの5042番ポートで実行されます
ローカルで実行しているので、外部からアクセスさせるためにはngrokを利用して、
SlackのRequest URLへngrokのアドレスを指定し問題なくチェックされるかを確認します

responder はバックグランド処理が簡単にかけるので便利そうです。

Herokuへのデプロイ方法も公式にあります
https://responder.kennethreitz.org/en/latest/deployment.html

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1