forked from mschmoyer/custom_store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
314 lines (254 loc) · 9.99 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import json
import os
import random
import requests
import uuid
from flask import Flask, render_template, request, jsonify, redirect, url_for, flash, session
from datetime import datetime
import base64
from flask_caching import Cache
import openai
from flask_sqlalchemy import SQLAlchemy
import pycountry
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Caching
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
# Environment variables
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
SS_API_KEY = os.environ["SHIPSTATION_API_KEY"]
SS_API_SECRET = os.environ["SHIPSTATION_API_SECRET"]
BASE_URL = "https://ss-devss257.sslocal.com:8060" # Bundles Z-DDE
#BASE_URL = "https://ssapi.shipstation.com/" # For a non-DDE account
# OpenAI API Key
openai.api_key = os.environ["OPENAI_API_KEY"]
# Our persistent database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chat_messages.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
sender = db.Column(db.String(80), nullable=False)
content = db.Column(db.String(500), nullable=False)
def __repr__(self):
return f"<Message {self.sender}: {self.content}>"
class Cart(db.Model):
id = db.Column(db.Integer, primary_key=True)
session_id = db.Column(db.String(255), nullable=False)
product_id = db.Column(db.Integer, nullable=False)
quantity = db.Column(db.Integer, default=1)
def to_dict(self):
return {
'id': self.id,
'session_id': self.session_id,
'product_id': self.product_id,
'quantity': self.quantity
}
# Add this line before the on_message event handler
message_counter = 0
# Initialize the session with a default value for 'session_id'
@app.before_request
def make_session_permanent():
session.permanent = True
session.setdefault('session_id', str(uuid.uuid4()))
def add_to_cart(product_id):
if not product_id:
return {'error': 'Product ID is required'}, 400
# Check if the product is already in the cart
cart_item = Cart.query.filter_by(session_id=session['session_id'], product_id=product_id).first()
if cart_item:
# Update the quantity if the product is already in the cart
cart_item.quantity += 1
else:
# Create a new cart item if the product is not in the cart
cart_item = Cart(
session_id=session['session_id'],
product_id=product_id,
quantity=1
)
db.session.add(cart_item)
db.session.commit()
return cart_item.to_dict(), 201
def create_shipstation_order(order_number, order_items, shipping_info):
country = pycountry.countries.get(name=shipping_info["country"])
country_code = country.alpha_2 if country else shipping_info["country"]
order = {
"orderNumber": order_number,
"orderDate": datetime.now().isoformat(),
"orderStatus": "awaiting_shipment",
"billTo": {
"name": shipping_info["name"],
"street1": shipping_info["street1"],
"city": shipping_info["city"],
"state": shipping_info["state"],
"postalCode": shipping_info["postalCode"],
"country": country_code,
"phone": "555-555-5555",
"email": "[email protected]"
},
"shipTo": {
"name": shipping_info["name"],
"street1": shipping_info["street1"],
"city": shipping_info["city"],
"state": shipping_info["state"],
"postalCode": shipping_info["postalCode"],
"country": country_code,
"phone": "555-555-5555",
"email": "[email protected]"
},
"items": order_items
}
return order
@cache.memoize(300)
def fetch_shipstation_products():
headers = {
"Content-Type": "application/json"
}
auth_string = f"{SS_API_KEY}:{SS_API_SECRET}"
auth_string_encoded = base64.b64encode(auth_string.encode("utf-8")).decode("utf-8")
headers["Authorization"] = f"Basic {auth_string_encoded}"
response = requests.get(f"{BASE_URL}/products", headers=headers)
if response.ok:
shipstation_data = response.json()
shipstation_products = shipstation_data.get("products", [])
products = []
for product in shipstation_products:
if product.get("weightOz", None) is not None:
transformed_product = {
"id": int(product.get("productId", 0)),
"name": product.get("name", ""),
"description": product.get("customsDescription", ""),
"price": product.get("price", 0),
"sku": product.get("sku", "")
}
products.append(transformed_product)
return products
else:
return []
def send_order_to_shipstation(order):
headers = {
"Content-Type": "application/json"
}
# Encode the API key and API secret using Base64
auth_string = f"{API_KEY}:{API_SECRET}"
auth_string_encoded = base64.b64encode(auth_string.encode("utf-8")).decode("utf-8")
# Set the Authorization header with the encoded credentials
headers["Authorization"] = f"Basic {auth_string_encoded}"
response = requests.post(f"{BASE_URL}/orders/createorder", headers=headers, data=json.dumps(order))
# Print the response status code and body
print(f"Response status code: {response.status_code}")
print(f"Response body: {response.text}")
return response
# ROUTES
@app.route("/")
def index():
return redirect(url_for('store'))
@app.route("/blog")
def blog():
return render_template("blog.html")
@app.route("/store")
def store():
products = fetch_shipstation_products()
return render_template("store.html", products=products)
@app.route('/cart', methods=['GET'])
def view_cart():
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
cart_items = Cart.query.filter_by(session_id=session['session_id']).all()
products_dict = {product['id']: product['name'] for product in fetch_shipstation_products()}
for item in cart_items:
item.name = products_dict.get(item.product_id)
return render_template('cart.html', cart_items=cart_items)
@app.route('/add_item_to_cart', methods=['POST'])
def add_item_to_cart_route():
product_id = request.form.get('product_id')
result, status_code = add_to_cart(product_id)
return jsonify(result), status_code
@app.route('/place_order', methods=['POST'])
def place_order():
product_ids = request.get_json().get('product_ids')
shipping_info = request.get_json().get('shipping_info')
order_id = str(uuid.uuid4())
products = fetch_shipstation_products()
order_items = []
for product_id in product_ids:
product = next((p for p in products if p["id"] == int(product_id)), None)
if product is None:
return jsonify({"error": f"Product with ID {product_id} not found."})
order_item = {
"lineItemKey": str(uuid.uuid4()),
"name": product["name"],
"sku": product["sku"],
"quantity": 1,
"unitPrice": product["price"],
"warehouseLocation": "Shelf A1"
}
order_items.append(order_item)
order = create_shipstation_order(order_id, order_items, shipping_info)
response = send_order_to_shipstation(order)
if response.ok:
# Delete all items from the user's cart
Cart.query.filter_by(session_id=session['session_id']).delete()
db.session.commit()
return jsonify({"message": "Order placed successfully!"})
else:
return jsonify({"error": "Failed to place the order. Please try again."})
# This is the now outdated function once used to buy a single product.
@app.route("/buy/<int:product_id>", methods=["POST"])
def buy(product_id):
shipping_info = request.get_json()
order_id = str(uuid.uuid4())
products = fetch_shipstation_products()
product = next((p for p in products if p["id"] == product_id), None)
if product is None:
return jsonify({"error": "Product not found."})
order_items = [{
"lineItemKey": str(uuid.uuid4()),
"name": product["name"],
"sku": product["sku"],
"quantity": 1,
"unitPrice": product["price"],
"warehouseLocation": "Shelf A1"
}]
order = create_shipstation_order(order_id, order_items, shipping_info)
response = send_order_to_shipstation(order)
if response.ok:
return jsonify({"message": "Order placed successfully!"})
else:
return jsonify({"error": "Failed to place the order. Please try again."})
@app.route("/chat", methods=["POST"])
def chat():
message = request.json["message"]
# Add a guiding prompt for the ChatGPT bot
prompt = (
"You are a customer service bot for an online store that sells engineering products. "
"Customers may ask you questions about the ShipStation app as well. You may only respond with a short friendly message and a link to a ShipStation KB article. Provide the actual URL to the article in your response.\n\n"
"Now, the customer asks:\n\n"
f"{message}\n\n"
"Chatbot:"
)
# Connect to ChatGPT and get the response
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
temperature=0.5,
max_tokens=100,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
response_text = response.choices[0].text.strip()
# Save the user's message to the database
chatbot_message = Message(sender="User", content=message)
db.session.add(chatbot_message)
db.session.commit()
return jsonify({"response": response_text})
@app.route("/messages", methods=["GET"])
def get_messages():
messages = Message.query.all()
messages_data = [
{"sender": message.sender, "content": message.content} for message in messages
]
return jsonify(messages_data)
if __name__ == "__main__":
app.run(debug=True)