76 lines
1.5 KiB
Django/Jinja
76 lines
1.5 KiB
Django/Jinja
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import sys
|
|
import json
|
|
from socket import gethostname
|
|
|
|
import requests
|
|
|
|
TOKEN = "{{ matrix_webhook_token }}"
|
|
|
|
{% if matrix_webhook_url is defined %}
|
|
URL = "{{ matrix_webhook_url }}"
|
|
{% else %}
|
|
URL = "https://{{ matrix_url }}/_webhook/incoming"
|
|
{% endif %}
|
|
|
|
def markdown_fmt(lines):
|
|
return "\r\n".join(
|
|
[f" {a}" for a in lines]
|
|
)
|
|
|
|
|
|
def send(text, room=None, add_prefix=True):
|
|
if add_prefix:
|
|
prefix = gethostname()
|
|
else:
|
|
prefix = None
|
|
|
|
params = {'room': room}
|
|
d = {
|
|
'prefix': prefix,
|
|
'text': text,
|
|
'token': TOKEN
|
|
}
|
|
|
|
r = requests.post(URL, json=d, params=params)
|
|
|
|
if r.status_code == 502:
|
|
print("502 bad gateway")
|
|
return 2
|
|
|
|
j = r.json()
|
|
# print(j)
|
|
if r.status_code == 200:
|
|
return 0
|
|
else:
|
|
return 1
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
parser.add_argument(
|
|
"-m", "--msg",
|
|
type=str,
|
|
required=False,
|
|
help="otherwise defaults to reading from stdin"
|
|
)
|
|
parser.add_argument(
|
|
"-r", "--room",
|
|
type=str,
|
|
help="override default room (#room:{{ domain }})"
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if args.msg is None:
|
|
lines = sys.stdin.readlines()
|
|
msg = markdown_fmt(lines)
|
|
else:
|
|
msg = args.msg
|
|
|
|
rc = send(msg, args.room)
|
|
sys.exit(rc)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|