-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·203 lines (155 loc) · 4.75 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import json
import os
import psycopg2
import intents.query_line
import intents.query_ticket
import intents.create_ticket
import intents.delete_ticket
from urllib import parse
from flask import Flask
from flask import request
from flask import make_response
from flask.ext.sqlalchemy import SQLAlchemy
import pdb
# Flask app should start in global layout
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class LineDB(db.Model):
__tablename__ = "mvp_table"
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.Text(), nullable=False)
issue_type = db.Column(db.Text(), nullable=False)
callback_method = db.Column(db.Text(), nullable=False)
callback_details = db.Column(db.Text())
# TODO come back to make the below work. Focus on MVP for v1
# last_name = db.Column(db.Text())
# tech_id = db.Column(db.Text())
# issue_details = db.Column(db.Text())
# que_notes = db.Column(db.Text())
def __init__(self, first_name, issue_type, callback_method, callback_details):
self.first_name = first_name
self.issue_type = issue_type
self.callback_method = callback_method
self.callback_details = callback_details
def __repr__(self):
return '<id {}>'.format(self.id)
@app.route('/webhook', methods=['POST'])
def webhook():
# pdb.set_trace()
# assert request is not None
req = request.get_json(silent=True, force=True)
# req = json.loads(request.data)
print("Request:")
print(json.dumps(req, indent=4))
# json_object = json.loads(req)
res = route_action(req)
json_convert = json.dumps(res)
r = make_response(json_convert)
r.headers['Content-Type'] = 'application/json'
print("**RETURN**\n")
print(r)
return r
<<<<<<< HEAD
# def db_connection(db_query):
# parse.uses_netloc.append("postgres")
# url = parse.urlparse(os.environ["DATABASE_URL"])
# # DATABASE_URL set in : heroku config
#
# conn = psycopg2.connect(
# database=url.path[1:],
# user=url.username,
# password=url.password,
# host=url.hostname,
# port=url.port
# )
#
# cur = conn.cursor()
# cur.execute(db_query)
#
# query_results = cur.fetchall()
#
# return query_results
=======
>>>>>>> develop
def route_action(req):
if req.get("result").get("action") == "queryLine":
res = process_query_line()
return res
elif req.get("result").get("action") == "createTicket":
res = process_create_ticket(req)
return res
elif req.get("result").get("action") == "queryTicket":
res = process_query_ticket(req)
return res
pass
elif req.get("result").get("action") == "deleteTicket":
res = process_delete_ticket(req)
return res
pass
else:
speech = "I didn't understand that action(webhook response)"
return {
"speech": speech,
"displayText": speech
}
def process_query_line():
data = intents.query_line.get_data()
speech = "The current line looks like:\n\n"
speech = speech + data
print(speech)
return {
"speech": speech,
"displayText": speech
}
def process_create_ticket(req):
post = intents.create_ticket.post_data(req)
speech = "Added your ticket in with the below:\n\n"
speech = speech + post
<<<<<<< HEAD
=======
print(speech)
return {
"speech": speech,
"displayText": speech
}
def process_query_ticket(req):
post = intents.query_ticket.get_ticket(req)
speech = "Just to verify, you're attempting to help out:\n"
speech = speech + post
speech = speech + "\n\n**Please respond with: 'yes, remove <number>'"
>>>>>>> develop
print(speech)
return {
"speech": speech,
"displayText": speech
}
<<<<<<< HEAD
def process_query_ticket(req):
post = intents.query_ticket.get_ticket(req)
speech = "Just to verify, you're attempting to help out:\n"
speech = speech + post
speech = speech + "\n\n**Please respond with: 'yes, remove <number>'"
print(speech)
return {
"speech": speech,
"displayText": speech
}
def process_delete_ticket(req):
post = intents.delete_ticket.delete_data(req)
=======
def process_delete_ticket(req):
post = intents.delete_ticket.delete_data(req)
>>>>>>> develop
speech = "Attempting to remove ticket..\n"
speech = speech + post
print(speech)
return {
"speech": speech,
"displayText": speech
}
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
print("Starting app on port %d" % port)
app.run(debug=False, port=port, host='0.0.0.0')