2025-02-18 14:31:40 +01:00

66 lines
1.5 KiB
Python

#!/bin/env python3
from flask import Flask, request, jsonify
import websocket
import json
import yaml
import random
from pprint import pprint
__version__ = "2.0.0b2"
versionstring='Taurix TellMe server v' + __version__
app = Flask(__name__)
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
hooks = {}
with open(r'/etc/tellme/hooks.yml') as hooksfile:
hooks = yaml.load(hooksfile, Loader=yaml.FullLoader)
def sendmessage(target, message):
global socket
jsonmessage = {}
jsonmessage['target'] = target
jsonmessage['message'] = message
socket.send_string(json.dumps(jsonmessage))
# Wait for a reply
reply = socket.recv_string()
print(f"Received reply: {reply}")
if reply == 'sent':
return True
else:
return False
@app.route("/webhook/<id>", methods=['POST'])
def webhook_receiver(id):
print("Webhook id %s" % (id))
pprint(request.json)
data = request.json # Get the JSON data from the incoming request
# Process the data and perform actions based on the event
print("Received webhook data:", data)
target = None
for key, value in hooks.items():
if str(key) == str(id):
target = value
if target is not None:
print(target)
with lock:
sendmessage(target, data.get('message'))
else:
print("No target found, dropping message")
return jsonify({'message': 'Webhook received successfully'}), 200
if __name__ == '__main__':
print("Started %s" % (versionstring))
app.run()