Skip to content

Conversation

@tre-ert
Copy link
Contributor

@tre-ert tre-ert commented Dec 15, 2025

I did not find any issue for this matter and as the fix is quite small I did not created one.

TLDR

Changing the variable botname to user at line 682 of the notifications.py file fixes an issue where the username of a Mattermost notifications is always "Prefect Notifications" even if a botname value was set.

Detailed explanation

When using Mattermost webhooks to send messages, the sender's username/botname displayed on the channel can be set as we which.
However, even when updating the botname section of the Mattermost block, the botname was : Prefect Notifications.
image
Screenshot of Mattermost block from Prefect UI
image
Screenshot of Mattermost instance

This string is declared in notifications.py file as the app_id of an AppriseAsset:

# A custom `AppriseAsset` that ensures Prefect 
# appear correctly across multiple messaging platforms
prefect_app_data = AppriseAsset(
    app_id="Prefect Notifications",
    app_desc="Prefect Notifications",
    app_url="https://prefect.io",
)

We'll get to this string later. In the Mattermost class of the same notifications.py file, a botname can be set:

botname: Optional[str] = Field(
    title="Bot name",
    default=None,
    description="The name of the bot that will send the message.",
)

and is later used in the NotifyMattermost class when calling its url() method:

def block_initialization(self) -> None:
    try:
        # Try importing for apprise>=1.18.0
        from apprise.plugins.mattermost import NotifyMattermost
    except ImportError:
        # Fallback for versions apprise<1.18.0
        from apprise.plugins.NotifyMattermost import (  # pyright: ignore[reportMissingImports] this is a fallback
            NotifyMattermost,  # pyright: ignore[reportUnknownVariableType] incomplete type hints in apprise
        )

    url = SecretStr(
        NotifyMattermost(
            token=self.token.get_secret_value(),
            fullpath=self.path,
            host=self.hostname,
            botname=self.botname,
            channels=self.channels,
            include_image=self.include_image,
            port=self.port,
            secure=self.secure,
        ).url()  # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] incomplete type hints in apprise
    )
    self._start_apprise_client(url)

However, in the apprise code, the botname variable doesn't exist in the url method. Instead, the botname value is inherited from the user variable:

# Determine if there is a botname present
botname = ""
if self.user:
    botname = "{botname}@".format(
        botname=NotifyMattermost.quote(self.user, safe=""),
    )

Thus, the botname value is never filled with the value provided by Prefect as the self.botname doesn't exist. But when the botname is empty, a username must still be provided to Mattermost. In the send function, the username is set as self.user or self.app_id:

# Set our user
payload["username"] = self.user if self.user else self.app_id

As such, the value specified earlier in the AppriseAsset was used and thus Prefect Notifications was the username used. Tests showed that changing the self.app_id value to another (when no botname was set) changed the username in the Mattermost notification.
Tests also confirmed that changing the variable botname to user at line 682 of the notifications.py file allowed to chose the username of the Mattermost notification (without Prefect Notifications overwriting it).

If you want to reproduce the issue, ensure integrations are allowed to replace the usernames in your Mattermost otherwise the name of the Mattermost user that created the webhook will be always used.

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 15, 2025

CodSpeed Performance Report

Merging #19780 will not alter performance

Comparing tre-ert:fix-mattermost-botname (51531f8) with main (42be7df)

Summary

✅ 2 untouched

Copy link
Collaborator

@zzstoatzz zzstoatzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@zzstoatzz zzstoatzz merged commit 4939097 into PrefectHQ:main Dec 15, 2025
57 of 58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants