8
8
import json
9
9
import logging
10
10
import os
11
+ import random
11
12
import re
12
13
from datetime import datetime , timedelta
13
14
from pathlib import Path
@@ -26,6 +27,7 @@ def setup_environment(debug: bool):
26
27
parser = argparse .ArgumentParser (description = 'WhatsApp Scheduler - Send scheduled messages via WhatsApp Web' )
27
28
parser .add_argument ('--debug' , action = 'store_true' , help = 'Debug mode: show browser and verbose logs' )
28
29
parser .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' )
29
31
args = parser .parse_args ()
30
32
setup_environment (args .debug )
31
33
@@ -140,6 +142,36 @@ async def send_message(contact, message):
140
142
print (f'✅ Sent to { contact } ' )
141
143
142
144
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
+
143
175
async def main ():
144
176
if not GOOGLE_API_KEY :
145
177
print ('❌ Set GOOGLE_API_KEY or GEMINI_API_KEY environment variable' )
@@ -149,6 +181,29 @@ async def main():
149
181
print (f'Profile: { USER_DATA_DIR } ' )
150
182
print ()
151
183
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
+
152
207
# Parse messages
153
208
print ('Parsing messages.txt...' )
154
209
messages = await parse_messages ()
0 commit comments