Spaces:
Runtime error
Runtime error
Create salesforce_dispatcher.py
Browse files
services/salesforce_dispatcher.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from simple_salesforce import Salesforce
|
| 2 |
+
from twilio.rest import Client
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
class SalesforceDispatcher:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
load_dotenv()
|
| 9 |
+
self.sf = Salesforce(
|
| 10 |
+
username=os.getenv("SF_USERNAME"),
|
| 11 |
+
password=os.getenv("SF_PASSWORD"),
|
| 12 |
+
security_token=os.getenv("SF_TOKEN")
|
| 13 |
+
)
|
| 14 |
+
self.twilio_client = Client(
|
| 15 |
+
os.getenv("TWILIO_SID"),
|
| 16 |
+
os.getenv("TWILIO_TOKEN")
|
| 17 |
+
)
|
| 18 |
+
self.twilio_number = os.getenv("TWILIO_NUMBER")
|
| 19 |
+
|
| 20 |
+
def create_case(self, subject, description):
|
| 21 |
+
"""Create a Salesforce case."""
|
| 22 |
+
case = self.sf.Case.create({
|
| 23 |
+
"Subject": subject,
|
| 24 |
+
"Description": description,
|
| 25 |
+
"Status": "New",
|
| 26 |
+
"Origin": "Web",
|
| 27 |
+
"Priority": "High"
|
| 28 |
+
})
|
| 29 |
+
return case["id"]
|
| 30 |
+
|
| 31 |
+
def send_email(self, to, subject, body):
|
| 32 |
+
"""Send email notification (simulated)."""
|
| 33 |
+
# Placeholder: Use Salesforce email service or external SMTP
|
| 34 |
+
print(f"Sending email to {to}\nSubject: {subject}\nBody: {body}")
|
| 35 |
+
|
| 36 |
+
def send_whatsapp(self, to, message):
|
| 37 |
+
"""Send WhatsApp message via Twilio."""
|
| 38 |
+
self.twilio_client.messages.create(
|
| 39 |
+
body=message,
|
| 40 |
+
from_=f"whatsapp:{self.twilio_number}",
|
| 41 |
+
to=f"whatsapp:{to}"
|
| 42 |
+
)
|