feat: add room to api config

This commit is contained in:
Alex Kelly 2021-11-09 08:57:46 -05:00
parent 778ebcdad9
commit e42aa941e5
4 changed files with 43 additions and 24 deletions

View file

@ -30,23 +30,32 @@ docker run --rm -it nim65s/matrix-webhook -h
``` ```
``` ```
usage: python -m matrix_webhook [-h] [-H HOST] [-P PORT] [-u MATRIX_URL] -i MATRIX_ID -p MATRIX_PW -k API_KEY [-v] usage: python -m matrix_webhook [-h] [-H HOST] [-P PORT] [-u MATRIX_URL]
[-i MATRIX_ID] [-p MATRIX_PW] [-k API_KEYS]
[-c CONFIG] [-v]
Configuration for Matrix Webhook. Configuration for Matrix Webhook.
options:
optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
-H HOST, --host HOST host to listen to. Default: `''`. Environment variable: `HOST` -H HOST, --host HOST host to listen to. Default: `''`. Environment
-P PORT, --port PORT port to listed to. Default: 4785. Environment variable: `PORT` variable: `HOST`
-P PORT, --port PORT port to listed to. Default: 4785. Environment
variable: `PORT`
-u MATRIX_URL, --matrix-url MATRIX_URL -u MATRIX_URL, --matrix-url MATRIX_URL
matrix homeserver url. Default: `https://matrix.org`. Environment variable: `MATRIX_URL` matrix homeserver url. Default: `https://matrix.org`.
Environment variable: `MATRIX_URL`
-i MATRIX_ID, --matrix-id MATRIX_ID -i MATRIX_ID, --matrix-id MATRIX_ID
matrix user-id. Required. Environment variable: `MATRIX_ID` matrix user-id. Required. Environment variable:
`MATRIX_ID`
-p MATRIX_PW, --matrix-pw MATRIX_PW -p MATRIX_PW, --matrix-pw MATRIX_PW
matrix password. Required. Environment variable: `MATRIX_PW` matrix password. Required. Environment variable:
-k API_KEY, --api-key API_KEY `MATRIX_PW`
shared secret to use this service. Required. Environment variable: `API_KEY` -k API_KEYS, --api-keys API_KEYS
comma separated list of shared secrets to use this
service. Required. Environment variable: `API_KEYS`
-c CONFIG, --config CONFIG
configuration file. Default: `config.yaml`
-v, --verbose increment verbosity level -v, --verbose increment verbosity level
``` ```
@ -86,7 +95,11 @@ Add a JSON webhook with `?formatter=github`, and put the `API_KEY` as secret
### For Grafana ### For Grafana
Add a webhook with an URL ending with `?formatter=grafana&key=API_KEY` Add a webhook with a URL ending with `?formatter=grafana&key=API_KEY`
### For Pingdom
Add a webhook with a URL ending with `?formatter=pingdom&key=API_KEY`
## Test room ## Test room

View file

@ -1,4 +1,4 @@
hostname: localhost host: localhost
port: 4785 port: 4785
# matrix-specific settings # matrix-specific settings
matrix: matrix:
@ -11,11 +11,9 @@ matrix:
# keys to allow These should be random strings # keys to allow These should be random strings
# these could be generated with something like `openssl rand -hex 24` # these could be generated with something like `openssl rand -hex 24`
# change these, you only need # change these, you only need
api: api_keys:
keys: RanomdTextForKey: "!room_id:server.domain" # Can add a comment for what the key is used
# change these keys. secondRandomkey: "!a_different_room_id:server.domain"
- d6a26923e41492d89cffd9e5b6d9838a89e71ffa061edb5d # Can add a comment for what the key is used thirdKey: #This one has no room specified, so it must be specified in the payload data or url
- 149d866b2591ceee2b254ca8b16c08fd27a7e2eed7ff01e0
# location of logfile
log: log:
level: debug level: debug

View file

@ -72,17 +72,16 @@ args = parser.parse_args()
if args.config: if args.config:
with open(args.config) as f: with open(args.config) as f:
config = yaml.safe_load(f) config = yaml.safe_load(f)
SERVER_ADDRESS = (config["hostname"], config["port"]) SERVER_ADDRESS = (config["host"], config["port"])
MATRIX_URL = config["matrix"]["url"] MATRIX_URL = config["matrix"]["url"]
MATRIX_ID = config["matrix"]["id"] MATRIX_ID = config["matrix"]["id"]
MATRIX_PW = config["matrix"]["pw"] MATRIX_PW = config["matrix"]["pw"]
API_KEYS = config["api_keys"] API_KEYS = config["api_keys"].keys()
LOG_FILE = config["log"] ROOM_KEYS = config["api_keys"]
VERBOSE = get_numeric_log_level(config["log"]["level"]) VERBOSE = get_numeric_log_level(config["log"]["level"])
else: else:
SERVER_ADDRESS = (args.host, args.port) SERVER_ADDRESS = (args.host, args.port)
MATRIX_URL = args.matrix_url MATRIX_URL = args.matrix_url
LOG_FILE = args.log
if not args.matrix_id: if not args.matrix_id:
print("Missing matrix user-id. Use -i or --matrix-id or specify in config.yaml") print("Missing matrix user-id. Use -i or --matrix-id or specify in config.yaml")
sys.exit(1) sys.exit(1)

View file

@ -37,14 +37,22 @@ async def matrix_webhook(request):
if "formatter" in request.rel_url.query: if "formatter" in request.rel_url.query:
try: try:
format = request.rel_url.query["formatter"] format_type = request.rel_url.query["formatter"]
plugin = importlib.import_module(f"matrix_webhook.formatters.{format}", "formatter") plugin = importlib.import_module(
f"matrix_webhook.formatters.{format_type}", "formatter"
)
data = plugin.formatter(data, request.headers) data = plugin.formatter(data, request.headers)
except ModuleNotFoundError: except ModuleNotFoundError:
return utils.create_json_response( return utils.create_json_response(
HTTPStatus.BAD_REQUEST, "Unknown formatter" HTTPStatus.BAD_REQUEST, "Unknown formatter"
) )
if (
"room_id" not in request.rel_url.query
and "room_id" not in data
and conf.ROOM_KEYS[f'{data["key"]}']
):
data["room_id"] = conf.ROOM_KEYS[f'{data["key"]}']
if "room_id" in request.rel_url.query and "room_id" not in data: if "room_id" in request.rel_url.query and "room_id" not in data:
data["room_id"] = request.rel_url.query["room_id"] data["room_id"] = request.rel_url.query["room_id"]
if "room_id" not in data: if "room_id" not in data:
@ -88,4 +96,5 @@ async def matrix_webhook(request):
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"formatted_body": formatted_body, "formatted_body": formatted_body,
} }
print(conf.ROOM_KEYS)
return await utils.send_room_message(data["room_id"], content) return await utils.send_room_message(data["room_id"], content)