]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Add last coffee pack button
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Tue, 12 Feb 2019 13:15:35 +0000 (14:15 +0100)
committerMichal Sojka <michal.sojka@cvut.cz>
Tue, 12 Feb 2019 13:29:21 +0000 (14:29 +0100)
When clicked, the message notifying Tonda about the lack of coffee is
sent to the Slack.

There must be `.config` file in `coffee-flask` folder. The `.config`
file is JSON formated. For this patch the following is needed:
```
{
        "coffeebot": {
                "url": "https://hooks.slack.com/services/..."
        }
}
```

app.py
templates/hello.html
templates/main.js

diff --git a/app.py b/app.py
index d66eff1219ff9bf2d68b7ce2d162436676242417..389aa2828c1e3017529a85de1772358ce3129425 100644 (file)
--- a/app.py
+++ b/app.py
@@ -12,6 +12,9 @@ import coffee_db as db
 import time
 from datetime import date, timedelta
 
+from json import loads
+from requests import post
+
 db.init_db()
 app = Flask(__name__)
 CORS(app, supports_credentials=True)
@@ -186,3 +189,22 @@ def log():
         print("Log:", data)
         return data
     return "nope"
+
+@app.route("/tellCoffeebot", methods=["POST"])
+def tell_coffeebot():
+    err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"
+    if request.method == "POST":
+        what = loads(request.data.decode("utf-8"))
+    try:
+        with open(".config", "r") as f:
+            conf = loads(f.read())
+    except:
+        return "Config needed! Please find in git history how it should look."
+    try:
+        res = post(conf["coffeebot"]["url"], json=what)
+        print("res is {}".format(res))
+    except:
+        err = "No connection! No covfefe! We all die here!"
+    if not res.ok:
+        err = "Slack don't like the request! It's discrimination!"
+    return err
index 0e709ce32731222deebc99f82fc2d6128fa795dd..f6247da85f8005184bcf16e1ff92b9d0dd2bdfd9 100644 (file)
@@ -1,5 +1,6 @@
 <center>
 <div id="user"></div>
+<p id="pLastCovfefe"><input type="button" value="The last coffee pack opened!" onclick="tellCoffeebot('Yay, <@U539N17JL|Tonda>, buy me a new covfefe pack! Thanks!')" /></p>
 <p>Hack me: <tt>https://rtime.felk.cvut.cz/gitweb/coffee/main.git</tt></p>
 <p>Debug: <span id="log"></span></p>
 </center>
index 8135983b75c8bdbee3570c81e2738047564b3867..b28b8263137c036ea8e5d9a3dfb4731913fd20a3 100644 (file)
@@ -205,3 +205,8 @@ function addCoffee(flavor, time = new Date()) {
 function sendLog(json) {
     ajax("POST", "log", json, "log");
 }
+
+function tellCoffeebot(what)
+{
+    ajax("POST", "tellCoffeebot", JSON.stringify({text: what}), "log");
+}