Skip to content

Commit

Permalink
Add raise error when a Teams message is large
Browse files Browse the repository at this point in the history
  • Loading branch information
TimvdHeijden committed Oct 3, 2024
1 parent aaa89ed commit cf85208
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion notify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
)

name = "notify"
__version__ = "1.0.0"
__version__ = "1.0.1"
7 changes: 7 additions & 0 deletions notify/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pandas as pd
import requests
from notify.types import DfsInfo
from pympler import asizeof


class NotifyTeams:
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion notify/tests/test_teams.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

import pytest

from notify import NotifyTeams
from notify.tests import import_sample_dfs

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = zyppnotify
version = 1.0.0
version = 1.0.1
author = Zypp
author_email = [email protected]
description = Send users notifications through various platforms
Expand Down

0 comments on commit cf85208

Please sign in to comment.