From babedf5d15e7e730e9d42efc7801cda543daf856 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Wed, 4 Oct 2023 17:55:19 +0800 Subject: [PATCH] fix: tg notification --- backend/src/module/network/request_contents.py | 3 +++ backend/src/module/network/request_url.py | 4 ++-- backend/src/module/notification/plugin/telegram.py | 10 ++++++++-- backend/src/module/utils/cache_image.py | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/backend/src/module/network/request_contents.py b/backend/src/module/network/request_contents.py index 9ab927bb..2eff2276 100644 --- a/backend/src/module/network/request_contents.py +++ b/backend/src/module/network/request_contents.py @@ -55,6 +55,9 @@ class RequestContent(RequestURL): def post_data(self, _url, data: dict) -> dict: return self.post_url(_url, data) + def post_form(self, _url, data: dict, files: dict) -> dict: + return self.post_form(_url, data, files) + def get_html(self, _url): return self.get_url(_url).text diff --git a/backend/src/module/network/request_url.py b/backend/src/module/network/request_url.py index 4f813b54..d4e17395 100644 --- a/backend/src/module/network/request_url.py +++ b/backend/src/module/network/request_url.py @@ -72,10 +72,10 @@ class RequestURL: logger.debug(f"[Network] Cannot connect to {url}.") return False - def post_form(self, url: str, data: dict): + def post_form(self, url: str, data: dict, files): try: req = self.session.post( - url=url, headers=self.header, data=data, timeout=5 + url=url, headers=self.header, data=data, files=files, timeout=5 ) req.raise_for_status() return req diff --git a/backend/src/module/notification/plugin/telegram.py b/backend/src/module/notification/plugin/telegram.py index 36e6672e..dbd0c962 100644 --- a/backend/src/module/notification/plugin/telegram.py +++ b/backend/src/module/notification/plugin/telegram.py @@ -2,6 +2,7 @@ import logging from module.models import Notification from module.network import RequestContent +from module.utils import load_image logger = logging.getLogger(__name__) @@ -24,9 +25,14 @@ class TelegramNotification(RequestContent): data = { "chat_id": self.chat_id, "caption": text, - "photo": notify.poster_path, "disable_notification": True, } - resp = self.post_data(self.notification_url, data) + photo = load_image(notify.poster_path) + if photo: + resp = self.post_form( + self.notification_url, data, files={"photo": photo} + ) + else: + resp = self.post_data(self.notification_url, data) logger.debug(f"Telegram notification: {resp.status_code}") return resp.status_code == 200 diff --git a/backend/src/module/utils/cache_image.py b/backend/src/module/utils/cache_image.py index 2753f3e6..2ff8e553 100644 --- a/backend/src/module/utils/cache_image.py +++ b/backend/src/module/utils/cache_image.py @@ -10,5 +10,5 @@ def save_image(img, suffix): def load_image(img_path): - with open(f"data/images/{img_path}", "rb") as f: + with open(f"data/{img_path}", "rb") as f: return f.read()