--- a/mod_rest/README.markdown Wed Jan 01 17:45:07 2020 +0100
+++ b/mod_rest/README.markdown Wed Jan 01 18:11:55 2020 +0100
@@ -167,7 +167,7 @@
## Python / Flask
-Simple echo bot that responds to messages:
+Simple echo bot that responds to messages as XML:
``` {.python}
from flask import Flask, Response, request
@@ -196,6 +196,32 @@
app.run()
```
+And a JSON variant:
+
+``` {.python}
+from flask import Flask, Response, request, jsonify
+
+app = Flask("echobot")
+
+
+@app.route("/", methods=["POST"])
+def hello():
+ print(request.data)
+ if request.is_json:
+ data = request.get_json()
+ if data["kind"] == "message":
+ return jsonify({"body": "hello"})
+
+ return Response(status=501)
+
+
+if __name__ == "__main__":
+ app.run()
+```
+
+Remember to set `rest_callback_content_type = "application/json"` for
+this to work.
+
# Compatibility
Requires Prosody trunk / 0.12