Skip to content

Commit

Permalink
switch to thread approach
Browse files Browse the repository at this point in the history
  • Loading branch information
isaric committed Dec 16, 2023
1 parent 198d27f commit ca53cd6
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
from mongo_client.repository import Repository
import os
import logging
from threading import Thread

from slack_client.slack import SlackClient

class DetectorFacade:

def __init__(self, detector, slack_verification_token, read_channel):
self.detector = detector
self.slack_verification_token = slack_verification_token
self.read_channel = read_channel

def process(self, content):
if content["token"] != self.slack_verification_token:
app.logger.info("Invalid token")
return
if "event" in content and "files" in content["event"]:
app.logger.info("Received event contains files")
# TODO: switch to manageable list of channel ids
if content["event"]["channel"] == self.read_channel:
detector.detect(content["event"]["files"][0]["url_private_download"])
else:
app.logger.info(f'Channel ID {content["event"]["channel"]} is not on read list. Skipping event')

app = Flask(__name__)


Expand All @@ -21,24 +41,17 @@
slack_client = SlackClient(os.environ['SLACK_API_TOKEN'])
repository = Repository(os.environ['MONGO_CONNECTION_STRING'])
detector = Detector(os.environ['WRITE_SLACK_CHANNEL_ID'], slack_client, repository)
detector_facade = DetectorFacade(detector, os.environ['SLACK_VERIFICATION_TOKEN'], os.environ['READ_SLACK_CHANNEL_ID'])

@app.route('/api/events', methods=['POST'])
async def add_message():
def add_message():
content = request.get_json(silent=True)
app.logger.info("received incoming event %s", content)
if content["type"] == "url_verification":
# TODO: get token from initial challenge instead of using env variable
return content["challenge"]
if content["token"] != os.environ['SLACK_VERIFICATION_TOKEN']:
app.logger.info("Invalid token")
return "Invalid token"
if "event" in content and "files" in content["event"]:
app.logger.info("Received event contains files")
# TODO: switch to manageable list of channel ids
if content["event"]["channel"] == os.environ['READ_SLACK_CHANNEL_ID']:
detector.detect(content["event"]["files"][0]["url_private_download"])
else:
app.logger.info(f'Channel ID {content["event"]["channel"]} is not on read list. Skipping event')
Thread(target=detector_facade.process, args=(content,)).start()
return "OK"



0 comments on commit ca53cd6

Please sign in to comment.