-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
267 lines (215 loc) · 8.39 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
from datetime import datetime
from messages import *
from flask import Flask, request, session, Response
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from sqlalchemy import exc
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
app.secret_key = '3d6f45a5fc12445dbac2f59c3b6c7cb1'
db_path = 'postgresql+psycopg2://{user}:{pw}@{url}/{db}'.format(
user="user", pw="your_password", url="127.0.0.1:5432", db="insurecad")
app.config['SQLALCHEMY_DATABASE_URI'] = db_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=True)
whatsappno = db.Column(db.String, nullable=True, unique=True)
token = db.Column(db.String, nullable=True)
gender = db.Column(db.String, nullable=True)
age = db.Column(db.String, nullable=True)
state = db.Column(db.String, nullable=True)
lga = db.Column(db.String, nullable=True)
email = db.Column(db.String, nullable=True)
def asdict(self):
return {
"id": self.id,
"name": self.name,
"phone": self.phone,
'token': self.token,
'gender': self.gender,
'age': self.age,
'state': self.state,
'lga': self.lga
}
def process_text(string_list):
new_string_list = []
if len(string_list) != 0:
for i in range(0, len(string_list)):
text = string_list[i]
index = int(text.index(':'))
substring = text[0:index+1]
b = text.replace(substring, '')
new_string_list.append(b)
return new_string_list
def strip_string(text):
new_text = text.replace('whatsapp:', '')
return new_text
def add_number_db(number):
user = User(whatsappno=number)
try:
db.session.add(user)
db.session.commit()
except exc.IntegrityError:
db.session.rollback()
except:
print('There was another error')
raise
else:
print('Everything is OK')
def get_final_reponse(number):
user = User.query.filter_by(whatsappno=number).first()
#recover last place of registration
if user.name is None:
return "Hello, Welcome to INSURECAD. Your number one provider of health insurance across Nigeria.😊\nWhat is your name ?"
elif user.gender is None:
return "Hello, Welcome to INSURECAD. Your number one provider of health insurance across Nigeria.😊\nAre you male or female ?"
elif user.state is None:
return "Hello, Welcome to INSURECAD. Your number one provider of health insurance across Nigeria.😊\nCan we know what state you are messaging us from ?"
elif user.lga is None:
return "Hello, Welcome to INSURECAD. Your number one provider of health insurance across Nigeria.😊\nAlright, what town\nare you messaging us from ?"
elif user.email is None:
return "Hello, Welcome to INSURECAD. Your number one provider of health insurance across Nigeria.😊\nWhat is your e-mail address ?"
else:
return "Hello, Welcome to INSURECAD. Your number one provider of health insurance across Nigeria.😊\nEnter the word *Service* to register for an insurance package ?"
def append_text_to_file(number, response):
file = open(os.path.join("chats/",number+".txt"),'a')
file.write("/n"+response)
file.close()
def get_last_line(number):
file = open(os.path.join("chats/",number+".txt"))
lines = file.readlines()
file.close()
if(len(lines) == 0):
return " "
return lines[-1]
@app.route("/bot", methods=['POST'])
def bot():
#create chat history file
incoming_msg = request.values.get('Body', '').lower()
text = strip_string(request.values.get('From', '').lower())
if(os.path.isfile("chats/"+text+".txt")):
pass
else:
file = open(os.path.join("chats/",text+".txt"),'w')
file.close()
#get last message sent
last_msg = get_last_line(text)
add_number_db(text)
resp = MessagingResponse()
msg = resp.message()
responded = False
if last_msg == name_question:
name = incoming_msg
user = User.query.filter_by(whatsappno=text).first()
if user is None:
pass
else:
user.name = name
db.session.commit()
response = "Great😊 Now " + name.capitalize() + \
".\nAre you male or female ?"
msg.body(response)
append_text_to_file(text,response)
responded = True
elif last_msg == gender_question:
gender = incoming_msg
user = User.query.filter_by(whatsappno=text).first()
if user is None:
pass
else:
user.gender = gender
db.session.commit()
response = "Alright😊.\nCan we know what state you are messaging us from ?"
msg.body(response)
append_text_to_file(text,response)
responded = True
elif last_msg == state_question:
state = incoming_msg
user = User.query.filter_by(whatsappno=text).first()
if user is None:
pass
else:
user.state = state
db.session.commit()
response = "What town in " + state.capitalize() + ".\nare you messaging us from ?"
msg.body(response)
append_text_to_file(text,response)
responded = True
elif last_msg == town_question:
town = incoming_msg
user = User.query.filter_by(whatsappno=text).first()
if user is None:
pass
else:
user.lga = town
db.session.commit()
response = "We are almost there🤗.\nWhat is your e-mail address ?"
msg.body(response)
append_text_to_file(text,response)
responded = True
elif last_msg == email_question:
if '@' not in incoming_msg:
response = "Please enter a valid e-mail address."
msg.body(response)
else:
user = User.query.filter_by(whatsappno=text).first()
if user is None:
pass
else:
user.email = incoming_msg
db.session.commit()
response = "Great. You now have access to INSURECAD services.\nEnter the word *Service* to register for an insurance package ?"
append_text_to_file(text,response)
msg.body(response)
elif 'service' in incoming_msg:
response = "\n1️⃣ Health Insurace.\n2️⃣ Accident Insurance🤵🏽.\nChoose an Option."
msg.body(response)
append_text_to_file(text,response)
responded = True
elif last_msg == option_question:
if '1' == incoming_msg or '2' == incoming_msg:
response = "\nDo you have an Insurecad token ? (yes/no)"
msg.body(response)
append_text_to_file(text,response)
responded = True
elif '2' == incoming_msg:
response = "\nDo you have an Insurecad token ? (yes/no)"
msg.body(response)
append_text_to_file(text,response)
responded = True
else:
response = "\nIt seems like you sent a wrong response. Please reply with only 1 or 2 and not both."
msg.body(response)
append_text_to_file(text,response)
elif last_msg == token_question:
if 'yes' == incoming_msg:
response = "Okay.\nPlease enter your token"
msg.body(response)
responded = True
append_text_to_file(text,response)
elif 'no' in incoming_msg:
response = "Please follow the link ➡️ to make payment:🌍 www.paystack.com/developers"
msg.body(response)
append_text_to_file(text,response)
responded = True
else:
response = "Please reply with only yes/no"
msg.body(response)
append_text_to_file(text,response)
elif last_msg == collect_token:
token = incoming_msg
response = "\nToken received. Thank you for using INSURECAD\n"
msg.body(response)
append_text_to_file(text,response)
else:
response = get_final_reponse(text)
msg.body(response)
append_text_to_file(text,response)
return Response(str(resp), mimetype='text/xml')
if __name__ == "__main__":
app.run()