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.
optional arguments:
options:
-h, --help show this help message and exit
-H HOST, --host HOST host to listen to. Default: `''`. Environment variable: `HOST`
-P PORT, --port PORT port to listed to. Default: 4785. Environment variable: `PORT`
-H HOST, --host HOST host to listen to. Default: `''`. Environment
variable: `HOST`
-P PORT, --port PORT port to listed to. Default: 4785. Environment
variable: `PORT`
-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
matrix user-id. Required. Environment variable: `MATRIX_ID`
matrix user-id. Required. Environment variable:
`MATRIX_ID`
-p MATRIX_PW, --matrix-pw MATRIX_PW
matrix password. Required. Environment variable: `MATRIX_PW`
-k API_KEY, --api-key API_KEY
shared secret to use this service. Required. Environment variable: `API_KEY`
matrix password. Required. Environment variable:
`MATRIX_PW`
-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
```
@ -86,7 +95,11 @@ Add a JSON webhook with `?formatter=github`, and put the `API_KEY` as secret
### 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

View file

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

View file

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

View file

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