geethareddy commited on
Commit
9eb0e55
·
verified ·
1 Parent(s): 4c8a79b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ # Salesforce Credentials
4
+ SALESFORCE_USERNAME = "geethareddy@sathkrutha.com"
5
+ SALESFORCE_PASSWORD = "7995604808g@"
6
+ SALESFORCE_SECURITY_TOKEN = " HIi7RyLwaqfYvvTLZvxrU8pU"
7
+ SALESFORCE_CONSUMER_KEY = "3MVG9GCMQoQ6rpzTiMF88fek3a99fPGaUP5aFXq.xKM6n2f.0asc.KLaQW4umn7PFXK55bh5HvD7lzN5NcHiV"
8
+ SALESFORCE_CONSUMER_SECRET = "C179C90625128C9B88C0275ED439E534CACE39CC129AB1977698C934583C7353"
9
+ SALESFORCE_LOGIN_URL = "https://login.salesforce.com/?locale=in"
10
+
11
+ # Authenticate to get Access Token
12
+ def get_salesforce_access_token():
13
+ payload = {
14
+ 'grant_type': 'password',
15
+ 'client_id': SALESFORCE_CONSUMER_KEY,
16
+ 'client_secret': SALESFORCE_CONSUMER_SECRET,
17
+ 'username': SALESFORCE_USERNAME,
18
+ 'password': SALESFORCE_PASSWORD + SALESFORCE_SECURITY_TOKEN,
19
+ }
20
+ response = requests.post(SALESFORCE_LOGIN_URL, data=payload)
21
+ if response.status_code == 200:
22
+ data = response.json()
23
+ return data['access_token'], data['instance_url']
24
+ else:
25
+ raise Exception(f"Failed to authenticate: {response.json()}")
26
+ def insert_customer(name, email, phone):
27
+ try:
28
+ access_token, instance_url = get_salesforce_access_token()
29
+ headers = {
30
+ 'Authorization': f'Bearer {access_token}',
31
+ 'Content-Type': 'application/json',
32
+ }
33
+ payload = {
34
+ 'FirstName': name,
35
+ 'LastName': 'Customer', # Placeholder
36
+ 'Email': email,
37
+ 'Phone': phone,
38
+ }
39
+ url = f"{instance_url}/services/data/vXX.X/sobjects/Contact/"
40
+ response = requests.post(url, headers=headers, json=payload)
41
+ if response.status_code == 201:
42
+ return "Customer record created successfully!"
43
+ else:
44
+ return f"Failed to create record: {response.json()}"
45
+ except Exception as e:
46
+ return str(e)
47
+ def collect_data(name, email, phone):
48
+ return insert_customer(name, email, phone)
49
+
50
+ interface = gr.Interface(
51
+ fn=collect_data,
52
+ inputs=["text", "text", "text"],
53
+ outputs="text",
54
+ title="Customer Details",
55
+ description="Enter your details to save them in Salesforce."
56
+ )
57
+
58
+ interface.launch()
59
+