Skip to content

Commit 3eeaf82

Browse files
authored
Feature: added auto mode to msg-use (#3107)
<!-- This is an auto-generated description by cubic. --> ## Summary by cubic Add auto mode (--auto) to msg-use to auto-reply to unread WhatsApp messages about every 30 minutes. Updates docs with usage. - **New Features** - --auto: opens the Unread tab, replies to each chat, and repeats every 30 ±5 minutes. - Respects --debug for a visible browser and saved session state. Graceful stop with Ctrl+C and simple retry on errors. - Added --auto usage examples to README and docs. <!-- End of auto-generated description by cubic. -->
2 parents b6d71b1 + 00c23ad commit 3eeaf82

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

docs/examples/apps/msg-use.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ python scheduler.py
7171
7272
# Debug Mode - See the browser in action
7373
python scheduler.py --debug
74+
75+
# Auto Mode - Respond to unread messages every ~30 minutes
76+
python scheduler.py --auto
7477
```
7578

7679
## Programmatic Usage

examples/apps/msg-use/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ python scheduler.py --test
5959
3. **Run scheduler**:
6060
```bash
6161
python scheduler.py
62+
63+
# Debug Mode - See the browser in action
64+
python scheduler.py --debug
65+
66+
# Auto Mode - Respond to unread messages every ~30 minutes
67+
python scheduler.py --auto
6268
```
6369

6470
## Programmatic Usage

examples/apps/msg-use/scheduler.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99
import logging
1010
import os
11+
import random
1112
import re
1213
from datetime import datetime, timedelta
1314
from pathlib import Path
@@ -26,6 +27,7 @@ def setup_environment(debug: bool):
2627
parser = argparse.ArgumentParser(description='WhatsApp Scheduler - Send scheduled messages via WhatsApp Web')
2728
parser.add_argument('--debug', action='store_true', help='Debug mode: show browser and verbose logs')
2829
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')
2931
args = parser.parse_args()
3032
setup_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('\nAuto-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+
143175
async 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\nAuto 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

Comments
 (0)