-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
93 lines (85 loc) · 4.45 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
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from pymongo import MongoClient
from datetime import datetime
cluster = MongoClient("mongodb+srv://whatsapp:[email protected]/?retryWrites=true&w=majority")
db = cluster["database"]
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:", "")[:-2]
res = MessagingResponse()
user = users.find_one({"number": number})
if bool(user) == False:
msg = res.message("Hi, thanks for contacting *Company Name*.\nYou can choose from one of the options below: "
"\n\n*Type*\n\n 1️⃣ To *contact* us \n 2️⃣ To *Book* an appointment with our company \n 3️⃣ To know our *working hours* \n 4️⃣ "
"To get our *address*")
msg.media("https://cyberintelligencesolution.com/images/slide.jpg")
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*: +91 01234 56789 \n*E-mail* : [email protected]")
elif option == 2:
res.message("You want to book an *appointment*.")
users.update_one(
{"number": number}, {"$set": {"status": "ordering"}})
res.message(
"You can select one of the following services to book an appointment: \n\n1️⃣ Managed Services \n2️⃣ Security Consulting \n3️⃣ Training Programs"
"\n4️⃣ eCommerce Solutions \n5️⃣ Cyber Security \n6️⃣ Placements \n0️⃣ Go Back")
elif option == 3:
res.message("We work 24/7 monitoring threat analysis to keep your business protected.")
elif option == 4:
res.message(
"Our Address Is : New York City")
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 *Book* an appointment with our company \n 3️⃣ To know our *working hours* \n 4️⃣ "
"To get our *address*")
elif 1 <= option <= 6:
services = ["Managed Services", "Security Consulting", "Training Programs", "eCommerce Solutions", "Cyber Security", "Placements"]
selected = services[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 Name & Mobile Number to confirm the appointment")
else:
res.message("Please enter a valid response")
elif user["status"] == "address":
selected = user["item"]
res.message("Thankyou for contacting *Sample Company* 😊")
res.message(f"Your appointment for *{selected}* has been received and we will contact you soon")
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 *Book* an appointment with our company \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()