Files
Whatsapp-Bridge-Integration/custom_components/whatsapp_bridge_integration/__init__.py

36 lines
1.5 KiB
Python

async def handle_send_message(call: ServiceCall):
message = call.data.get("message")
recipient = call.data.get("recipient")
use_manual = call.data.get("use_manual_number", False)
target_number = call.data.get("number")
entries = hass.config_entries.async_entries(DOMAIN)
targets = []
# Logik-Prüfung
if use_manual and target_number:
# Fall A: Manuelle Nummer
targets.append(target_number)
elif recipient == "all_accounts":
# Fall B: Rundruf
for e in entries:
targets.append(e.data.get(CONF_PHONE_NUMBER))
elif recipient:
# Fall C: Spezifisches Profil (Recipient Name)
for e in entries:
if e.title.lower() == recipient.lower():
targets.append(e.data.get(CONF_PHONE_NUMBER))
# Senden (wie gehabt)
for num in targets:
url = "http://866fd2eb-whatsapp-bridge:3000/send"
try:
await hass.async_add_executor_job(
lambda: requests.post(url, json={
"number": num,
"message": message
}, timeout=10)
)
_LOGGER.info("WhatsApp sent to %s", num)
except Exception as e:
_LOGGER.error("Error sending to %s: %s", num, str(e))