From 69a4c30510ace57b83567667afac64874ba369c3 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 5 Dec 2023 22:31:07 +0800 Subject: [PATCH 1/2] SlackReporter with preformatted rich text --- CHANGELOG.md | 4 ++++ lib/urlwatch/reporters.py | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbf24410..4dd300a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format mostly follows [Keep a Changelog](http://keepachangelog.com/en/1.0.0/ ## UNRELEASED +### Added + +- Slack Reporter: `rich_text` config option for preformatted rich text + ### Changed - Remove EOL'd Python 3.7 (new minimum requirement is Python 3.8), add Python 3.12 testing diff --git a/lib/urlwatch/reporters.py b/lib/urlwatch/reporters.py index e3983417..ac396c08 100644 --- a/lib/urlwatch/reporters.py +++ b/lib/urlwatch/reporters.py @@ -705,7 +705,7 @@ def submit(self): def submit_chunk(self, webhook_url, text): logger.debug("Sending {} request with text: {}".format(self.__kind__, text)) - post_data = {"text": text} + post_data = self.prepare_post_data(text) result = requests.post(webhook_url, json=post_data) try: if result.status_code == requests.codes.ok: @@ -719,12 +719,35 @@ def submit_chunk(self, webhook_url, text): result.content)) return result + def prepare_post_data(self, text): + if self.config.get('rich_text', False): + return { + "blocks": [ + { + "type": "rich_text", + "elements": [ + { + "type": "rich_text_preformatted", + "elements": [ + {"type": "text", "text": text} + ] + } + ] + } + ] + } + else: + return {"text": text} + class MattermostReporter(SlackReporter): """Send a message to a Mattermost channel""" __kind__ = 'mattermost' + def prepare_post_data(self, text): + return {"text": text} + class DiscordReporter(TextReporter): """Send a message to a Discord channel""" From 25468e2cf5e2e16935c3eb2a36d9d377ba458887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=CC=B6e=CC=B6v=CC=B6i=CC=B6n=CC=B6?= Date: Mon, 11 Dec 2023 13:24:26 +0800 Subject: [PATCH 2/2] Update CHANGELOG.md Co-authored-by: Thomas Perl --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dd300a6..83c7d0e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ The format mostly follows [Keep a Changelog](http://keepachangelog.com/en/1.0.0/ ### Added -- Slack Reporter: `rich_text` config option for preformatted rich text +- Slack Reporter: `rich_text` config option for preformatted rich text (#780, by vimagick) ### Changed