[31388f42] Fix NameError in reply generator and implement free-mail quality detection
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import os
|
||||
import json
|
||||
import requests
|
||||
import sqlite3
|
||||
import re
|
||||
|
||||
# Load API Key
|
||||
def get_gemini_key():
|
||||
@@ -21,71 +23,145 @@ def get_gemini_key():
|
||||
|
||||
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/"):
|
||||
def get_matrix_context(industry_name, persona_name):
|
||||
"""Fetches Pains, Gains and Arguments from CE Database."""
|
||||
context = {
|
||||
"industry_pains": "",
|
||||
"industry_gains": "",
|
||||
"persona_description": "",
|
||||
"persona_arguments": ""
|
||||
}
|
||||
db_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'companies_v3_fixed_2.db')
|
||||
if not os.path.exists(db_path):
|
||||
return context
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
c = conn.cursor()
|
||||
|
||||
# Get Industry Data
|
||||
c.execute('SELECT pains, gains FROM industries WHERE name = ?', (industry_name,))
|
||||
ind_res = c.fetchone()
|
||||
if ind_res:
|
||||
context["industry_pains"], context["industry_gains"] = ind_res
|
||||
|
||||
# Get Persona Data
|
||||
c.execute('SELECT description, convincing_arguments FROM personas WHERE name = ?', (persona_name,))
|
||||
per_res = c.fetchone()
|
||||
if per_res:
|
||||
context["persona_description"], context["persona_arguments"] = per_res
|
||||
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print(f"DB Error in matrix lookup: {e}")
|
||||
|
||||
return context
|
||||
|
||||
def get_product_recommendation(area_str):
|
||||
"""
|
||||
Generates a personalized sales email using Gemini API.
|
||||
Selects the right robot based on the surface area mentioned in the lead.
|
||||
"""
|
||||
# Naive extraction of first number in the string
|
||||
nums = re.findall(r'\d+', area_str.replace('.', '').replace(',', ''))
|
||||
area_val = int(nums[0]) if nums else 0
|
||||
|
||||
if area_val >= 5000 or "über 10.000" in area_str:
|
||||
return {
|
||||
"name": "Scrubber 75",
|
||||
"reason": "als industrielles Kraftpaket für Großflächen ausgelegt",
|
||||
"usp": "höchste Effizienz und Autonomie auf mehreren tausend Quadratmetern"
|
||||
}
|
||||
elif area_val >= 1000:
|
||||
return {
|
||||
"name": "Scrubber 50 oder Phantas",
|
||||
"reason": "die optimale Balance zwischen Reinigungsleistung und Wendigkeit",
|
||||
"usp": "ideal für mittelgroße Fertigungs- und Lagerbereiche"
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"name": "Phantas oder Pudu CC1",
|
||||
"reason": "kompakt und wendig für komplexe Umgebungen",
|
||||
"usp": "perfekt für Büros, Praxen oder engere Verkehrswege"
|
||||
}
|
||||
|
||||
def generate_email_draft(lead_data, company_data, booking_link="[IHR BUCHUNGSLINK - BITTE IN .ENV EINTRAGEN]"):
|
||||
"""
|
||||
Generates a high-end, personalized sales email using Gemini API and Matrix knowledge.
|
||||
"""
|
||||
api_key = get_gemini_key()
|
||||
if not api_key:
|
||||
return "Error: Gemini API Key not found."
|
||||
|
||||
# Extract Data
|
||||
# Extract Data from Lead Engine
|
||||
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
|
||||
try: meta = json.loads(lead_data['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')
|
||||
role = meta.get('role', 'Wirtschaftlicher Entscheider')
|
||||
|
||||
# Data from Company Explorer
|
||||
ce_summary = company_data.get('research_dossier') or company_data.get('summary', 'Keine Details verfügbar.')
|
||||
ce_vertical = company_data.get('industry_ai') or company_data.get('vertical', 'Allgemein')
|
||||
ce_website = company_data.get('website', '')
|
||||
ce_summary = company_data.get('research_dossier') or company_data.get('summary', '')
|
||||
ce_vertical = company_data.get('industry_ai') or company_data.get('vertical', 'Industry - Manufacturing')
|
||||
ce_opener = company_data.get('ai_opener', '')
|
||||
|
||||
# Prompt Engineering
|
||||
# Product logic
|
||||
product = get_product_recommendation(area)
|
||||
|
||||
# Fetch "Golden Records" from Matrix
|
||||
matrix = get_matrix_context(ce_vertical, role)
|
||||
|
||||
# Prompt Engineering for "Unwiderstehliche E-Mail"
|
||||
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):
|
||||
Du bist ein Senior Sales Executive bei Robo-Planet. Antworte auf eine Anfrage von Tradingtwins.
|
||||
Schreibe eine E-Mail auf "Human Expert Level".
|
||||
|
||||
WICHTIGE STRATEGIE:
|
||||
- Starte NICHT mit seiner Position (CFO). Starte mit der Wertschätzung für sein UNTERNEHMEN ({company_name}).
|
||||
- Der Empfänger soll durch die Tiefe der Argumente MERKEN, dass wir für einen Entscheider schreiben.
|
||||
- Mappe ihn erst später als "finanziellen/wirtschaftlichen Entscheider".
|
||||
- Erwähne eine ROI-Perspektive (Amortisation).
|
||||
|
||||
KONTEXT (Vom Company Explorer):
|
||||
- Firma: {company_name}
|
||||
- Standort: {city}
|
||||
- Branche/Vertical: {ce_vertical}
|
||||
- Web-Zusammenfassung: {ce_summary}
|
||||
- Branche: {ce_vertical}
|
||||
- Branchen-Pains (Nutze diese für die Argumentation): {matrix['industry_pains']}
|
||||
- Branchen-Gains: {matrix['industry_gains']}
|
||||
- Dossier/Business-Profil: {ce_summary}
|
||||
- Strategischer Aufhänger: {ce_opener}
|
||||
|
||||
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.)
|
||||
- Rolle: {role}
|
||||
|
||||
PRODUKT-EMPFEHLUNG (Basierend auf Fläche {area}):
|
||||
- Modell: {product['name']}
|
||||
- Warum: {product['reason']}
|
||||
- USP: {product['usp']}
|
||||
|
||||
ANFRAGE-DETAILS (Vom Kunden):
|
||||
- Reinigungsfläche: {area}
|
||||
- Einsatzzweck: {purpose}
|
||||
ANFRAGE-DETAILS:
|
||||
- Bedarf: {area}
|
||||
- Zweck: {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}
|
||||
AUFGABE:
|
||||
Schreibe eine E-Mail mit dieser Struktur:
|
||||
1. EINSTIEG: Fokus auf Klemm Bohrtechnik und deren Marktstellung/Produkte (Bezug auf den 'Strategischen Aufhänger').
|
||||
2. DIE BRÜCKE: Verknüpfe die Präzision ihrer Produkte mit der Notwendigkeit von sauberen Hallenböden (besonders bei {area}). Nutze den Schmerzpunkt "Prozesssicherheit/Sensorik".
|
||||
3. DIE LÖSUNG: Positioniere den {product['name']} als genau die richtige Wahl für diese Größenordnung ({area}).
|
||||
4. ROI-LOGIK: Sprich ihn als wirtschaftlichen Entscheider an. Erwähne, dass wir für solche Projekte ROI-Kalkulationen erstellen, die oft eine Amortisation in unter 18-24 Monaten zeigen.
|
||||
5. CALL TO ACTION: Beratungsgespräch + Buchungslink: {booking_link}
|
||||
|
||||
TONALITÄT:
|
||||
Professionell, hilfreich, auf den Punkt. Keine Marketing-Floskeln.
|
||||
STIL:
|
||||
Senior, Augenhöhe, keine Floskeln, extrem fokussiert auf Effizienz und Qualität.
|
||||
|
||||
FORMAT:
|
||||
Betreff: [Vorschlag für Betreff]
|
||||
Betreff: [Relevanter Betreff, der direkt auf Klemm Bohrtechnik / Effizienz zielt]
|
||||
|
||||
[E-Mail Text]
|
||||
"""
|
||||
@@ -93,9 +169,7 @@ def generate_email_draft(lead_data, company_data, booking_link="https://outlook.
|
||||
# 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}]}]
|
||||
}
|
||||
payload = {"contents": [{"parts": [{"text": prompt}]}]}
|
||||
|
||||
try:
|
||||
response = requests.post(url, headers=headers, json=payload)
|
||||
@@ -116,4 +190,4 @@ if __name__ == "__main__":
|
||||
"vertical": "Healthcare / Krankenhaus",
|
||||
"summary": "Ein großes Klinikum der Maximalversorgung mit Fokus auf Kardiologie."
|
||||
}
|
||||
print(generate_email_draft(mock_lead, mock_company))
|
||||
print(generate_email_draft(mock_lead, mock_company))
|
||||
Reference in New Issue
Block a user