Introduced transport and restructured hooks file

This commit is contained in:
2026-05-02 12:49:40 +02:00
parent 342da10dbb
commit aaf516bcd5

View File

@@ -6,7 +6,6 @@ import yaml
import random import random
import logging import logging
import os import os
import os.path
from pprint import pprint from pprint import pprint
__version__ = "2.2.0" __version__ = "2.2.0"
@@ -33,7 +32,7 @@ config = {}
def read_configs(): def read_configs():
global hooks global hooks, config
with open(r'/etc/tellme/hooks.yml') as hooksfile: with open(r'/etc/tellme/hooks.yml') as hooksfile:
hooks = yaml.load(hooksfile, Loader=yaml.FullLoader) hooks = yaml.load(hooksfile, Loader=yaml.FullLoader)
@@ -71,13 +70,8 @@ def send_smp_message(target, message):
return False return False
def get(hook_id) def get_hook(hook_id):
target = None return hooks.get(str(hook_id))
for key, value in hooks.items():
if str(key) == str(id):
target = value
return target
@app.route("/webhook/<id>", methods=['POST']) @app.route("/webhook/<id>", methods=['POST'])
@@ -101,7 +95,7 @@ def webhook_receiver(id):
type = data.get('Type') type = data.get('Type')
if type == 'Verification': if type == 'Verification':
message = ("GoAlert verification code: %s" % (data.get('Code`'))) message = ("GoAlert verification code: %s" % (data.get('Code')))
if type == 'Alert': if type == 'Alert':
message = ("Alert %s: %s\n%s" % (data.get('AlertID'), data.get('Summary'), data.get('Details'))) message = ("Alert %s: %s\n%s" % (data.get('AlertID'), data.get('Summary'), data.get('Details')))
@@ -111,7 +105,14 @@ def webhook_receiver(id):
hook = get_hook(id) hook = get_hook(id)
if hook is None:
return jsonify({'message': 'Hook not found'}), 404
transport = hook.get('transport')
target = hook.get('target') target = hook.get('target')
if transport is not None:
if transport == 'simplex':
if target is not None: if target is not None:
log.info(target) log.info(target)
if message is not None: if message is not None:
@@ -120,6 +121,10 @@ def webhook_receiver(id):
log.error("No message, dropping") log.error("No message, dropping")
else: else:
log.error("No target found, dropping message") log.error("No target found, dropping message")
return jsonify({'message': 'No target found, dropping message'}), 400
else:
log.error("No transport found, dropping message")
return jsonify({'message': 'No transport found, dropping message'}), 400
return jsonify({'message': 'Webhook received successfully'}), 200 return jsonify({'message': 'Webhook received successfully'}), 200