Last active
September 4, 2019 22:10
-
-
Save andrewkim316/937d8e74ff9bbfb380650e5a1c86eb65 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
# Unsubsribe to streaming channel(s). Must contain one or more arguments | |
# representing streams you want to disconnect to. | |
@app.route("/unsubscribe_streaming", methods=["POST"]) | |
def unsubscribe_handler(): | |
args = request.form.get("text").split(" ") | |
if len(args) == 1 and args[0].strip() == "": | |
return BAD_ARGS | |
try: | |
disconnected = 0 | |
for stream in args: | |
# If the specified stream exists in the dictionary and it has been | |
# initialized, stop listening. | |
if streams[stream] is not None: | |
streams[stream].terminate() | |
streams[stream] = None | |
disconnected += 1 | |
if len(args) == disconnected: | |
text = f"Unsubscription{('', 's')[disconnected > 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) - disconnected} unsubscription(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