Upto12forenglish commited on
Commit
f31a243
1 Parent(s): 43a67bd

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +23 -0
  2. main.py +283 -0
  3. password_generator.py +22 -0
  4. requirements.txt +4 -0
  5. send_email_template.py +59 -0
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the current directory contents into the container at /app
8
+ COPY . /app
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Set environment variables, including TRANSFORMERS_CACHE
14
+ ENV TRANSFORMERS_CACHE /app/cache
15
+
16
+ # Set cache directory permissions
17
+ RUN mkdir -p /app/cache && chmod 777 /app/cache
18
+
19
+ # Make port 7860 available to the world outside this container
20
+ EXPOSE 7860
21
+
22
+ # Define the command to run your application
23
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from fastapi.responses import RedirectResponse, HTMLResponse
4
+ import urllib.parse
5
+ import requests, json, time
6
+ from password_generator import generate_password
7
+ import threading
8
+
9
+
10
+ user_credentials_google_apps_script_url = "https://script.google.com/macros/s/AKfycbzm5dC7jMJ4y7Cdu-sW05kozA6HgcTp2zoHi7BjJQ7Hhvp0exihsllgBsiqMoa68i6-XA/exec"
11
+ app_link = "https://huggingface.co/spaces/Upto12forenglish/ZidTurboApp/edit/main/main.py"
12
+
13
+ def send_email_credentials(email, username, password, app_link):
14
+ url = "https://hook.eu2.make.com/soremxx4ove87uk7hmghfuhvtl3gaqc6"
15
+
16
+ payload = {
17
+ "email": email,
18
+ "username": username,
19
+ "password": password,
20
+ "app_link": app_link
21
+ }
22
+
23
+ try:
24
+ response = requests.post(url, json=payload)
25
+ response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
26
+ print(f"Status Code: {response.status_code}")
27
+ except requests.exceptions.RequestException as e:
28
+ print(f"An error occurred: {e}")
29
+ if e.response is not None:
30
+ print(f"Response Content: {e.response.content.decode()}")
31
+
32
+ # On getting the store credentials, Get the store id and info using "https://api.zid.sa/v1/managers/account/profile" endpoint
33
+ def get_store_id(access_token, authorization):
34
+ print("Entered get store id function")
35
+ time.sleep(0.5)
36
+ # Set up the authorization header
37
+ headers = {
38
+ 'Authorization': 'Bearer ' + authorization,
39
+ 'X-Manager-Token': access_token,
40
+ }
41
+
42
+ # Make the GET request to the API endpoint
43
+ response = requests.get('https://api.zid.sa/v1/managers/account/profile', headers=headers)
44
+ response_data = response.json()
45
+ store_name = response_data['user']['name']
46
+ store_id = response_data['user']['store']['id']
47
+ store_username = response_data['user']['username']
48
+ store_manager_email = response_data['user']['email']
49
+ store_manager_mobile = response_data['user']['mobile']
50
+ # Print the JSON response
51
+ print("Store name is ", store_name)
52
+ print("Store id is ", store_id)
53
+ print("Username is ", store_username)
54
+ print("Email is ", store_manager_email)
55
+ print("Phone is ", store_manager_mobile)
56
+
57
+ print("Exited get store id function")
58
+ return store_name, store_id, store_username, store_manager_email, store_manager_mobile
59
+
60
+ # On new app installation, add the store info to the database sheet
61
+ def create_new_user(access_token, authorization, refresh_token, expires_in):
62
+ print("Entered create new user function")
63
+ # Call get_store_id to retreive store information
64
+ store_name, store_id, username, email, mobile = get_store_id(access_token, authorization)
65
+
66
+ time.sleep(0.5)
67
+ # Generate user password
68
+ password = generate_password()
69
+ print("Password was generated successfully, ", password)
70
+
71
+ time.sleep(0.5)
72
+ # Send the store info to google app script to register the new user information to the user database
73
+ try:
74
+ response = requests.get(user_credentials_google_apps_script_url, params={"accessToken": access_token, "authorization": authorization, "refreshToken":refresh_token, "expiresIn": expires_in, "storeName": store_name, "storeID": store_id, "username": username, "storeManagerEmail": email, "mobileNumber": mobile, "storeManagerpassword": password})
75
+ responseData = json.loads(response.text)
76
+ print(responseData) # Put the result in the queue
77
+ except Exception as e:
78
+ print(str(e)) # Put the error message in the queue
79
+
80
+ time.sleep(0.5)
81
+ print("Now sending the credentials email to the store owner")
82
+ # Send an email with user credentials
83
+ # send_email(email, username, password, app_link)
84
+ send_email_credentials(email, username, password, app_link)
85
+ print("Exited create new user function")
86
+
87
+ def process_oauth_response(response_data):
88
+ try:
89
+ # Extract data
90
+ access_token = response_data["access_token"]
91
+ token_type = response_data["token_type"]
92
+ expires_in = response_data["expires_in"]
93
+ authorization = response_data["authorization"]
94
+ refresh_token = response_data["refresh_token"]
95
+
96
+ print("Access Token: ", access_token)
97
+ print("Authorization: ", authorization)
98
+ print("Refresh Token: ", refresh_token)
99
+ print("Expires in: ", expires_in)
100
+
101
+ create_new_user(access_token, authorization, refresh_token, expires_in)
102
+ except KeyError as e:
103
+ print(f"Missing expected key in response data: {e}")
104
+ except Exception as e:
105
+ print(f"An error occurred while processing the response: {e}")
106
+
107
+ app = FastAPI()
108
+
109
+
110
+ class Message(BaseModel):
111
+ message: str
112
+
113
+
114
+ @app.get("/", response_class=HTMLResponse)
115
+ async def read_root():
116
+ return """
117
+ <!DOCTYPE html>
118
+ <html>
119
+ <head>
120
+ <title>Zid-Copilot</title>
121
+ <style>
122
+ body {
123
+ font-family: Arial, sans-serif;
124
+ background-color: #f4f4f4;
125
+ margin: 0;
126
+ padding: 0;
127
+ display: flex;
128
+ justify-content: center;
129
+ align-items: center;
130
+ height: 100vh;
131
+ text-align: center;
132
+ }
133
+ .container {
134
+ background-color: #fff;
135
+ border-radius: 10px;
136
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
137
+ padding: 20px;
138
+ max-width: 600px;
139
+ width: 100%;
140
+ }
141
+ h1 {
142
+ color: #333;
143
+ }
144
+ ul {
145
+ list-style-type: none; /* Remove default bullet points */
146
+ padding: 0;
147
+ }
148
+ li {
149
+ margin-bottom: 10px;
150
+ background-color: #fff;
151
+ color: #333;
152
+ padding: 10px 20px;
153
+ border-radius: 5px;
154
+ cursor: default;
155
+ border: 1px solid #ccc;
156
+ box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
157
+ }
158
+ li:hover {
159
+ background-color: #f9f9f9;
160
+ }
161
+ .start-button {
162
+ background-color: #28a745;
163
+ color: white;
164
+ padding: 10px 20px;
165
+ border: none;
166
+ border-radius: 5px;
167
+ cursor: pointer;
168
+ transition: background-color 0.3s;
169
+ width: 100%;
170
+ margin-top: 20px;
171
+ }
172
+ .start-button:hover {
173
+ background-color: #218838;
174
+ }
175
+ .hidden-buttons {
176
+ display: none;
177
+ margin-top: 20px;
178
+ }
179
+ .hidden-buttons input {
180
+ width: calc(100% - 22px);
181
+ padding: 10px;
182
+ margin: 5px;
183
+ border: 1px solid #ccc;
184
+ border-radius: 5px;
185
+ }
186
+ .hidden-buttons label {
187
+ display: block;
188
+ margin-bottom: 5px;
189
+ color: #333;
190
+ }
191
+ .hidden-buttons button {
192
+ background-color: #007bff;
193
+ color: white;
194
+ padding: 10px 20px;
195
+ border: none;
196
+ border-radius: 5px;
197
+ margin: 5px;
198
+ cursor: pointer;
199
+ transition: background-color 0.3s;
200
+ }
201
+ .hidden-buttons button:hover {
202
+ background-color: #0056b3;
203
+ }
204
+ </style>
205
+ <script>
206
+ function showButtons() {
207
+ document.getElementById('hidden-buttons').style.display = 'block';
208
+ }
209
+
210
+ function handleSubmit(event) {
211
+ event.preventDefault();
212
+ const messageDiv = document.getElementById('message');
213
+ messageDiv.textContent = 'File submitted successfully, now processing';
214
+ messageDiv.style.display = 'block';
215
+ }
216
+
217
+ function openReferenceFile() {
218
+ window.open('https://docs.google.com/spreadsheets/d/1L9pUbj8V9lm6g9Fqy-9viRQzon0YtExXNE77SozuHns/edit?usp=drive_link', '_blank');
219
+ }
220
+ </script>
221
+ </head>
222
+ <body>
223
+ <div class="container">
224
+ <h1>Welcome to Zid-Copilot!</h1>
225
+ <p>Your ultimate assistant for effortless product management.</p>
226
+ <ul>
227
+ <li>Unlimited ease in adding and modifying your store's products</li>
228
+ <li>Intuitive interface for seamless navigation</li>
229
+ <li>24/7 support for any assistance you may need</li>
230
+ <li>Stay ahead with our cutting-edge technology</li>
231
+ <li>Effortlessly boost your store's performance</li>
232
+ <li>Join thousands of satisfied users today!</li>
233
+ </ul>
234
+ <button class="start-button" onclick="showButtons()">Start Now</button>
235
+ <div id="hidden-buttons" class="hidden-buttons">
236
+ <form onsubmit="handleSubmit(event)">
237
+ <label for="file-link">Insert your products Google Sheet file link:</label>
238
+ <input type="text" id="file-link" name="file-link" required>
239
+ <button type="submit">Submit</button>
240
+ <button type="button" onclick="openReferenceFile()">Use Reference File</button>
241
+ </form>
242
+ <div id="message" style="display: none; margin-top: 20px; color: green;"></div>
243
+ </div>
244
+ </div>
245
+ </body>
246
+ </html>
247
+ """
248
+
249
+ @app.post("/update")
250
+ async def receive_message(message: Message):
251
+ print("Received message:", message.message)
252
+ return {"message": "Message received successfully"}
253
+
254
+
255
+ @app.get("/redirect")
256
+ def redirect_to_oauth():
257
+ queries = {
258
+ 'client_id': '3365',
259
+ 'redirect_uri': 'https://upto12forenglish-zidturboapp.hf.space/oauth/callback',
260
+ 'response_type': 'code'
261
+ }
262
+ query_string = urllib.parse.urlencode(queries)
263
+ oauth_url = f'https://oauth.zid.sa/oauth/authorize?{query_string}'
264
+ return RedirectResponse(url=oauth_url)
265
+
266
+ @app.get("/oauth/callback")
267
+ def callback(code):
268
+ payload = {
269
+ 'grant_type': 'authorization_code',
270
+ 'client_id': 3365,
271
+ 'client_secret': 'YAZTkOvz0EkHOZuuEbisoeLrqPVC4bWVDcSwexLS',
272
+ 'redirect_uri': 'https://upto12forenglish-zidturboapp.hf.space/oauth/callback', # Your Application's Callback URI
273
+ 'code': code # grant code
274
+ }
275
+ response = requests.post('https://oauth.zid.sa/oauth/token', data=payload)
276
+
277
+ response_data = response.json()
278
+
279
+ # Start a new thread to process the response data
280
+ processing_thread = threading.Thread(target=process_oauth_response, args=(response_data,))
281
+ processing_thread.start()
282
+
283
+ return RedirectResponse(url=app_link)
password_generator.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import string
3
+
4
+ def generate_password():
5
+ # Define characters, numbers, and special characters
6
+ chars = string.ascii_lowercase + string.ascii_uppercase + string.digits + "@#&"
7
+
8
+ # Ensure at least one character from each category
9
+ password = random.choice(string.ascii_lowercase) \
10
+ + random.choice(string.ascii_uppercase) \
11
+ + random.choice(string.digits) \
12
+ + random.choice("@#&")
13
+
14
+ # Add remaining characters randomly
15
+ password += ''.join(random.choice(chars) for _ in range(4))
16
+
17
+ # Shuffle the password to mix characters
18
+ password_list = list(password)
19
+ random.shuffle(password_list)
20
+ password = ''.join(password_list)
21
+
22
+ return password
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ uvicorn
2
+ python-multipart
3
+ requests
4
+ fastapi
send_email_template.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import smtplib
2
+ from email.mime.multipart import MIMEMultipart
3
+ from email.mime.text import MIMEText
4
+
5
+ def send_email(receiver_email, username, user_password, app_link):
6
+ # Email account credentials
7
+ sender_email = "naqra.app@outlook.com"
8
+ sender_password = "@naserCSE92" # Replace with the actual password
9
+
10
+ # Email content
11
+ subject = "Thank You for Installing Naqra App"
12
+
13
+ body = f"""
14
+ <html>
15
+ <body>
16
+ <p>Dear {username},</p>
17
+ <p>Welcome to the Naqra community!</p>
18
+ <p><span style="font-size: 20px; color: blue;">Thank you for installing the Naqra app.</span></p>
19
+ <p>Here are your login credentials:</p>
20
+ <p>Username: {username}<br>
21
+ Password: {user_password}</p>
22
+ <p><a href="{app_link}" style="text-decoration: none;">
23
+ <button style="background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer;">Log in to Naqra App</button>
24
+ </a></p>
25
+ <p>If you have any questions or need assistance, please don't hesitate to reach out to our support team.</p>
26
+ <p>Best regards,<br>
27
+ Naqra Team</p>
28
+ </body>
29
+ </html>
30
+ """
31
+
32
+ # Create the email message
33
+ msg = MIMEMultipart()
34
+ msg['From'] = sender_email
35
+ msg['To'] = receiver_email
36
+ msg['Subject'] = subject
37
+
38
+ # Attach the email body with HTML content
39
+ msg.attach(MIMEText(body, 'html'))
40
+
41
+ try:
42
+ # Connect to the Outlook SMTP server
43
+ server = smtplib.SMTP('smtp.office365.com', 587)
44
+ server.starttls()
45
+
46
+ # Log in to the email account
47
+ server.login(sender_email, sender_password)
48
+
49
+ # Send the email
50
+ server.send_message(msg)
51
+
52
+ print("Email sent successfully!")
53
+
54
+ except Exception as e:
55
+ print(f"Failed to send email. Error: {e}")
56
+
57
+ finally:
58
+ # Terminate the SMTP session
59
+ server.quit()