tellme/server/src/notifier.py

73 lines
2.0 KiB
Python
Raw Normal View History

2025-02-18 14:27:46 +01:00
#!/bin/env python3
import websocket
import json
import random
import zmq
from pprint import pprint
__version__ = "2.0.0b2"
ws = None
uri = "ws://localhost:5080"
ws = websocket.create_connection(uri)
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
def sendsmpmessage(target, message):
global ws
print("Sendmessage %s called to %s" % (message, target))
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 """
if ws is None:
uri = "ws://localhost:5080"
ws = websocket.create_connection(uri) # Blocking WebSocket connection
ws.send(json_command) # Send message to WebSocket
responsejson = ws.recv() # Receive response
response = json.loads(responsejson)
# ws.close()
for chatitem in response['resp']['chatItems']:
statusdict = chatitem['chatItem']['meta']['itemStatus']
try:
status = statusdict['sndProgress']
except KeyError:
print("FAILED")
status = 'FAILED'
else:
smessage = chatitem['chatItem']['meta']['itemText']
print("Message '%s' sent over websocket to %s, status was %s" % (smessage, target, status))
2025-02-18 14:38:20 +01:00
if status == 'complete' or status == 'partial':
2025-02-18 14:27:46 +01:00
return True
else:
return False
if __name__ == '__main__':
while True:
2025-02-18 14:35:27 +01:00
zmqmessage = socket.recv_string()
2025-02-18 14:27:46 +01:00
print(f"Received request: {zmqmessage}")
result = True
decoded = json.loads(zmqmessage)
pprint(decoded)
2025-02-18 14:36:10 +01:00
result = sendsmpmessage(decoded.get('target'), decoded.get('message'))
2025-02-18 14:38:20 +01:00
print(result)
2025-02-18 14:27:46 +01:00
if result is True:
socket.send_string("sent")
else:
socket.send_string("failed")