-
-
Save hyperanalysis/f76498ce3a2b865599c7cffc62f6f3e2 to your computer and use it in GitHub Desktop.
Simple script to send notifications from Tautulli to Matrix (Riot.im)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import sys | |
| import requests | |
| import json | |
| import markdown | |
| import urllib | |
| MATRIX_SERVER = 'https://matrix.org' #replace with custom server if running own node | |
| MATRIX_PORT = '8448' #default management port | |
| MATRIX_USER = 'USERNAME' | |
| MATRIX_PASSWORD = 'PASSWORD' | |
| MATRIX_ROOM = 'ROOM_ID' #i.e. 'exampleRoom:matrix.org' | |
| if (len(sys.argv) <= 1): | |
| print("No message body entered") | |
| exit(1) | |
| #Tautulli sends arguments as multiple parts instead of single string, so need to concatenate | |
| if (len(sys.argv) > 1): | |
| MESSAGE_BODY = '' | |
| print(sys.argv) | |
| for item in sys.argv: | |
| if (item == sys.argv[0]): | |
| continue | |
| MESSAGE_BODY = MESSAGE_BODY + item + ' ' | |
| MESSAGE_BODY = MESSAGE_BODY.decode('utf-8') | |
| MATRIX_CONNECTION = MATRIX_SERVER + ':' + MATRIX_PORT | |
| #Get expected login type | |
| LOGIN_URL = MATRIX_CONNECTION + '/_matrix/client/r0/login' | |
| response = requests.get(LOGIN_URL, verify=False) | |
| data = json.loads(response.text) | |
| LOGIN_TYPE = data['flows'][0]['type'] | |
| if (LOGIN_TYPE != 'm.login.password'): | |
| print('Unexpected login type found!') | |
| exit(1) | |
| #Login and get an access token to search instance and post messages | |
| data = {"type":LOGIN_TYPE, "user":MATRIX_USER, "password":MATRIX_PASSWORD} | |
| response = requests.post(LOGIN_URL, data=json.dumps(data), verify=False) | |
| data = json.loads(response.text) | |
| ACCESS_TOKEN = data['access_token'] | |
| #Get room ID for specified room to allow for message delivery | |
| GET_ROOM_URL = MATRIX_CONNECTION + '/_matrix/client/r0/directory/room/' + urllib.quote(MATRIX_ROOM) | |
| response = requests.get(GET_ROOM_URL, verify=False) | |
| data = json.loads(response.text) | |
| ROOM_ID = data['room_id'] | |
| #Converts markdown text to html and removes head and tail <p> tags | |
| FORMATTED_BODY = markdown.markdown(MESSAGE_BODY) | |
| if (FORMATTED_BODY.startswith('<p>') and FORMATTED_BODY.endswith('</p>')): | |
| FORMATTED_BODY = FORMATTED_BODY[3:-4] | |
| FORMATTED_BODY = FORMATTED_BODY.replace('\\n','<br />') | |
| #If html and plaintext versions are the same send a standard message for backwards compatability | |
| if (MESSAGE_BODY == FORMATTED_BODY): | |
| MESSAGE_DATA = {"msgtype":"m.text", "body":MESSAGE_BODY} | |
| else: | |
| MESSAGE_DATA = {"msgtype":"m.text", "format":"org.matrix.custom.html", "body":MESSAGE_BODY, "formatted_body":FORMATTED_BODY} | |
| MESSAGE_URL = MATRIX_CONNECTION + '/_matrix/client/r0/rooms/' + ROOM_ID + '/send/m.room.message?access_token=' + ACCESS_TOKEN | |
| response = requests.post(MESSAGE_URL, data=json.dumps(MESSAGE_DATA), verify=False) | |
| #Logs out of current session, ensuring no access tokens remain valid | |
| LOGOUT_URL = MATRIX_CONNECTION + '/_matrix/client/r0/logout?access_token=' + ACCESS_TOKEN | |
| response = requests.post(LOGOUT_URL, verify=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment