Browse Source

Receiving a file on that endpoint now and trying to play it in audiobot

Kirk Trombley 5 years ago
parent
commit
a7593d357f
1 changed files with 12 additions and 12 deletions
  1. 12 12
      app.py

+ 12 - 12
app.py

@@ -1,11 +1,13 @@
 #!/usr/bin/env python3
 
+import tempfile
+import os.path
 from telnetlib import Telnet
 from collections import defaultdict
 from datetime import timedelta
 
 import toml
-from flask import Flask, jsonify, render_template
+from flask import Flask, jsonify, render_template, request
 from flask_cors import CORS
 
 IDLE_TIMEOUT = timedelta(minutes=5)
@@ -49,17 +51,8 @@ def post_to_channel(host, port, username, password, message):
     print(message_command.decode("utf-8"))
     with Telnet(host, port, 5) as tn:
         login(tn, username, password)
-
         tn.write(message_command)
-        response = tn.read_until(b"\n").decode("utf-8")
-        print("after sendtextmessage")
-        print(response)
-        print(tn.read_until(b"\n").decode("utf-8"))
-        print("----")
-
         tn.write(b"quit\n")
-        print("after quit")
-        print(tn.read_until(b"\n").decode("utf-8"))
 
 
 def query_ts(host, port, username, password):
@@ -148,14 +141,21 @@ def get_status_page():
     return render_template("embedded_body.html", users=get_users()[1], refresh_rate=REFRESH_RATE)
 
 
-@app.route("/audiobot")
+@app.route("/audiobot", methods=["POST"])
 def message_audiobot():
+    temp_dir = tempfile.mkdtemp()
+    temp_file = os.path.join(temp_dir, "received.mp3")
+
+    with open(temp_file, "wb") as f:
+        f.write(request.data)
+        f.close()
+
     post_to_channel(
         app.config["host"], 
         app.config["port"], 
         app.config["user"], 
         app.config["pass"],
-        "Hello, world!"
+        f"!play {temp_file}"
     )
 
     return "", 204