Very simple working server
This commit is contained in:
45
server/src/tellmesrv.py
Normal file
45
server/src/tellmesrv.py
Normal file
@@ -0,0 +1,45 @@
|
||||
#!/bin/env python3
|
||||
from flask import Flask, request, jsonify
|
||||
# import asyncio
|
||||
import websocket
|
||||
import json
|
||||
import random
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
def sendmessage(target, message):
|
||||
print("Sendmessage called")
|
||||
uri = "ws://localhost:5080"
|
||||
#message = "#Bottest Hello, world!"
|
||||
msg = ("%s %s" % (target, message))
|
||||
|
||||
# Create a unique correlation ID
|
||||
command = {
|
||||
"corrId": f"id{random.randint(0, 999999)}",
|
||||
"cmd": msg,
|
||||
}
|
||||
json_command = json.dumps(command)
|
||||
|
||||
""" Connects to WebSocket server, sends a message, and returns the response """
|
||||
ws = websocket.create_connection(uri) # Blocking WebSocket connection
|
||||
ws.send(json_command) # Send message to WebSocket
|
||||
response = ws.recv() # Receive response
|
||||
ws.close() # Close WebSocket connection
|
||||
print(response)
|
||||
return response
|
||||
|
||||
@app.route('/webhook', methods=['POST'])
|
||||
def webhook_receiver():
|
||||
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)
|
||||
return jsonify({'message': 'Webhook received successfully'}), 200
|
||||
|
||||
@app.route('/webhook', methods=['GET'])
|
||||
def webhook_test():
|
||||
print("Sending test")
|
||||
sendmessage('#Bottest', 'Testing 123')
|
||||
return jsonify({'message': 'Webhook received successfully'}), 200
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user