Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,41 +7,41 @@ PASSWORD = "7995604808g@"
|
|
| 7 |
SECURITY_TOKEN = "HIi7RyLwaqfYvvTLZvxrU8pU"
|
| 8 |
CONSUMER_KEY = "3MVG9GCMQoQ6rpzTiMF88fek3awTaWrIiDcs_.Cm22MYTnZGgyNwfvV8Zstde05XXgJlUDQBxUHgtC62on9e6"
|
| 9 |
CONSUMER_SECRET = "BE933C0B77021E853412938449543632636EE48301A12DDF786C87253B9C46F5"
|
| 10 |
-
LOGIN_URL = "https://
|
| 11 |
|
| 12 |
-
#
|
| 13 |
def get_salesforce_access_token():
|
| 14 |
payload = {
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
}
|
| 21 |
response = requests.post(LOGIN_URL, data=payload)
|
| 22 |
if response.status_code == 200:
|
| 23 |
data = response.json()
|
| 24 |
-
return data[
|
| 25 |
else:
|
| 26 |
raise Exception(f"Authentication failed: {response.json()}")
|
| 27 |
|
| 28 |
-
# Function to save customer
|
| 29 |
def save_customer_to_salesforce(name, email, phone):
|
| 30 |
try:
|
|
|
|
| 31 |
access_token, instance_url = get_salesforce_access_token()
|
| 32 |
headers = {
|
| 33 |
-
|
| 34 |
-
|
| 35 |
}
|
| 36 |
payload = {
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
}
|
| 42 |
url = f"https://huggingface.co/spaces/nagasurendra/DynamicMenu1"
|
| 43 |
response = requests.post(url, headers=headers, json=payload)
|
| 44 |
-
|
| 45 |
if response.status_code == 201:
|
| 46 |
return "Customer details saved successfully!"
|
| 47 |
else:
|
|
@@ -49,20 +49,20 @@ def save_customer_to_salesforce(name, email, phone):
|
|
| 49 |
except Exception as e:
|
| 50 |
return str(e)
|
| 51 |
|
| 52 |
-
# Gradio interface
|
| 53 |
def submit_form(name, email, phone):
|
| 54 |
if not name or not email or not phone:
|
| 55 |
-
return "All fields are required
|
| 56 |
return save_customer_to_salesforce(name, email, phone)
|
| 57 |
|
| 58 |
-
# Create
|
| 59 |
interface = gr.Interface(
|
| 60 |
fn=submit_form,
|
| 61 |
inputs=["text", "text", "text"], # Fields: Name, Email, Phone
|
| 62 |
outputs="text",
|
| 63 |
title="Customer Login Page",
|
| 64 |
-
description="Enter your details to save them in Salesforce
|
| 65 |
)
|
| 66 |
|
| 67 |
# Launch the app
|
| 68 |
-
interface.launch()
|
|
|
|
| 7 |
SECURITY_TOKEN = "HIi7RyLwaqfYvvTLZvxrU8pU"
|
| 8 |
CONSUMER_KEY = "3MVG9GCMQoQ6rpzTiMF88fek3awTaWrIiDcs_.Cm22MYTnZGgyNwfvV8Zstde05XXgJlUDQBxUHgtC62on9e6"
|
| 9 |
CONSUMER_SECRET = "BE933C0B77021E853412938449543632636EE48301A12DDF786C87253B9C46F5"
|
| 10 |
+
LOGIN_URL = "https://login.salesforce.com/?locale=in"
|
| 11 |
|
| 12 |
+
# Authenticate and get Salesforce access token
|
| 13 |
def get_salesforce_access_token():
|
| 14 |
payload = {
|
| 15 |
+
"grant_type": "password",
|
| 16 |
+
"client_id": CONSUMER_KEY,
|
| 17 |
+
"client_secret": CONSUMER_SECRET,
|
| 18 |
+
"username": USERNAME,
|
| 19 |
+
"password": PASSWORD + SECURITY_TOKEN,
|
| 20 |
}
|
| 21 |
response = requests.post(LOGIN_URL, data=payload)
|
| 22 |
if response.status_code == 200:
|
| 23 |
data = response.json()
|
| 24 |
+
return data["access_token"], data["instance_url"]
|
| 25 |
else:
|
| 26 |
raise Exception(f"Authentication failed: {response.json()}")
|
| 27 |
|
| 28 |
+
# Function to save customer data to Salesforce
|
| 29 |
def save_customer_to_salesforce(name, email, phone):
|
| 30 |
try:
|
| 31 |
+
# Get Salesforce access token and instance URL
|
| 32 |
access_token, instance_url = get_salesforce_access_token()
|
| 33 |
headers = {
|
| 34 |
+
"Authorization": f"Bearer {access_token}",
|
| 35 |
+
"Content-Type": "application/json",
|
| 36 |
}
|
| 37 |
payload = {
|
| 38 |
+
"FirstName": name,
|
| 39 |
+
"LastName": "Customer", # Placeholder for last name
|
| 40 |
+
"Email": email,
|
| 41 |
+
"Phone": phone,
|
| 42 |
}
|
| 43 |
url = f"https://huggingface.co/spaces/nagasurendra/DynamicMenu1"
|
| 44 |
response = requests.post(url, headers=headers, json=payload)
|
|
|
|
| 45 |
if response.status_code == 201:
|
| 46 |
return "Customer details saved successfully!"
|
| 47 |
else:
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
return str(e)
|
| 51 |
|
| 52 |
+
# Gradio interface for input
|
| 53 |
def submit_form(name, email, phone):
|
| 54 |
if not name or not email or not phone:
|
| 55 |
+
return "All fields are required!"
|
| 56 |
return save_customer_to_salesforce(name, email, phone)
|
| 57 |
|
| 58 |
+
# Create Gradio interface
|
| 59 |
interface = gr.Interface(
|
| 60 |
fn=submit_form,
|
| 61 |
inputs=["text", "text", "text"], # Fields: Name, Email, Phone
|
| 62 |
outputs="text",
|
| 63 |
title="Customer Login Page",
|
| 64 |
+
description="Enter your details to save them in Salesforce."
|
| 65 |
)
|
| 66 |
|
| 67 |
# Launch the app
|
| 68 |
+
interface.launch(share=True)
|