From 5b2f6de323cc45945ddae4b6e9755b9d0b51540d Mon Sep 17 00:00:00 2001 From: redfast00 Date: Wed, 11 Sep 2019 22:38:01 +0200 Subject: [PATCH] Fix webhook not having app context --- app/config.example.py | 2 +- app/notification.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/config.example.py b/app/config.example.py index 9f5ceca..48367fc 100644 --- a/app/config.example.py +++ b/app/config.example.py @@ -6,7 +6,7 @@ class Configuration(object): SQLALCHEMY_TRACK_MODIFICATIONS = False DEBUG = True SECRET_KEY = "" - SLACK_WEBHOOK = "" + SLACK_WEBHOOK = None LOGFILE = "haldis.log" ZEUS_KEY = "tomtest" ZEUS_SECRET = "blargh" diff --git a/app/notification.py b/app/notification.py index be20c3c..db13610 100644 --- a/app/notification.py +++ b/app/notification.py @@ -22,30 +22,29 @@ def post_order_to_webhook(order_item) -> None: remaining_minutes(order_item.stoptime), url_for("order_bp.order", id=order_item.id, _external=True), ) - webhookthread = WebhookSenderThread(message) + webhookthread = WebhookSenderThread(message, app.config["SLACK_WEBHOOK"]) webhookthread.start() class WebhookSenderThread(Thread): - def __init__(self, message: str) -> None: + def __init__(self, message: str, url: str) -> None: super(WebhookSenderThread, self).__init__() self.message = message + self.url = url def run(self) -> None: self.slack_webhook() def slack_webhook(self) -> None: - js = json.dumps({"text": self.message}) - url = app.config["SLACK_WEBHOOK"] - if len(url) > 0: - requests.post(url, data=js) + if self.url: + requests.post(url, json={"text": self.message}) else: - app.logger.info(str(js)) + print(self.message) def remaining_minutes(value) -> str: delta = value - datetime.now() if delta.total_seconds() < 0: return "0" - minutes, _ = divmod(delta.total_seconds(), 60) + minutes = delta.total_seconds() // 60 return "%02d" % minutes