Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (69 loc) · 2.79 KB
/
Copy pathmain.py
File metadata and controls
92 lines (69 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""WhatsApp Mom Auto-Reply Agent
Only replies to mom, uses Gemini for short calm replies.
"""
import time
import logging
import os
from dotenv import load_dotenv
load_dotenv()
CONTACTS = [
os.getenv("MOM_CONTACT_NAME", "Sujathamma"),
os.getenv("FRIEND_CONTACT_NAME", "Sharmishta"),
]
CHECK_INTERVAL = int(os.getenv("CHECK_INTERVAL_SECONDS", "15"))
from whatsapp import get_driver, load_whatsapp, open_chat, get_last_message, send_message
from ai import generate_reply
from filters import already_replied, is_duplicate, sanitize_reply, mark_replied, is_safe
logging.basicConfig(level=logging.INFO)
def main():
driver = None
try:
driver = get_driver()
ok = load_whatsapp(driver)
if not ok:
logging.error("WhatsApp failed to load")
return
logging.info("Watching contacts: %s", CONTACTS)
while True:
for contact in CONTACTS:
try:
if not open_chat(driver, contact):
continue
msg = get_last_message(driver)
if not msg:
continue
msg_id, is_from_me, text = msg
if not text or is_from_me:
continue
if already_replied(msg_id):
continue
if is_duplicate(text):
logging.info("Duplicate detected; marking replied for %s", contact)
mark_replied(msg_id, text)
continue
reply = generate_reply(text, contact=contact)
contact_type = "mom" if "sujathamma" in contact.lower() or "mom" in contact.lower() else "friend"
safe_reply = sanitize_reply(reply, contact_type=contact_type)
# For mom, double-check safety; friends use looser rules already
if contact_type == "mom" and not is_safe(safe_reply):
logging.info("Reply not safe; using fallback for %s", contact)
safe_reply = "phone was on silent <3"
sent = send_message(driver, safe_reply)
if sent:
mark_replied(msg_id, text)
logging.info("[Agent → %s] %s", contact, safe_reply)
except Exception as e:
logging.exception("Error handling contact %s: %s", contact, e)
time.sleep(CHECK_INTERVAL)
except KeyboardInterrupt:
logging.info("Shutting down")
except Exception as e:
logging.exception("main loop error: %s", e)
finally:
if driver:
try:
driver.quit()
except Exception:
pass
if __name__ == "__main__":
main()