-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviews.py
68 lines (56 loc) · 1.97 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""
Views for handling the incoming HTTP requests to the app.
"""
from flask import request, abort, render_template
from flask_restful import Resource
from bot import ChatBot
from conf import SECRET_STRING
def IndexTemplate():
"""
Return simple index template.
"""
return render_template('index.html')
class APIView(Resource):
"""
API view which contains parsing request data,
initial request handling, and updating user client.
Other API views should extend this one.
"""
def __init__(self):
# Very low level authentication based on given secret
if request.args.get('secret') != SECRET_STRING:
abort(403)
def post(self):
# Take needed items from request data as variables
json_data = request.get_json(force=True)
# Resource object containing all data changes
self.resource = json_data.get('resource')
# Authentication object containing user ID and token
self.authentication = json_data.get('app_user_auth')
# If resource, resource_id, or authentication
# data is missing we can't continue
if not (self.resource and self.authentication):
abort(400)
# Initialize chat bot
self.bot = ChatBot(self.authentication)
class ChatAPIView(APIView):
"""
API view for handling routed chat webhooks.
In this case the bot has received a notification
about new chat that has been added.
"""
def post(self):
super(ChatAPIView, self).post()
self.bot.handle_new_routed_chat(self.resource)
return {'detail': 'OK'}
class ChatMessageAPIView(APIView):
"""
API view for handling routed chat message webhooks.
In this case the bot has receiver a notification
about new chat message that has been added.
"""
def post(self):
super(ChatMessageAPIView, self).post()
self.bot.handle_new_user_chat_message(self.resource)
return {'detail': 'OK'}