diff --git a/CHANGELOG.md b/CHANGELOG.md index dbf24410..83c7d0e5 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 (#780, by vimagick) + ### 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"""