fix: prevent TypeError in Teams webhook when user data is missing (#22444)

json.loads(event_data.get("user", {})) crashes with TypeError when
the "user" key is absent because the default value {} is a dict, not
a JSON string. json.loads expects str/bytes, not dict.

Also handle the case where "user" is already a dict (not serialized
JSON) to make the webhook more robust.

Co-authored-by: gambletan <ethanchang32@gmail.com>
This commit is contained in:
Alvin Tang
2026-03-09 05:45:21 +08:00
committed by GitHub
parent f78b238b40
commit 3e513be963

View File

@@ -26,9 +26,14 @@ async def post_webhook(name: str, url: str, message: str, event_data: dict) -> b
# Microsoft Teams Webhooks
elif "webhook.office.com" in url:
action = event_data.get("action", "undefined")
user_data = event_data.get("user", "{}")
if isinstance(user_data, dict):
user_dict = user_data
else:
user_dict = json.loads(user_data)
facts = [
{"name": name, "value": value}
for name, value in json.loads(event_data.get("user", {})).items()
for name, value in user_dict.items()
]
payload = {
"@type": "MessageCard",