3853
|
1 from flask import Flask, Response, request, jsonify |
|
2 |
|
3 app = Flask("echobot") |
|
4 |
|
5 |
|
6 @app.route("/api", methods=["POST"]) |
|
7 def hello(): |
|
8 print(request.data) |
|
9 if request.is_json: |
|
10 data = request.get_json() |
|
11 |
|
12 if "kind" not in data: |
|
13 return Response(status=400) |
|
14 |
|
15 if data["kind"] == "message" and "body" in data: |
|
16 return jsonify({"body": "Yes this is flask app"}) |
|
17 |
|
18 elif data["kind"] == "iq" and data["type"] == "get": |
|
19 if "ping" in data: |
|
20 return Response(status=204) |
|
21 |
|
22 elif "disco" in data: |
|
23 return jsonify( |
|
24 { |
|
25 "disco": { |
|
26 "identities": [ |
|
27 { |
|
28 "category": "component", |
|
29 "type": "generic", |
|
30 "name": "Flask app", |
|
31 } |
|
32 ], |
|
33 "features": [ |
|
34 "http://jabber.org/protocol/disco#info", |
|
35 "http://jabber.org/protocol/disco#items", |
|
36 "urn:xmpp:ping", |
|
37 ], |
|
38 } |
|
39 } |
|
40 ) |
|
41 |
|
42 elif "items" in data: |
|
43 return jsonify( |
|
44 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]} |
|
45 ) |
|
46 |
|
47 return Response(status=501) |
|
48 |
|
49 |
|
50 if __name__ == "__main__": |
|
51 app.run() |