Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
@@ -21,13 +21,24 @@ def customerDetailsGsheets( objective,personal_details,industry,recipientRole,re
|
|
21 |
scopes = [
|
22 |
"https://www.googleapis.com/auth/spreadsheets"
|
23 |
]
|
|
|
24 |
json_string = os.getenv('credentials')
|
|
|
|
|
25 |
|
26 |
# Convert the string back to a JSON object
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
30 |
client = gspread.authorize(creds)
|
|
|
|
|
|
|
|
|
31 |
try:
|
32 |
workbook = client.open_by_key(sheet_id)
|
33 |
except gspread.SpreadsheetNotFound:
|
@@ -41,11 +52,12 @@ def customerDetailsGsheets( objective,personal_details,industry,recipientRole,re
|
|
41 |
|
42 |
# Append the customer details as a new row in the worksheet
|
43 |
if not (objective and personal_details):
|
44 |
-
return{
|
45 |
-
"response"
|
46 |
}
|
47 |
|
48 |
|
|
|
49 |
try:
|
50 |
sheet.append_row([objective, personal_details, industry, recipientRole, recipientName])
|
51 |
return {
|
|
|
21 |
scopes = [
|
22 |
"https://www.googleapis.com/auth/spreadsheets"
|
23 |
]
|
24 |
+
# Load the credentials from the environment variable
|
25 |
json_string = os.getenv('credentials')
|
26 |
+
if not json_string:
|
27 |
+
return {"response": "Credentials not found in environment variable.", "status": "error"}
|
28 |
|
29 |
# Convert the string back to a JSON object
|
30 |
+
try:
|
31 |
+
credentials = json.loads(json_string)
|
32 |
+
except json.JSONDecodeError:
|
33 |
+
return {"response": "Failed to decode JSON credentials.", "status": "error"}
|
34 |
|
35 |
+
# Create credentials from the JSON object
|
36 |
+
creds = Credentials.from_service_account_info(credentials, scopes=scopes)
|
37 |
client = gspread.authorize(creds)
|
38 |
+
|
39 |
+
# The sheet ID should also be stored in a secret or defined
|
40 |
+
sheet_id = os.getenv('sheet_id') # Example of getting sheet_id from environment variable
|
41 |
+
|
42 |
try:
|
43 |
workbook = client.open_by_key(sheet_id)
|
44 |
except gspread.SpreadsheetNotFound:
|
|
|
52 |
|
53 |
# Append the customer details as a new row in the worksheet
|
54 |
if not (objective and personal_details):
|
55 |
+
return {
|
56 |
+
"response": "Please enter the important details"
|
57 |
}
|
58 |
|
59 |
|
60 |
+
|
61 |
try:
|
62 |
sheet.append_row([objective, personal_details, industry, recipientRole, recipientName])
|
63 |
return {
|