2019-09-10 15:17:35 +02:00
|
|
|
"Script that handles Haldis notifications on chat platforms"
|
2019-08-28 03:46:04 +02:00
|
|
|
import json
|
2019-10-01 20:57:23 +02:00
|
|
|
import typing
|
2019-08-28 03:46:04 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from flask import current_app as app
|
|
|
|
from flask import url_for
|
2019-10-01 20:57:23 +02:00
|
|
|
from models.order import Order
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
def webhook_text(order: Order) -> typing.Optional[str]:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Function that makes the text for the notification"""
|
2020-01-27 02:31:02 +01:00
|
|
|
if order.location_id == "test":
|
2019-10-01 20:57:23 +02:00
|
|
|
return None
|
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
if order.courier is not None:
|
2022-04-19 22:03:00 +02:00
|
|
|
# pylint: disable=C0301, C0209
|
2019-10-01 20:57:23 +02:00
|
|
|
return "<!channel|@channel> {3} is going to {1}, order <{0}|here>! Deadline in {2} minutes!".format(
|
2022-04-20 02:05:10 +02:00
|
|
|
url_for("order_bp.order_from_slug", order_slug=order.slug, _external=True),
|
2020-01-27 02:31:02 +01:00
|
|
|
order.location_name,
|
|
|
|
remaining_minutes(order.stoptime),
|
2022-06-03 19:27:11 +02:00
|
|
|
order.courier.username,
|
2019-09-05 03:33:29 +02:00
|
|
|
)
|
2019-10-01 20:57:23 +02:00
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
# pylint: disable=C0209
|
2019-10-01 20:57:23 +02:00
|
|
|
return "<!channel|@channel> New order for {}. Deadline in {} minutes. <{}|Open here.>".format(
|
2020-01-27 02:31:02 +01:00
|
|
|
order.location_name,
|
|
|
|
remaining_minutes(order.stoptime),
|
2022-04-20 02:05:10 +02:00
|
|
|
url_for("order_bp.order_from_slug", order_slug=order.slug, _external=True),
|
2019-10-01 20:57:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
def post_order_to_webhook(order: Order) -> None:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Function that sends the notification for the order"""
|
2020-01-27 02:31:02 +01:00
|
|
|
message = webhook_text(order)
|
2019-10-01 20:57:23 +02:00
|
|
|
if message:
|
2020-07-17 11:40:15 +02:00
|
|
|
webhookthread = WebhookSenderThread(message, app.config["SLACK_WEBHOOK"])
|
2019-10-01 20:57:23 +02:00
|
|
|
webhookthread.start()
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WebhookSenderThread(Thread):
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Extension of the Thread class, which sends a webhook for the notification"""
|
2019-10-16 15:00:45 +02:00
|
|
|
|
2019-09-11 22:38:01 +02:00
|
|
|
def __init__(self, message: str, url: str) -> None:
|
2022-04-19 22:03:00 +02:00
|
|
|
super().__init__()
|
2019-08-28 03:46:04 +02:00
|
|
|
self.message = message
|
2019-09-11 22:38:01 +02:00
|
|
|
self.url = url
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def run(self) -> None:
|
2019-08-28 03:46:04 +02:00
|
|
|
self.slack_webhook()
|
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def slack_webhook(self) -> None:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""The webhook for the specified chat platform"""
|
2019-09-11 22:38:01 +02:00
|
|
|
if self.url:
|
2019-09-16 23:14:52 +02:00
|
|
|
requests.post(self.url, json={"text": self.message})
|
2019-08-28 03:46:04 +02:00
|
|
|
else:
|
2019-09-11 22:38:01 +02:00
|
|
|
print(self.message)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def remaining_minutes(value) -> str:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Return the remaining minutes until the deadline of and order"""
|
2019-08-28 03:46:04 +02:00
|
|
|
delta = value - datetime.now()
|
|
|
|
if delta.total_seconds() < 0:
|
|
|
|
return "0"
|
2022-05-11 18:33:34 +02:00
|
|
|
minutes = int(delta.total_seconds() // 60)
|
2022-04-19 22:03:00 +02:00
|
|
|
return f"{minutes:02}"
|