From 9295194c81dbafbf418201d41058d61287506378 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Mon, 27 Sep 2021 17:42:01 +0200 Subject: [PATCH] Initial based on telegram-http-gateway --- .gitignore | 2 ++ README.md | 34 ++++++++++++++++++++++ interface.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ req.txt | 2 ++ 4 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 interface.py create mode 100644 req.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b37df17 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +signal_targets.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..51526d8 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# HTTP->Telegram Gateway Notification Service +Simplistic server to listing for HTTP queries, specifically from Icinga or Grafana and send out Signal-Messages. + +# Signal Setup +- Setup signal cli +- add the target number(s) (one per line) to signal\_targets.txt + +# Server Setup + + usage: interface.py [-h] [--interface INTERFACE] + [--port PORT] + [--signal-cli-bin SIGNAL_CLI_BIN] + + optional arguments: + -h, --help show this help message and exit + --interface INTERFACE + Interface on which to listen (default: localhost) + --port PORT Port on which to listen (default: 5000) + --signal-cli-bin SIGNAL_CLI_BIN + Path to signal-cli binary if no in $PATH (default: None) + +# HTTP Request +The HTTP request must be a *POST*-request, with *Content-Type: application/json* and a json-field containing the key *"message"* with the value being the message you want to send. + +The following locations are supported: + + /send-all # send a message to all subscribed clients + /send-all-icinga # send a message based on icinga-noficiation format + +# Example (curl) + + curl -X POST -H "Content-Type: application/json" --data '{"message":"hallo world"}' localhost:5000/send-all + + diff --git a/interface.py b/interface.py new file mode 100755 index 0000000..8e0bed9 --- /dev/null +++ b/interface.py @@ -0,0 +1,81 @@ +#!/usr/bin/python3 + +import argparse +import flask +import subprocess +import os + +HOST = "icinga.atlantishq.de" +SIGNAL_USER_FILE = "signal_targets.txt" +app = flask.Flask("Signal Notification Gateway") + +def dbReadSignalUserFile(): + users = [] + with open(SIGNAL_USER_FILE, "r") as f: + for line in f: + user = line.strip() + if user: + users.append(user) + return users + +def signalSend(user, msg): + signalCliBin = "signal-cli" + if app.config["SIGNAL_CLI_BIN"]: + signalCliBin = app.config["SIGNAL_CLI_BIN"] + cmd = [signalCliBin, "send", "-m", msg, user] + subprocess.Popen(cmd) + +def sendMessageToAllClients(msg): + for number in dbReadSignalUserFile(): + signalSend(number, msg) + +@app.route('/send-all', methods=["POST"]) +def sendToAll(): + sendMessageToAllClients(flask.request.json["message"]) + return ("","204") + +@app.route('/send-all-grafana', methods=["POST"]) +def sendToAllGrafana(): + j = flask.request.json + state = j["state"] + if state == "ok": + string = "Humidity: Ok (<50%)" + else: + string = "Humidity: Warning (>50%)" + + sendMessageToAllClients(string) + return ("","204") + +@app.route('/send-all-icinga', methods=["POST"]) +def sendToAllIcinga(): + args = flask.request.json + + for key in args.keys(): + if type(args[key]) == str: + print(key) + + # build message # + serviceName = args["service_name"] + if args["service_display_name"]: + serviceName = args["service_display_name"] + + message = "{service} {state}\n{host}\n{output}".format(service=serviceName, + state=args["service_state"], + host=args["service_host"], + output=args["service_output"]) + sendMessageToAllClients(message) + return ("","204") + +if __name__ == "__main__": + + parser = argparse.ArgumentParser(description='Simple Telegram Notification Interface', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument('--interface', default="localhost", help='Interface on which to listen') + parser.add_argument('--port', default="5000", help='Port on which to listen') + parser.add_argument("--signal-cli-bin", default=None, type=str, + help="Path to signal-cli binary if no in $PATH") + args = parser.parse_args() + + app.config["SIGNAL_CLI_BIN"] = os.path.expanduser(args.signal_cli_bin) + + app.run(host=args.interface, port=args.port) diff --git a/req.txt b/req.txt new file mode 100644 index 0000000..3db7ef1 --- /dev/null +++ b/req.txt @@ -0,0 +1,2 @@ +python-telegram-bot +flask