Compare commits
13 Commits
040bd08503
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b95b776418 | |||
| b6e11f148f | |||
| a9e4febaa5 | |||
| 25b17f93fc | |||
| e9f57dd3f9 | |||
| 845c937d80 | |||
| ecb2429a0d | |||
| 5509f5e177 | |||
| 73592dd53e | |||
| f92a44b5c1 | |||
| a969b950d1 | |||
| 6bedd576be | |||
| fbfc7d9992 |
80
server/src/notifier.py
Normal file
80
server/src/notifier.py
Normal file
@@ -0,0 +1,80 @@
|
||||
#!/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()
|
||||
if response is not None:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
# for chatitem in response['resp']['chatItems']:
|
||||
# statusdict = chatitem['chatItem']['meta']['itemStatus']
|
||||
# try:
|
||||
# status = statusdict['sndProgress']
|
||||
# except KeyError:
|
||||
# pprint(response['resp']['chatItems'])
|
||||
# print('---')
|
||||
# pprint(chatitem)
|
||||
# print("FAILED")
|
||||
# status = 'FAILED'
|
||||
# else:
|
||||
# smessage = chatitem['chatItem']['meta']['itemText']
|
||||
# print("Message '%s' sent over websocket to %s, status was %s" % (smessage, target, status))
|
||||
|
||||
# if status == 'complete' or status == 'partial':
|
||||
# return True
|
||||
# else:
|
||||
# return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Tellme Notifier started...")
|
||||
while True:
|
||||
zmqmessage = socket.recv_string()
|
||||
print(f"Received request: {zmqmessage}")
|
||||
result = True
|
||||
|
||||
decoded = json.loads(zmqmessage)
|
||||
pprint(decoded)
|
||||
|
||||
result = sendsmpmessage(decoded.get('target'), decoded.get('message'))
|
||||
print(result)
|
||||
|
||||
if result is True:
|
||||
socket.send_string("sent")
|
||||
else:
|
||||
socket.send_string("failed")
|
||||
@@ -3,61 +3,38 @@ from flask import Flask, request, jsonify
|
||||
import websocket
|
||||
import json
|
||||
import yaml
|
||||
import random
|
||||
import threading
|
||||
import zmq
|
||||
from pprint import pprint
|
||||
|
||||
__version__ = "2.0.0b2"
|
||||
versionstring='Taurix TellMe server v' + __version__
|
||||
|
||||
app = Flask(__name__)
|
||||
lock = threading.Lock()
|
||||
|
||||
ws = None
|
||||
uri = "ws://localhost:5080"
|
||||
ws = websocket.create_connection(uri)
|
||||
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 ws
|
||||
print("Sendmessage %s called to %s" % (message, target))
|
||||
msg = ("%s %s" % (target, message))
|
||||
global socket
|
||||
jsonmessage = {}
|
||||
jsonmessage['target'] = target
|
||||
jsonmessage['message'] = message
|
||||
socket.send_string(json.dumps(jsonmessage))
|
||||
|
||||
# Create a unique correlation ID
|
||||
command = {
|
||||
"corrId": f"id{random.randint(0, 999999)}",
|
||||
"cmd": msg,
|
||||
}
|
||||
json_command = json.dumps(command)
|
||||
# Wait for a reply
|
||||
reply = socket.recv_string()
|
||||
print(f"Received reply: {reply}")
|
||||
|
||||
""" 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))
|
||||
|
||||
if status == 'complete':
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
if reply == 'sent':
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
@app.route("/webhook/<id>", methods=['POST'])
|
||||
@@ -75,8 +52,10 @@ def webhook_receiver(id):
|
||||
|
||||
if target is not None:
|
||||
print(target)
|
||||
with lock:
|
||||
if data.get('message') is not None:
|
||||
sendmessage(target, data.get('message'))
|
||||
else:
|
||||
print("No message, droppint")
|
||||
else:
|
||||
print("No target found, dropping message")
|
||||
|
||||
|
||||
12
server/tellme_notifier.service
Normal file
12
server/tellme_notifier.service
Normal file
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Tellme Notifier
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=tellme
|
||||
WorkingDirectory=/opt/tellme
|
||||
ExecStart=python3 notifier.py
|
||||
Restart=always
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user