Skip to content

Instantly share code, notes, and snippets.

@andrewkim316
Last active September 4, 2019 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewkim316/7cc12a08ae2aad354072d1802cb86795 to your computer and use it in GitHub Desktop.
Save andrewkim316/7cc12a08ae2aad354072d1802cb86795 to your computer and use it in GitHub Desktop.
# 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