120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
import os
|
|
import json
|
|
import requests
|
|
|
|
# Load API Key
|
|
def get_gemini_key():
|
|
candidates = [
|
|
"gemini_api_key.txt", # Current dir
|
|
"/app/gemini_api_key.txt", # Docker default
|
|
os.path.join(os.path.dirname(__file__), "gemini_api_key.txt"), # Script dir
|
|
os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gemini_api_key.txt') # Parent dir
|
|
]
|
|
|
|
for path in candidates:
|
|
if os.path.exists(path):
|
|
try:
|
|
with open(path, 'r') as f:
|
|
return f.read().strip()
|
|
except:
|
|
pass
|
|
|
|
return os.getenv("GEMINI_API_KEY")
|
|
|
|
def generate_email_draft(lead_data, company_data, booking_link="https://outlook.office365.com/owa/calendar/RoboplanetGmbH@robo-planet.de/bookings/"):
|
|
"""
|
|
Generates a personalized sales email using Gemini API.
|
|
"""
|
|
api_key = get_gemini_key()
|
|
if not api_key:
|
|
return "Error: Gemini API Key not found."
|
|
|
|
# Extract Data
|
|
company_name = lead_data.get('company_name', 'Interessent')
|
|
contact_name = lead_data.get('contact_name', 'Damen und Herren')
|
|
|
|
# Metadata from Lead
|
|
meta = {}
|
|
if lead_data.get('lead_metadata'):
|
|
try:
|
|
meta = json.loads(lead_data.get('lead_metadata'))
|
|
except:
|
|
pass
|
|
|
|
area = meta.get('area', 'Unbekannte Fläche')
|
|
purpose = meta.get('purpose', 'Reinigung')
|
|
city = meta.get('city', '')
|
|
role = meta.get('role', 'Unbekannt')
|
|
|
|
# Data from Company Explorer
|
|
ce_summary = company_data.get('summary', 'Keine Details verfügbar.')
|
|
ce_vertical = company_data.get('vertical', 'Allgemein')
|
|
ce_website = company_data.get('website', '')
|
|
|
|
# Prompt Engineering
|
|
prompt = f"""
|
|
Du bist ein erfahrener Vertriebsexperte für Roboter-Reinigungslösungen bei Robo-Planet.
|
|
Deine Aufgabe ist es, eine Antwort-E-Mail auf eine Lead-Anfrage zu formulieren.
|
|
|
|
HINTERGRUND ZUM PROSPEKT (Aus Analyse):
|
|
- Firma: {company_name}
|
|
- Standort: {city}
|
|
- Branche/Vertical: {ce_vertical}
|
|
- Web-Zusammenfassung: {ce_summary}
|
|
|
|
ANSPRECHPARTNER:
|
|
- Name: {contact_name}
|
|
- Rolle/Position: {role} (WICHTIG: Nutze dieses Wissen für den Tonfall. Ein Geschäftsführer braucht Argumente zu ROI/Effizienz, ein Facility Manager zu Operativem/Handling.)
|
|
|
|
ANFRAGE-DETAILS (Vom Kunden):
|
|
- Reinigungsfläche: {area}
|
|
- Einsatzzweck: {purpose}
|
|
|
|
DEIN ZIEL:
|
|
Schreibe eine kurze, prägnante und wertschätzende E-Mail.
|
|
1. Bedanke dich für die Anfrage.
|
|
2. Zeige kurz, dass du verstanden hast, was die Firma macht.
|
|
3. Gehe auf die Fläche ({area}) ein.
|
|
- Wenn > 1000qm oder Industrie/Halle: Erwähne den "Puma M20" oder "Scrubber 75" als Kraftpaket.
|
|
- Wenn < 1000qm oder Büro/Praxis/Gastro: Erwähne den "Phantas" oder "Pudu CC1" als wendige Lösung.
|
|
- Wenn "Unbekannt": Stelle eine offene Frage zur Umgebung.
|
|
4. Call to Action: Schlage ein kurzes Beratungsgespräch vor.
|
|
5. Füge diesen Buchungslink ein: {booking_link}
|
|
|
|
TONALITÄT:
|
|
Professionell, hilfreich, auf den Punkt. Keine Marketing-Floskeln.
|
|
|
|
FORMAT:
|
|
Betreff: [Vorschlag für Betreff]
|
|
|
|
[E-Mail Text]
|
|
"""
|
|
|
|
# Call Gemini API
|
|
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={api_key}"
|
|
headers = {'Content-Type': 'application/json'}
|
|
payload = {
|
|
"contents": [{"parts": [{"text": prompt}]}]
|
|
}
|
|
|
|
try:
|
|
response = requests.post(url, headers=headers, json=payload)
|
|
response.raise_for_status()
|
|
result = response.json()
|
|
return result['candidates'][0]['content']['parts'][0]['text']
|
|
except Exception as e:
|
|
return f"Error generating draft: {str(e)}"
|
|
|
|
if __name__ == "__main__":
|
|
# Test Mock
|
|
mock_lead = {
|
|
"company_name": "Klinikum Test",
|
|
"contact_name": "Dr. Müller",
|
|
"lead_metadata": json.dumps({"area": "5000 qm", "purpose": "Desinfektion und Boden", "city": "Berlin"})
|
|
}
|
|
mock_company = {
|
|
"vertical": "Healthcare / Krankenhaus",
|
|
"summary": "Ein großes Klinikum der Maximalversorgung mit Fokus auf Kardiologie."
|
|
}
|
|
print(generate_email_draft(mock_lead, mock_company))
|