Browse Source

Added functionality to send a message to the default channel

Kirk Trombley 5 years ago
parent
commit
5ffc059625
1 changed files with 56 additions and 14 deletions
  1. 56 14
      app.py

+ 56 - 14
app.py

@@ -24,24 +24,47 @@ def parse_ts_response(response):
     return [{k: v for k, v in pr} for pr in pairs]
 
 
-def query_ts(host, port, username, password):
+def login(tn, username, password):
     login = f"login {username} {password}\n".encode("utf-8")
 
+    print("connection")
+    print(tn.read_until(b"\n").decode("utf-8"))
+    print(tn.read_until(b"\n").decode("utf-8"))
+    print("----")
+
+    tn.write(login)
+    print("after login")
+    print(tn.read_until(b"\n").decode("utf-8"))
+    print("----")
+
+    tn.write(b"use 1 -virtual\n")
+    print("after use")
+    print(tn.read_until(b"\n").decode("utf-8"))
+    print("----")
+
+
+def post_to_channel(host, port, username, password, message):
+    message_santized = message.strip().replace(" ", r"\s")
+    message_command = f"sendtextmessage targetmode=2 msg={message_santized}\n".encode("utf-8")
+    print(message_command.decode("utf-8"))
     with Telnet(host, port, 5) as tn:
-        print("connection")
-        print(tn.read_until(b"\n").decode("utf-8"))
-        print(tn.read_until(b"\n").decode("utf-8"))
-        print("----")
+        login(tn, username, password)
 
-        tn.write(login)
-        print("after login")
+        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"use 1 -virtual\n")
-        print("after use")
+        tn.write(b"quit\n")
+        print("after quit")
         print(tn.read_until(b"\n").decode("utf-8"))
-        print("----")
+
+
+def query_ts(host, port, username, password):
+    with Telnet(host, port, 5) as tn:
+        login(tn, username, password)
 
         tn.write(b"channellist\n")
         response = tn.read_until(b"\n").decode("utf-8")
@@ -86,10 +109,12 @@ def query_ts(host, port, username, password):
 
 
 def get_users():
-    with open("secret.toml") as infile:
-        cfg = toml.load(infile)
-
-    client_info, channels = query_ts(cfg["host"], cfg["port"], cfg["user"], cfg["pass"])
+    client_info, channels = query_ts(
+        app.config["host"], 
+        app.config["port"], 
+        app.config["user"], 
+        app.config["pass"]
+    )
 
     users = []
     channel_users = defaultdict(list)
@@ -123,5 +148,22 @@ def get_status_page():
     return render_template("embedded_body.html", users=get_users()[1], refresh_rate=REFRESH_RATE)
 
 
+@app.route("/audiobot")
+def message_audiobot():
+    post_to_channel(
+        app.config["host"], 
+        app.config["port"], 
+        app.config["user"], 
+        app.config["pass"],
+        "Hello, world!"
+    )
+
+    return "", 204
+
+
 if __name__ == "__main__":
+    with open("secret.toml") as infile:
+        cfg = toml.load(infile)
+    for k in ("host", "port", "user", "pass"):
+        app.config[k] = cfg[k]
     app.run("0.0.0.0", 5000, debug=True, threaded=True)