From cf85208d682b70ae3a2bbfe07b4b0b3be2175597 Mon Sep 17 00:00:00 2001 From: Tim van der Heijden Date: Thu, 3 Oct 2024 14:42:11 +0200 Subject: [PATCH] Add raise error when a Teams message is large --- README.md | 4 ++++ notify/__init__.py | 2 +- notify/teams.py | 7 +++++++ notify/tests/test_teams.py | 12 +++++++++++- setup.cfg | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8af0bcd..0642215 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ mail.send_email() ## Notify Teams For the Notify for Teams (1.0.0) you can create a webhook as by following the steps in the [Microsoft documentation](https://support.microsoft.com/en-us/office/create-incoming-webhooks-with-workflows-for-microsoft-teams-8ae491c7-0394-4861-ba59-055e33f75498). + +Requirement: Add member that sends the message to the channel + +In the workflow app, make sure the message is sent as user, and not as Flowbot. ```python from notify import NotifyTeams from notify.tests import import_sample_dfs diff --git a/notify/__init__.py b/notify/__init__.py index fb73a63..3fb1519 100644 --- a/notify/__init__.py +++ b/notify/__init__.py @@ -11,4 +11,4 @@ ) name = "notify" -__version__ = "1.0.0" +__version__ = "1.0.1" diff --git a/notify/teams.py b/notify/teams.py index b5c5414..58828d5 100644 --- a/notify/teams.py +++ b/notify/teams.py @@ -3,6 +3,7 @@ import pandas as pd import requests from notify.types import DfsInfo +from pympler import asizeof class NotifyTeams: @@ -41,6 +42,9 @@ def create_adaptive_card_dataframe(df: pd.DataFrame) -> dict: if df.shape[0] > 30: logging.warning(f"only first 30 records will be added.({df.shape[0]}> the limit of 30).") df = df.head(n=30) + if df.shape[1] > 10: + logging.warning("Only the first 10 columns will be shown") + df = df.iloc[:, :10].copy() df_dict = df.to_dict("records") col_widths = [] @@ -349,6 +353,9 @@ def basic_message( if extra: self.add_extra_elements(extra) self.msg["attachments"][0]["content"]["body"] = self.body + body_size = asizeof.asizeof(self.body) + if body_size > 40_000: + raise ValueError(f"Body size is {body_size} bytes. This is will result in a Teams message above 28KB.") try: response = requests.post(url=self.webhook, json=self.msg) return response diff --git a/notify/tests/test_teams.py b/notify/tests/test_teams.py index 518ea53..90e49f6 100644 --- a/notify/tests/test_teams.py +++ b/notify/tests/test_teams.py @@ -1,5 +1,7 @@ import os +import pytest + from notify import NotifyTeams from notify.tests import import_sample_dfs @@ -45,7 +47,6 @@ def test_teams_with_df(): """ df = import_sample_dfs().get("Transactions") - teams = NotifyTeams(webhook=os.environ.get("teams_webhook")) teams.basic_message( title="Pytest with 1 dataframe", @@ -75,5 +76,14 @@ def test_teams_with_extra(): ) # adds the dataframe to the message as a table +def test_teams_to_large_basic_message(): + """ + versturen van een simpel teams bericht + """ + teams = NotifyTeams(webhook=os.environ.get("teams_webhook")) + with pytest.raises(ValueError): + teams.basic_message(title="Pytest", message="This is message is too large" * 9999) + + if __name__ == "__main__": test_teams_with_df() diff --git a/setup.cfg b/setup.cfg index 63c74b6..214ce91 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = zyppnotify -version = 1.0.0 +version = 1.0.1 author = Zypp author_email = hello@zypp.io description = Send users notifications through various platforms