-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
97 lines (84 loc) · 4.34 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
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from pymongo import MongoClient
from datetime import datetime
cluster = MongoClient("mongodb+srv://maleesha:[email protected]/?retryWrites=true&w=majority")
db = cluster["injector"]
users = db["users"]
orders = db["orders"]
app = Flask(__name__)
@app.route("/", methods=["get", "post"])
def reply():
text = request.form.get("Body")
number = request.form.get("From")
number = number.replace("whatsapp:", "")
res = MessagingResponse()
user = users.find_one({"number": number})
if bool(user) == False:
res.message("Hi, thanks for contacting *The Network Chunkies*.\nYou can choose from one of the options below: "
"\n\n*Type*\n\n 1️⃣ To *contact* us \n 2️⃣ To *order* snacks \n 3️⃣ To know our *working hours* \n 4️⃣ "
"To get our *address*")
users.insert_one({"number": number, "status": "main", "messages": []})
elif user["status"] == "main":
try:
option = int(text)
except:
res.message("Please enter a valid response!")
return str(res)
if option == 1:
res.message("You can contact us through phone or e-mail.\n\n*Phone*: 9470 154 9092 \n*E-mail* : [email protected]")
elif option == 2:
res.message("You have entered *ordering mode*.")
users.update_one(
{"number": number}, {"$set": {"status": "ordering"}})
res.message(
"You can select one of the following cakes to order: \n\n1️⃣ Red Velvet \n2️⃣ Dark Forest \n3️⃣ Ice Cream Cake"
"\n4️⃣ Plum Cake \n5️⃣ Sponge Cake \n6️⃣ Genoise Cake \n7️⃣ Angel Cake \n8️⃣ Carrot Cake \n9️⃣ Fruit Cake \n0️⃣ Go Back")
elif option == 3:
res.message("We work from *9 a.m. to 5 p.m*.")
elif option == 4:
res.message(
"We have multiple stores across the city. Our main center is at *4/54, New Delhi*")
else:
res.message("Please enter a valid response")
elif user["status"] == "ordering":
try:
option = int(text)
except:
res.message("Please enter a valid response")
return str(res)
if option == 0:
users.update_one(
{"number": number}, {"$set": {"status": "main"}})
res.message("You can choose from one of the options below: "
"\n\n*Type*\n\n 1️⃣ To *contact* us \n 2️⃣ To *order* snacks \n 3️⃣ To know our *working hours* \n 4️⃣ "
"To get our *address*")
elif 1 <= option <= 9:
cakes = ["Red Velvet Cake", "Dark Forest Cake", "Ice Cream Cake",
"Plum Cake", "Sponge Cake", "Genoise Cake", "Angel Cake", "Carrot Cake", "Fruit Cake"]
selected = cakes[option - 1]
users.update_one(
{"number": number}, {"$set": {"status": "address"}})
users.update_one(
{"number": number}, {"$set": {"item": selected}})
res.message("Excellent choice 😉")
res.message("Please enter your address to confirm the order")
else:
res.message("Please enter a valid response")
elif user["status"] == "address":
selected = user["item"]
res.message("Thanks for shopping with us 😊")
res.message(f"Your order for *{selected}* has been received and will be delivered within 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":
res.message("Hi, thanks for contacting again.\nYou can choose from one of the options below: "
"\n\n*Type*\n\n 1️⃣ To *contact* us \n 2️⃣ To *order* snacks \n 3️⃣ To know our *working hours* \n 4️⃣ "
"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(res)
if __name__ == "__main__":
app.run()