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