
On Tue, Feb 12, 2019 at 01:49:38PM +0100, Michal Sojka wrote:
On Wed, Feb 06 2019, Jiri Vlasak wrote:
When clicked, the message notifying Tonda about the lack of coffee is sent to the Slack.
Problems to be solved with this patch were told to Jiri personally. In a nutshell, there is not error handling and if an error happens on the server, the terminal goes crazy (ends up in some infinite loop).
Error handling and docstring added, please see v4. Thanks! jiri
-M.
--- app.py | 19 +++++++++++++++++++ templates/hello.html | 1 + templates/main.js | 5 +++++ 3 files changed, 25 insertions(+)
diff --git a/app.py b/app.py index d66eff1..1d54d70 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,19 @@ 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")) + with open(".config", "r") as f: + conf = loads(f.read()) + 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
_______________________________________________ Coffee mailing list Coffee@rtime.felk.cvut.cz https://rtime.felk.cvut.cz/mailman/listinfo/coffee