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

Added option to set msgtype for messages #12

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ message: |
- {{ text }}
{% endfor %}
message_format: markdown
message_type: m.text
auth_type: Basic
auth_token: abc:123
force_json: false
Expand Down Expand Up @@ -102,6 +103,12 @@ The format the message is interpreted as. Must be one of:
- `html`


### `message_type`
The type the message is sent as. Must be one of:
- `m.text` (default)
- `m.notice`


### `auth_type`
This can be used to protect a webhook against unauthorized access.
Can be one of `Basic` for HTTP basic auth with username and password or `Bearer` for bearer token auth.
Expand Down
1 change: 1 addition & 0 deletions base-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ method: "POST"
room: "!AAAAAAAAAAAAAAAAAA:example.com"
message: "Hello world!"
message_format: "plaintext"
message_type: "m.text"
auth_type:
auth_token:
force_json: false
Expand Down
16 changes: 12 additions & 4 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("room")
helper.copy("message")
helper.copy("message_format")
helper.copy("message_type")
helper.copy("auth_type")
helper.copy("auth_token")
helper.copy("force_json")
Expand All @@ -63,6 +64,12 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
if message_format not in valid_message_formats:
raise ValueError(f"Invalid message_format '{message_format}' specified! "
f"Must be one of: {', '.join(valid_message_formats)}")
# validate message_type
valid_message_types = {"m.text", "m.notice"}
message_type = helper.base["message_type"]
if message_type not in valid_message_types:
raise ValueError(f"Invalid message_type '{message_type}' specified! "
f"Must be one of: {', '.join(valid_message_types)}")

# validate auth_type and auth_token
valid_auth_types = {"Basic", "Bearer"}
Expand Down Expand Up @@ -208,14 +215,15 @@ def unauthorized(text: str) -> Response:
"but the template generated an empty message.")
return Response()

self.log.info(f"Sending message to room {room}: {message}")
msgtype = self.config["message_type"]
self.log.info(f"Sending message ({msgtype}) to room {room}: {message}")
try:
if self.config["message_format"] == 'markdown':
await self.client.send_markdown(room, message)
await self.client.send_markdown(room, message, msgtype=msgtype)
elif self.config["message_format"] == 'html':
await self.client.send_text(room, None, html=message)
await self.client.send_text(room, None, html=message, msgtype=msgtype)
else:
await self.client.send_text(room, message)
await self.client.send_text(room, message, msgtype=msgtype)
except Exception as e:
error_message = f"Failed to send message '{message}' to room {room}: {e}"
self.log.error(error_message)
Expand Down
Loading