[PATCH v4] Add last coffee pack button

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 | 26 ++++++++++++++++++++++++++ templates/hello.html | 1 + templates/main.js | 5 +++++ 3 files changed, 32 insertions(+) diff --git a/app.py b/app.py index d66eff1..4ef7ca3 100644 --- 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,26 @@ def log(): print("Log:", data) return data return "nope" + +@app.route("/tellCoffeebot", methods=["POST"]) +def tell_coffeebot(): + """Send message to Slack notifying Tonda about the lack of coffee. + + Returned string is shown as log message in ``Debug:`` paragraph. + """ + 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 diff --git a/templates/hello.html b/templates/hello.html index 0e709ce..f6247da 100644 --- a/templates/hello.html +++ b/templates/hello.html @@ -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> diff --git a/templates/main.js b/templates/main.js index 8135983..b28b826 100644 --- a/templates/main.js +++ b/templates/main.js @@ -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"); +} -- 2.11.0
participants (1)
-
Jiri Vlasak