66 lines
1.5 KiB
Python
Raw Normal View History

2025-01-30 15:02:01 +01:00
#!/bin/env python3
from flask import Flask, request, jsonify
import websocket
import json
2025-01-30 15:47:14 +01:00
import yaml
2025-01-30 15:02:01 +01:00
import random
2025-02-12 11:22:50 +01:00
from pprint import pprint
2025-01-30 15:02:01 +01:00
2025-02-12 11:09:21 +01:00
__version__ = "2.0.0b2"
2025-02-04 14:16:20 +01:00
versionstring='Taurix TellMe server v' + __version__
2025-02-04 13:51:00 +01:00
2025-01-30 15:02:01 +01:00
app = Flask(__name__)
2025-02-12 11:43:23 +01:00
2025-02-18 14:31:40 +01:00
context = zmq.Context()
2025-02-18 14:27:46 +01:00
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
2025-01-30 15:02:01 +01:00
2025-01-30 15:40:24 +01:00
hooks = {}
2025-02-04 13:43:24 +01:00
with open(r'/etc/tellme/hooks.yml') as hooksfile:
2025-01-30 15:47:14 +01:00
hooks = yaml.load(hooksfile, Loader=yaml.FullLoader)
2025-01-30 15:40:24 +01:00
2025-02-12 11:32:41 +01:00
2025-02-18 14:27:46 +01:00
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
2025-02-12 11:32:41 +01:00
2025-01-30 15:02:01 +01:00
2025-01-30 15:40:24 +01:00
@app.route("/webhook/<id>", methods=['POST'])
def webhook_receiver(id):
print("Webhook id %s" % (id))
2025-02-12 12:21:57 +01:00
pprint(request.json)
2025-01-30 15:02:01 +01:00
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)
2025-02-04 17:41:05 +01:00
target = None
for key, value in hooks.items():
2025-02-04 17:49:19 +01:00
if str(key) == str(id):
2025-02-04 17:41:05 +01:00
target = value
if target is not None:
print(target)
2025-02-12 11:11:58 +01:00
with lock:
sendmessage(target, data.get('message'))
2025-02-04 17:49:19 +01:00
else:
print("No target found, dropping message")
2025-01-30 15:40:24 +01:00
2025-01-30 15:02:01 +01:00
return jsonify({'message': 'Webhook received successfully'}), 200
2025-01-30 15:40:24 +01:00
2025-01-30 15:02:01 +01:00
if __name__ == '__main__':
2025-02-04 14:16:20 +01:00
print("Started %s" % (versionstring))
2025-02-04 19:15:31 +01:00
app.run()