Skip to content

Commit

Permalink
Allow setting Slack messsage colour as attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Oct 3, 2024
1 parent 8a5cf9a commit 06b50d6
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lvmapi/routers/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Message(BaseModel):
Field(description="Blocks of data to be sent"),
] = None

attachments: Annotated[
list[dict] | None,
Field(description="Attachments to be sent"),
] = None

channel: Annotated[
str | None,
Field(
Expand Down Expand Up @@ -88,6 +93,7 @@ async def route_post_message(message: Message) -> None:
username=message.username,
icon_url=message.icon_url,
mentions=message.mentions,
attachments=message.attachments,
)
except Exception as err:
raise HTTPException(500, detail=str(err))
Expand All @@ -101,15 +107,30 @@ async def route_get_message(
channel: str | None = Query(None, description="Channel where to send the message"),
username: str = Query(default_user, description="Username to send the message as"),
icon_url: str | None = Query(None, description="URL for the icon to use"),
color: str | None = Query(None, description="Color of the message attachment"),
) -> None:
"""Sends a message to the Slack channel."""

if color is not None:
attachments = [
{
"color": color,
"text": text,
"mrkdwn_in": ["text"],
"fallback": text,
}
]
text = ""
else:
attachments = []

await route_post_message(
Message(
text=text,
channel=channel,
username=username,
icon_url=icon_url,
attachments=attachments,
)
)

Expand Down

0 comments on commit 06b50d6

Please sign in to comment.