tilburg-hub/evolution/welcome_bot.py
Atlas SHB a402cbaef5 Initial community site — AI HUB Tilburg
Zero-dependency static site. config.json-driven.
Deploy: docker compose up -d
Publish: GitHub Pages / Cloudflare Pages / Tailscale Serve

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-23 13:37:25 +02:00

90 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
AI HUB Tilburg — WhatsApp welcome bot via Evolution API.
Sends a welcome message when a new member joins the group.
Setup:
cp .env.example .env
# fill in your values
python3 welcome_bot.py --serve # starts a webhook server on :8765
"""
import os, json, sys, http.server, urllib.request, urllib.parse
EVO_URL = os.getenv("EVOLUTION_API_URL", "http://localhost:8120")
EVO_KEY = os.getenv("EVOLUTION_API_KEY", "")
INSTANCE = os.getenv("EVOLUTION_INSTANCE", "atlas")
GROUP_JID = os.getenv("WA_GROUP_JID", "") # 123456789@g.us
WELCOME_MESSAGE = """👋 Welkom bij AI HUB Tilburg!
We zijn een open community van builders, makers en denkers in de regio Tilburg. Geen leider, geen verplichtingen.
📍 We komen samen op zaterdag 12:0014:00 bij Business Centre Tilburg.
Vertel gerust wie je bent en wat je bouwt. Niemand beslist voor jou — jouw project is van jou.
Fijn dat je erbij bent! 🙌"""
def send_message(to_jid: str, text: str):
payload = json.dumps({"number": to_jid, "text": text}).encode()
req = urllib.request.Request(
f"{EVO_URL}/message/sendText/{INSTANCE}",
data=payload,
headers={"Content-Type": "application/json", "apikey": EVO_KEY},
method="POST"
)
try:
with urllib.request.urlopen(req, timeout=10) as r:
print(f"[bot] sent to {to_jid}: HTTP {r.status}")
return True
except Exception as e:
print(f"[bot] error: {e}")
return False
class WebhookHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(length)
self.send_response(200)
self.end_headers()
try:
event = json.loads(body)
except Exception:
return
# Evolution API group participant update event
event_type = event.get("event", "")
if event_type == "group-participants.update":
data = event.get("data", {})
action = data.get("action", "")
jid = data.get("id", "")
if action in ("add", "invite") and jid:
print(f"[bot] new member: {jid}")
# Welcome in group
send_message(GROUP_JID, WELCOME_MESSAGE)
# Or DM the new member directly:
# send_message(jid, WELCOME_MESSAGE)
def log_message(self, *args):
pass # suppress access logs
def main():
if "--serve" in sys.argv:
port = int(os.getenv("BOT_PORT", "8765"))
server = http.server.HTTPServer(("0.0.0.0", port), WebhookHandler)
print(f"[bot] listening on :{port}")
print(f"[bot] register this URL as Evolution webhook for instance '{INSTANCE}'")
server.serve_forever()
else:
print("Usage: python3 welcome_bot.py --serve")
print(" Then register http://YOUR_IP:8765 as a webhook in Evolution API")
if __name__ == "__main__":
main()