Refactor and fix

This commit is contained in:
Midgard 2019-09-03 01:05:40 +02:00
parent c3524a6536
commit cf9b0e03cc

25
chat.py
View file

@ -77,7 +77,7 @@ def messages_post():
if (time-last_sent_from_ip).total_seconds() < timeout: if (time-last_sent_from_ip).total_seconds() < timeout:
return "OK Felix" return "OK Felix"
message = request.data message = request.data.decode()
if len(message) > 200: if len(message) > 200:
return "Message too long, maximum 200" return "Message too long, maximum 200"
@ -85,23 +85,26 @@ def messages_post():
if not message: if not message:
return "No message found" return "No message found"
message_str = message.decode() if "spam" in message.lower():
if "spam" in message_str.lower():
messages.append(Message(time, "1.3.3.7", "ip", "Nee")) messages.append(Message(time, "1.3.3.7", "ip", "Nee"))
return "OK" return "OK"
if sender != "somebody":
message_str = "<{}> {}".format(sender, message_str)
messages.append(Message(time, sender, sendertype, message_str)) # Save message to serve it in this application
url = 'http://10.0.5.42:8000/' # Set destination URL here messages.append(Message(time, sender, sendertype, message))
post_fields = {'X-Messages': base64.b64encode(message)}
# Send message to espeak to speak it out loud
speak(message.encode())
# Send message to MessageOS
msg_with_sendr: str = "<{}> {}".format(sender, message) if sender != "somebody" else message
msg_b64 = base64.b64encode(msg_with_sendr.encode())
url = "http://10.0.5.42:8000/" # Set destination URL here
post_fields = {"X-Messages": msg_b64}
assembly_request = urllib.request.Request(url, urlencode(post_fields).encode()) assembly_request = urllib.request.Request(url, urlencode(post_fields).encode())
assembly_request.add_header('X-Messages', base64.b64encode(message)) assembly_request.add_header("X-Messages", msg_b64)
urllib.request.urlopen(assembly_request).read().decode() urllib.request.urlopen(assembly_request).read().decode()
speak(message)
return "OK" return "OK"