-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (88 loc) · 4.43 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
from flask import Flask, request
import requests
from twilio.twiml.messaging_response import MessagingResponse
from pymongo import MongoClient
from datetime import datetime
cluster = MongoClient("mongodb+srv://harish:[email protected]/?retryWrites=true&w=majority")
db = cluster["bekary"]
users = db["users"]
orders = db["orders"]
app = Flask(__name__.split('.')[0])
@app.route('/', methods=['GET', 'POST'])
def reply():
text = request.form.get("Body")
number = request.form.get("From")
number = number.replace("whatsapp:", "")
response = MessagingResponse()
user = users.find_one({"number": number})
if bool(user) ==False :
response.message(
"Hi, thanks for contacting *The Red Velvet* \n You can choose from one of th options below: "
"\n\n*Type*\n\n1️⃣ TO *Contact* us \n2️⃣ To *order* snacks \n3️⃣To know oue *working hours* \n4️⃣ To get "
"our*Address*")
users.insert_one({"number": number, "status": "main", "messages": []})
elif user["status"] == "main":
try:
option = int(text)
except:
response.message("Please enter a valid response")
return str(response)
if option == 1:
response.message("you can contact us through phone or email. \n\n *phone* 997892307634 \n *E-mail* : "
elif option == 2:
response.message("you have entered *ordering mode*.")
users.update_one(
{"number": number}, {"$set": {"status": "ordering"}})
response.message(
"You can select one of the following cakes to order, \n\n *1️⃣Red Velvet* \n2️⃣Dark Forest "
"\n3️⃣Ice Cream Cake \n4️⃣Plum Cake \n5️⃣Sponge Cake \n6️⃣Genoise Cake \n7️⃣Carrot Cake "
"\n8️⃣Butterscotch \n0️⃣Go Back* ")
elif option == 3:
response.message("We work on everyday *9 AM to 5 PM*")
elif option == 4:
response.message("We work many centres across the city our main centre at *Bangalore*")
else:
response.message("Please enter a valid response")
elif user["status"] == "ordering":
try:
option = int(text)
except:
response.message("Please enter a valid response")
return str(response)
if option == 0:
users.update_one(
{"number": number}, {"$set": {"status": "main"}})
response.message(" You can choose from one of th options below: "
"\n\n*Type*\n\n1️⃣ TO *Contact* us \n2️⃣ To *order* snacks \n3️⃣To know oue *working hours* \n4️⃣ To get "
"our*Address*")
elif 1 <= option <= 9:
cakes = ["Red Velvet Cake", "Dark Forest Cake", "Ice Cream Cake", "Plum Cake", "Sponge Cake",
"Genoise Cake", "Carrot Cake", "Butterscotch"]
selected = cakes[option - 1]
users.update_one(
{"number": number}, {"$set": {"status": "address"}})
users.update_one(
{"number": number}, {"$set": {"item": selected}})
response.message("Excellent choice😉")
response.message("Please Enter your address to confirm the order")
else:
response.message("Please enter a valid response")
elif user["status"] == "address":
selected = user["item"]
response.message("Thanks for shopping with us")
response.message(f" your order for {selected} has been received and will be delivered with in an hour")
orders.insert_one({"number": number, "item": selected, "address": text, "order_time": datetime.now()})
users.update_one(
{"number": number}, {"$set": {"status": "ordered"}})
elif user["status"] == "ordered":
response.message(
"Hi, thanks for contacting again * The Red Velvet* \n You can choose from one of th options below: "
"\n\n*Type*\n\n1️⃣ TO *Contact* us \n2️⃣ To *order* snacks \n3️⃣To know oue *working hours* \n4️⃣ To get "
"our*Address*")
users.update_one(
{"number": number}, {"$set": {"status": "main"}})
users.update_one({"number": number}, {"$push": {"messages": {"text": text, "date": datetime.now()}}})
return str(response)
if __name__ == '__main__':
app.run()