# HG changeset patch # User Kim Alvefur # Date 1577898715 -3600 # Node ID d3757e089433a97427a14c705ee70d0a644b5c4f # Parent 1bab6f67eb5f73f4b16ed743fee4ba27150aeb91 mod_rest: Add a JSON callback example diff -r 1bab6f67eb5f -r d3757e089433 mod_rest/README.markdown --- 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