bin/send-matrix-webhook

50 lines
1.5 KiB
Plaintext
Raw Normal View History

2023-12-31 04:04:27 +01:00
#!/usr/bin/env python
import json
import sys
import requests
def send_matrix_webhook(access_token, room_id, message, user_id_to_ping, matrix_server):
"""
Send a message to a Matrix room using a webhook.
:param access_token: The access token for Matrix API authentication.
:param room_id: The ID of the room where the message will be sent.
:param message: The message to be sent.
:param user_id_to_ping: The user to ping in the message.
:param matrix_server: The URL of the Matrix homeserver.
"""
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
full_message = f"{user_id_to_ping}: {message}"
data = {
"msgtype": "m.text",
"body": full_message,
"format": "org.matrix.custom.html",
"formatted_body": f'<a href="https://matrix.to/#/{user_id_to_ping}">{user_id_to_ping}</a>: {message}',
}
url = f"{matrix_server}/_matrix/client/r0/rooms/{room_id}/send/m.room.message"
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
print("Message sent successfully.")
else:
print(f"Failed to send message. Status code: {response.status_code}")
if __name__ == "__main__":
access_token = ""
room_id = sys.argv[1]
message = sys.argv[2]
user_id_to_ping = ""
matrix_server = ""
send_matrix_webhook(access_token, room_id, message, user_id_to_ping, matrix_server)