Skip to content
Snippets Groups Projects
Verified Commit acd227a5 authored by TheJoeCoder's avatar TheJoeCoder
Browse files

[backend] try to improve webhooks

parent 3c0356ab
Branches
No related tags found
No related merge requests found
......@@ -267,10 +267,11 @@ def refresh_cart_items():
row += 1
configure_grid(cart_frame)
def confirm_order(address_vars: dict):
global cart_id, cart_key
def confirm_order(addressline1, addressline2, city, postcode):
global cart_id, cart_key, req_cart_items
required_vars = [addressline1, city, postcode]
# Check all address fields are filled
for var in address_vars:
for var in required_vars:
if (var.get() == ""):
messagebox.showerror("Error", "Please fill in all address fields")
return
......@@ -280,11 +281,13 @@ def confirm_order(address_vars: dict):
return
# Join address fields
address = ""
for var in address_vars:
address += var.get() + ", "
address = address[:-2]
address += addressline1.get() + ", "
address += addressline2.get() + ", "
address += city.get() + ", "
address += postcode.get()
# Confirm order
reqjson = {"id": cart_id, "key": cart_key, "address": address}
logger.debug(reqjson)
res = requests.post(get_endpoint("/cart/confirmorder"), json=reqjson)
if (res.status_code == 200):
req_cart_items = []
......@@ -292,6 +295,8 @@ def confirm_order(address_vars: dict):
new_session()
tab_control.select(order_frame)
messagebox.showinfo("OK!", "Order confirmed")
else:
messagebox.showerror("Error", "Failure to order: " + res.text)
def tab_changed_handler(event):
selected_tab = event.widget.select()
......@@ -389,7 +394,8 @@ confirm_label_4.grid(column=1, row=4, sticky=(W, E))
confirm_entry_4 = ttk.Entry(confirm_frame, width=20, textvariable=confirm_postcode)
confirm_entry_4.grid(column=2, row=4, sticky=(W, E))
confirm_button_1 = ttk.Button(confirm_frame, text="Confirm", command=lambda: confirm_order([confirm_addressline1, confirm_addressline2, confirm_city, confirm_postcode]))
confirm_button_1 = ttk.Button(confirm_frame, text="Confirm", command=lambda: confirm_order(confirm_addressline1, confirm_addressline2, confirm_city, confirm_postcode))
confirm_button_1.grid(column=1, row=5, sticky=(W, E))
# Pack
tab_control.pack(expand=1, fill="both")
......
......@@ -211,10 +211,11 @@ def confirm_order_cart():
if (address == None):
return gen_missing_message("address")
# Test for invalid address and convert to dict
try:
address = json.loads(address)
except:
return gen_invalid_message("address")
#print(address)
#try:
# address = json.loads(address)
#except:
# return gen_invalid_message("address")
# Build order
order = {
"address": address,
......@@ -224,9 +225,19 @@ def confirm_order_cart():
order_id = db.orders.insert_one(order).inserted_id
# Post webhook
webhook_data = {
"username": "PizzaBot",
"content": config.webhook_content_prepend + str(order_id)
"username": "PizzaBot"
}
if config.webhook_discord:
# Post webhook
embed = config.webhook_dc_embed_json
for i in range(0, len(embed["fields"])):
for field_k, field_v in embed["fields"][i].items():
embed["fields"][i][field_k] = str(field_v).replace("%ORDER_ID%", str(order_id))
embed["fields"][i][field_k] = str(field_v).replace("%ADDRESS%", str(address))
embed["fields"][i][field_k] = str(field_v).replace("%ITEMS%", str(cart["items"]))
webhook_data["embeds"] = [embed]
else:
webhook_data["content"] = config.webhook_content_prepend + str(order_id)
try:
webhook_response = requests.post(config.webhook_url, json=webhook_data)
if (webhook_response.status_code != 200):
......
webhook = "https://example.com/webhook"
webhook_content_prepend = "New order received! Order ID: "
webhook_discord = True # If true, will use discord embeds below instead of prepend
webhook_dc_embed_json = {
"author": {
"name": "PizzaPalace"
},
"title": "New Order!",
"fields": [
{
"name": "Order ID",
"value": "%ORDER_ID%"
},
{
"name": "Address",
"value": "%ADDRESS%"
},
{
"name": "Items",
"value": "%ITEMS%"
}
],
"color": "#00b0f4",
"footer": {
"text": "PizzaPalace by TheJoeCoder"
}
}
delivery_flatrate = 0.0
mongo_uri = "mongodb://localhost:27017/"
mongo_db = "pizza_palace"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment