Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to send Teams messages as HTML content #224

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions custom_components/o365/classes/teamssensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ def extra_state_attributes(self):
attributes[ATTR_DATA] = self.coordinator.data[self.entity_key][ATTR_DATA]
return attributes

def send_chat_message(self, chat_id, message):
def send_chat_message(self, chat_id, message, content_type="text"):
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Validate 'content_type' parameter to ensure it only accepts 'text' or 'html'.

Adding a validation step for 'content_type' can prevent errors from unsupported types and ensure that the function behaves as expected.

Suggested change
def send_chat_message(self, chat_id, message, content_type="text"):
def send_chat_message(self, chat_id, message, content_type="text"):
valid_content_types = ["text", "image", "video"]
if content_type not in valid_content_types:
raise ValueError("Unsupported content type")

"""Send a message to the specified chat."""
if not self._validate_chat_permissions():
return False

chats = self.teams.get_my_chats()
for chat in chats:
if chat.object_id == chat_id:
message = chat.send_message(content=message)
message = chat.send_message(content=message, content_type=content_type)
self._raise_event(EVENT_SEND_CHAT_MESSAGE, chat_id)
return True
_LOGGER.warning("Chat %s not found for send message", chat_id)
Expand Down
1 change: 1 addition & 0 deletions custom_components/o365/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class EventResponse(Enum):
ATTR_CHAT_ID = "chat_id"
ATTR_CHAT_TYPE = "chat_type"
ATTR_COMPLETED = "completed"
ATTR_CONTENT_TYPE = "content_type"
ATTR_CREATED = "created"
ATTR_COLOR = "color"
ATTR_CONTENT = "content"
Expand Down
11 changes: 2 additions & 9 deletions custom_components/o365/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ATTR_CATEGORIES,
ATTR_CHAT_ID,
ATTR_COMPLETED,
ATTR_CONTENT_TYPE,
ATTR_DESCRIPTION,
ATTR_DUE,
ATTR_EMAIL,
Expand Down Expand Up @@ -337,13 +338,5 @@
CHAT_SERVICE_SEND_MESSAGE_SCHEMA = {
vol.Required(ATTR_CHAT_ID): cv.string,
vol.Required(ATTR_MESSAGE): cv.string,
}

CHAT_SERVICE_SEND_MESSAGE_SCHEMA = {
vol.Required(ATTR_CHAT_ID): cv.string,
vol.Required(ATTR_MESSAGE): cv.string,
}
CHAT_SERVICE_SEND_MESSAGE_SCHEMA = {
vol.Required(ATTR_CHAT_ID): cv.string,
vol.Required(ATTR_MESSAGE): cv.string,
vol.Optional(ATTR_CONTENT_TYPE, default="text"): cv.String,
}
13 changes: 13 additions & 0 deletions custom_components/o365/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,19 @@ send_chat_message:
required: true
selector:
text:
content_type:
name: Content Type
description: The type of content to send, html if you are sending a HTML message or text for plain text
example: text
required: false
selector:
select:
mode: dropdown
options:
- label: HTML
Comment on lines +468 to +477
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Consider marking 'content_type' as required if it's critical for message formatting.

If the content type is essential for the message to be formatted correctly, it might be better to enforce its specification by making it required.

value: html
- label: Text
value: text

update_user_status:
name: Update user Teams status
Expand Down
1 change: 1 addition & 0 deletions docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ target:
data:
chat_id: xxxxxxxxxxxxxxxxxxxxxxxxx
message: Hello world
content_type: text
Copy link

Choose a reason for hiding this comment

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

suggestion (code_clarification): Update documentation to explain the optional nature of 'content_type' and its defaults.

Expanding the documentation to clarify when and how to use the 'content_type' field can enhance user understanding and correct usage of the API.

Suggested change
content_type: text
content_type: text
# The `content_type` field specifies the type of content being sent.
# Use this field to define the format of the message content.
# For example, 'text' indicates that the message content is plain text.

```

## Status Services
Expand Down