88import json
99import logging
1010import os
11+ import random
1112import re
1213from datetime import datetime , timedelta
1314from pathlib import Path
@@ -26,6 +27,7 @@ def setup_environment(debug: bool):
2627parser = argparse .ArgumentParser (description = 'WhatsApp Scheduler - Send scheduled messages via WhatsApp Web' )
2728parser .add_argument ('--debug' , action = 'store_true' , help = 'Debug mode: show browser and verbose logs' )
2829parser .add_argument ('--test' , action = 'store_true' , help = 'Test mode: show what messages would be sent without sending them' )
30+ parser .add_argument ('--auto' , action = 'store_true' , help = 'Auto mode: respond to unread messages every 30 minutes' )
2931args = parser .parse_args ()
3032setup_environment (args .debug )
3133
@@ -140,6 +142,36 @@ async def send_message(contact, message):
140142 print (f'✅ Sent to { contact } ' )
141143
142144
145+ async def auto_respond_to_unread ():
146+ """Click unread tab and respond to messages"""
147+ print ('\n Auto-responding to unread messages...' )
148+
149+ llm = ChatGoogle (model = 'gemini-2.0-flash-exp' , temperature = 0.3 , api_key = GOOGLE_API_KEY )
150+
151+ task = """
152+ 1. Go to https://web.whatsapp.com
153+ 2. Wait for page to load
154+ 3. Click on the "Unread" filter tab
155+ 4. If there are unread messages:
156+ - Click on each unread chat
157+ - Read the last message
158+ - Generate and send a friendly, contextual response
159+ - Move to next unread chat
160+ 5. Report how many messages were responded to
161+ """
162+
163+ browser = BrowserSession (
164+ headless = not args .debug ,
165+ user_data_dir = str (USER_DATA_DIR ),
166+ storage_state = str (STORAGE_STATE_FILE ) if STORAGE_STATE_FILE .exists () else None ,
167+ )
168+
169+ agent = Agent (task = task , llm = llm , browser_session = browser )
170+ result = await agent .run ()
171+ print ('✅ Auto-response complete' )
172+ return result
173+
174+
143175async def main ():
144176 if not GOOGLE_API_KEY :
145177 print ('❌ Set GOOGLE_API_KEY or GEMINI_API_KEY environment variable' )
@@ -149,6 +181,29 @@ async def main():
149181 print (f'Profile: { USER_DATA_DIR } ' )
150182 print ()
151183
184+ # Auto mode - respond to unread messages periodically
185+ if args .auto :
186+ print ('AUTO MODE - Responding to unread messages every ~30 minutes' )
187+ print ('Press Ctrl+C to stop.\n ' )
188+
189+ while True :
190+ try :
191+ await auto_respond_to_unread ()
192+
193+ # Wait 30 minutes +/- 5 minutes randomly
194+ wait_minutes = 30 + random .randint (- 5 , 5 )
195+ print (f'\n ⏰ Next check in { wait_minutes } minutes...' )
196+ await asyncio .sleep (wait_minutes * 60 )
197+
198+ except KeyboardInterrupt :
199+ print ('\n \n Auto mode stopped by user' )
200+ break
201+ except Exception as e :
202+ print (f'\n ❌ Error in auto mode: { e } ' )
203+ print ('Waiting 5 minutes before retry...' )
204+ await asyncio .sleep (300 )
205+ return
206+
152207 # Parse messages
153208 print ('Parsing messages.txt...' )
154209 messages = await parse_messages ()
0 commit comments