Skip to content

Instantly share code, notes, and snippets.

@seratch
Last active May 20, 2021 00:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seratch/cebf54230086cfde67530531c1b7ee0d to your computer and use it in GitHub Desktop.
Save seratch/cebf54230086cfde67530531c1b7ee0d to your computer and use it in GitHub Desktop.
Bolt for Python: AWS App Runner Example
import os
import logging
logging.basicConfig(level=logging.DEBUG)
# -----------------------------
# Bolt app
# -----------------------------
from slack_bolt.async_app import AsyncApp
app = AsyncApp(
token=os.environ["SLACK_BOT_TOKEN"],
signing_secret=os.environ["SLACK_SIGNING_SECRET"],
)
@app.event("app_mention")
async def hello(event, say):
await say(f"Hey <@{event['user']}>!")
# -----------------------------
# Web app & server
# -----------------------------
import uvicorn
from fastapi import FastAPI, Request, Response
from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
api = FastAPI()
handler = AsyncSlackRequestHandler(app)
@api.get("/")
async def root(req: Request):
return Response(status_code=200, content="OK")
@api.post("/slack/events")
async def slack_events(req: Request):
return await handler.handle(req)
# python3 app.py
if __name__ == "__main__":
uvicorn.run(
"app:api",
host="0.0.0.0",
port=int(os.environ.get("PORT", 8000)),
)
version: 1.0
runtime: python3
build:
commands:
build:
- pip install -r requirements.txt
# Using the admin console to set these variables would be recommended
env:
- name: SLACK_BOT_TOKEN
value: "xoxb-"
- name: SLACK_SIGNING_SECRET
value: ""
run:
command: python app.py
network:
port: 8000
# https://gallery.ecr.aws/e5d9f5q7/aws-app-runner-bolt-python
FROM python:3.9.5-slim-buster
EXPOSE 8000
WORKDIR /app/
COPY requirements.txt /app/
COPY app.py /app/
RUN pip install -r requirements.txt
CMD python /app/app.py
slack-bolt>=1.6,<2
aiohttp>=3,<4
fastapi>=0.65.1,<0.66
uvicorn>=0.13.4,<0.14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment