Last active
September 4, 2019 22:10
-
-
Save andrewkim316/7cc12a08ae2aad354072d1802cb86795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Subscribe to streaming channel(s). Must contain one or more arguments | |
# representing streams you want to connect to. | |
@app.route("/subscribe_streaming", methods=["POST"]) | |
def stream_data_handler(): | |
args = request.form.get("text").split(" ") | |
if len(args) == 1 and args[0].strip() == "": | |
return BAD_ARGS | |
try: | |
connected = 0 | |
for stream in args: | |
# If the specified stream exists in the dictionary and it hasn't | |
# been initialized, subscribe and listen to it. | |
if streams[stream] is None: | |
streams[stream] = multiprocessing.Process( | |
target=runThread, args=(stream,)) | |
streams[stream].start() | |
connected += 1 | |
if len(args) == connected: | |
text = f"Subscription{('','s')[connected > 1]} to {(' ').join(args)} sent." | |
response = requests.post( | |
url="https://slack.com/api/chat.postMessage", | |
data={ | |
"token": config["slack_token"], | |
"channel": request.form.get("channel_name"), | |
"text": text}) | |
return "" | |
else: | |
return f"{len(args) - connected} subscription(s) failed." | |
except Exception as e: | |
return f'ERROR: {str(e)}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment