Skip to content

Commit

Permalink
Merge pull request #1 from xiaoyu74/slack-bolt-dev-env
Browse files Browse the repository at this point in the history
Setup slack bolt development environment
  • Loading branch information
hemslo authored Feb 15, 2024
2 parents 8004d14 + 4364194 commit 5e628db
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/tools/slack_bolt_env/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
slack-bolt==1.18.1
fastapi==0.79.0
uvicorn==0.17.6
python-dotenv==0.20.0
requests==2.27.1
19 changes: 19 additions & 0 deletions app/tools/slack_bolt_env/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Create the virtual environment
echo "Creating virtual environment..."
python3 -m venv .venv

# Activate the virtual environment
echo "Activating virtual environment..."
source .venv/bin/activate

# Install dependencies
echo "Installing dependencies from requirements.txt..."
pip install -r requirements.txt

echo "Setup completed. Virtual environment is ready and dependencies are installed."

# Run the app
echo "Running sre-ascent-dev.py..."
python3 sre-ascent-dev.py
47 changes: 47 additions & 0 deletions app/tools/slack_bolt_env/sre-ascent-dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_bolt.adapter.fastapi import SlackRequestHandler
from fastapi import FastAPI, Request

# Initialize a Bolt for sre-ascent-dev app
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)

# Define an event listener for "message" events
@app.event("message")
def handle_message_events(body, say, logger):
logger.info(body)
text = body.get('event', {}).get('text', '')
if 'hello' in text.lower():
say("Hello there! :wave:")

# Define an event listener for "app_mention" events
@app.event("app_mention")
def handle_app_mention_events(body, say, logger):
logger.info(body)
say("Hello SRE slackers! :blush:")

# FastAPI app to handle routes
fastapi_app = FastAPI()

# Create a request handler for the app
handler = SlackRequestHandler(app)

@fastapi_app.post("/slack/events")
async def slack_events(request: Request):
return await handler.handle(request)

# Check if the script is being run directly
if __name__ == "__main__":
# Check if SLACK_APP_TOKEN is set for Socket Mode
if 'SLACK_APP_TOKEN' in os.environ:
# Start the app in Socket Mode
handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
handler.start()
else:
# If not using socket mode, we can start the FastAPI app using the command in terminal:
# uvicorn app:fastapi_app --reload --port 3000
print("SLACK_APP_TOKEN not found. Please set it to run in Socket Mode or to run the FastAPI app with uvicorn from the terminal.")

0 comments on commit 5e628db

Please sign in to comment.