mirror of
https://codeberg.org/hyperreal/bin
synced 2024-11-25 10:23:42 +01:00
50 lines
1.5 KiB
Python
Executable File
50 lines
1.5 KiB
Python
Executable File
#!/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)
|