forked from Scottcjn/bottube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx_mention_monitor.py
More file actions
executable file
·479 lines (382 loc) · 18.2 KB
/
Copy pathx_mention_monitor.py
File metadata and controls
executable file
·479 lines (382 loc) · 18.2 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/env python3
"""
X Mention Monitor & Auto-Reply Assistant
Monitors @RustchainPOA mentions and alerts for fast replies.
The 75x algorithm boost requires quick author responses!
Features:
- Real-time mention monitoring
- Desktop notifications
- Suggested reply generation
- Reply posting with one command
- Conversation tracking
Usage:
python3 x_mention_monitor.py monitor # Watch for mentions
python3 x_mention_monitor.py reply <id> "text" # Quick reply
python3 x_mention_monitor.py pending # Show unreplied mentions
"""
import argparse
import json
import os
import sqlite3
import subprocess
import sys
import time
from datetime import datetime, timedelta
from pathlib import Path
import tweepy
# ═══════════════════════════════════════════════════════════════
# CONFIGURATION
# ═══════════════════════════════════════════════════════════════
# Credentials from environment variables
# Set: TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET,
# TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET, TWITTER_BEARER_TOKEN
DB_PATH = Path(__file__).parent / "x_mentions.db"
CHECK_INTERVAL = 60 # seconds between checks
ACCOUNT_USERNAME = "RustchainPOA"
# Sophia's personality for auto-generated replies
SOPHIA_PERSONALITY = """
Sophia is friendly, tech-savvy, enthusiastic about AI.
She uses emojis sparingly but effectively.
She asks follow-up questions to keep conversations going.
She's knowledgeable about BoTTube, AI agents, crypto, and tech.
"""
# Quick reply templates
REPLY_TEMPLATES = {
"thanks": "Thank you! 🙏 Really appreciate the support. What got you interested in AI-generated content?",
"welcome": "Welcome aboard! 🚀 Excited to have you. Any questions about getting started?",
"agree": "Exactly! 💯 You get it. What's your take on where this is all heading?",
"interesting": "That's a fascinating point! 🤔 Could you elaborate on that?",
"question": "Great question! ",
"cool": "Love this energy! 🔥 ",
"grok": "Hey @grok - thoughts on this? 🤖",
}
# ═══════════════════════════════════════════════════════════════
# DATABASE
# ═══════════════════════════════════════════════════════════════
def init_db():
"""Initialize SQLite database for tracking mentions."""
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS mentions (
id INTEGER PRIMARY KEY,
tweet_id TEXT UNIQUE,
author_id TEXT,
author_username TEXT,
text TEXT,
conversation_id TEXT,
created_at TEXT,
seen_at TEXT,
replied INTEGER DEFAULT 0,
replied_at TEXT,
reply_tweet_id TEXT
)
""")
c.execute("""
CREATE TABLE IF NOT EXISTS conversations (
id INTEGER PRIMARY KEY,
conversation_id TEXT UNIQUE,
started_at TEXT,
last_activity TEXT,
reply_count INTEGER DEFAULT 0,
our_reply_count INTEGER DEFAULT 0
)
""")
conn.commit()
conn.close()
def save_mention(tweet_id, author_id, author_username, text, conversation_id, created_at):
"""Save a mention to the database."""
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
try:
c.execute("""
INSERT OR IGNORE INTO mentions
(tweet_id, author_id, author_username, text, conversation_id, created_at, seen_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (tweet_id, author_id, author_username, text, conversation_id,
created_at, datetime.now().isoformat()))
conn.commit()
return c.rowcount > 0 # True if new mention
finally:
conn.close()
def mark_replied(tweet_id, reply_tweet_id):
"""Mark a mention as replied to."""
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
UPDATE mentions
SET replied = 1, replied_at = ?, reply_tweet_id = ?
WHERE tweet_id = ?
""", (datetime.now().isoformat(), reply_tweet_id, tweet_id))
conn.commit()
conn.close()
def get_pending_mentions():
"""Get mentions we haven't replied to yet."""
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
SELECT tweet_id, author_username, text, created_at, seen_at
FROM mentions
WHERE replied = 0
ORDER BY created_at DESC
""")
results = c.fetchall()
conn.close()
return results
# ═══════════════════════════════════════════════════════════════
# TWITTER CLIENT
# ═══════════════════════════════════════════════════════════════
def get_client():
"""Get authenticated Twitter client from environment variables."""
consumer_key = os.environ.get("TWITTER_CONSUMER_KEY")
consumer_secret = os.environ.get("TWITTER_CONSUMER_SECRET")
access_token = os.environ.get("TWITTER_ACCESS_TOKEN")
access_token_secret = os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")
bearer_token = os.environ.get("TWITTER_BEARER_TOKEN")
if not all([consumer_key, consumer_secret, access_token, access_token_secret]):
print("❌ Missing Twitter credentials. Set environment variables:")
print(" TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET")
print(" TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET")
print(" TWITTER_BEARER_TOKEN (optional, for search)")
sys.exit(1)
return tweepy.Client(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret,
bearer_token=bearer_token,
)
# ═══════════════════════════════════════════════════════════════
# NOTIFICATION
# ═══════════════════════════════════════════════════════════════
def send_notification(title: str, message: str):
"""Send desktop notification (Linux)."""
try:
subprocess.run([
"notify-send",
"-u", "critical",
"-t", "10000",
title,
message
], capture_output=True)
except:
pass # Notification not critical
# Also print with bell
print(f"\a") # Terminal bell
print(f"🔔 {title}")
print(f" {message}")
def generate_reply_suggestion(tweet_text: str) -> str:
"""Generate a suggested reply based on the tweet content."""
text_lower = tweet_text.lower()
# Check for common patterns and suggest replies
if any(word in text_lower for word in ["thank", "thanks", "appreciate"]):
return REPLY_TEMPLATES["thanks"]
if any(word in text_lower for word in ["joined", "signed up", "new here", "just found"]):
return REPLY_TEMPLATES["welcome"]
if any(word in text_lower for word in ["agree", "exactly", "right", "true", "yes"]):
return REPLY_TEMPLATES["agree"]
if "?" in tweet_text:
return REPLY_TEMPLATES["question"] + "[Answer their question + ask follow-up]"
if any(word in text_lower for word in ["cool", "awesome", "amazing", "love"]):
return REPLY_TEMPLATES["cool"] + "What aspect interests you most?"
if any(word in text_lower for word in ["grok", "@grok"]):
return "Great point! @grok what do you think about this? 🤖"
# Default: acknowledge and ask question
return "Interesting perspective! 🤔 What made you think of that?"
# ═══════════════════════════════════════════════════════════════
# MONITOR
# ═══════════════════════════════════════════════════════════════
def fetch_mentions(client, since_id=None):
"""Fetch recent mentions."""
try:
# Get mentions timeline
mentions = client.get_users_mentions(
id="1944928465121124352", # RustchainPOA user ID
max_results=10,
since_id=since_id,
tweet_fields=["created_at", "conversation_id", "author_id"],
expansions=["author_id"],
)
return mentions
except tweepy.TooManyRequests:
print("⚠️ Rate limited, waiting...")
return None
except Exception as e:
print(f"⚠️ Error fetching mentions: {e}")
return None
def monitor_mentions(duration_mins: int = None, continuous: bool = True):
"""
Monitor for new mentions and alert.
Args:
duration_mins: How long to monitor (None = forever)
continuous: Keep running or one-shot
"""
init_db()
client = get_client()
print("╔════════════════════════════════════════════════════════════╗")
print("║ X MENTION MONITOR - @RustchainPOA ║")
print("╚════════════════════════════════════════════════════════════╝")
print()
print(f"⏰ Checking every {CHECK_INTERVAL} seconds")
print(f"🎯 Goal: Reply within 15 mins for 75x algorithm boost!")
print()
print("Press Ctrl+C to stop\n")
print("-" * 60)
start_time = time.time()
last_seen_id = None
check_count = 0
while True:
check_count += 1
now = datetime.now().strftime("%H:%M:%S")
# Check duration limit
if duration_mins:
elapsed = (time.time() - start_time) / 60
if elapsed >= duration_mins:
print(f"\n⏱️ Duration reached ({duration_mins} mins). Stopping.")
break
print(f"[{now}] Check #{check_count}...", end=" ")
mentions = fetch_mentions(client, since_id=last_seen_id)
if mentions is None:
print("Rate limited, sleeping 60s")
time.sleep(60)
continue
if not mentions.data:
print("No new mentions")
else:
# Build user lookup
users = {}
if mentions.includes and "users" in mentions.includes:
for user in mentions.includes["users"]:
users[user.id] = user.username
new_count = 0
for tweet in mentions.data:
author_username = users.get(tweet.author_id, "unknown")
# Skip our own tweets
if author_username.lower() == ACCOUNT_USERNAME.lower():
continue
# Save and check if new
is_new = save_mention(
tweet_id=str(tweet.id),
author_id=str(tweet.author_id),
author_username=author_username,
text=tweet.text,
conversation_id=str(tweet.conversation_id) if tweet.conversation_id else None,
created_at=tweet.created_at.isoformat() if tweet.created_at else None,
)
if is_new:
new_count += 1
last_seen_id = max(last_seen_id or 0, int(tweet.id))
# Alert!
send_notification(
f"🔔 New mention from @{author_username}",
tweet.text[:100]
)
# Show details
print(f"\n{'='*60}")
print(f"🆕 NEW MENTION from @{author_username}")
print(f" {tweet.text}")
print(f"\n 💡 Suggested reply:")
suggestion = generate_reply_suggestion(tweet.text)
print(f" {suggestion}")
print(f"\n 📎 Quick reply:")
print(f" python3 x_mention_monitor.py reply {tweet.id} \"Your reply here\"")
print(f" 🔗 https://x.com/{author_username}/status/{tweet.id}")
print(f"{'='*60}\n")
if new_count > 0:
print(f"Found {new_count} new mention(s)!")
else:
print("No new mentions")
if not continuous:
break
time.sleep(CHECK_INTERVAL)
def quick_reply(tweet_id: str, reply_text: str):
"""Quickly reply to a mention."""
client = get_client()
print(f"\n🚀 Replying to tweet {tweet_id}...")
print(f" Text: {reply_text[:100]}...")
try:
response = client.create_tweet(
text=reply_text,
in_reply_to_tweet_id=tweet_id
)
reply_id = response.data['id']
print(f"\n✅ Reply posted!")
print(f" https://x.com/RustchainPOA/status/{reply_id}")
# Mark as replied in DB
mark_replied(tweet_id, str(reply_id))
return reply_id
except tweepy.TooManyRequests:
print("❌ Rate limited. Try again in a few minutes.")
except Exception as e:
print(f"❌ Error: {e}")
return None
def show_pending():
"""Show mentions we haven't replied to."""
init_db()
pending = get_pending_mentions()
print("\n╔════════════════════════════════════════════════════════════╗")
print("║ PENDING MENTIONS (Need Reply!) ║")
print("╚════════════════════════════════════════════════════════════╝\n")
if not pending:
print("🎉 All caught up! No pending mentions.")
return
print(f"⚠️ {len(pending)} mention(s) need replies:\n")
for tweet_id, username, text, created_at, seen_at in pending:
# Calculate age
if created_at:
created = datetime.fromisoformat(created_at.replace("Z", "+00:00"))
age_mins = (datetime.now(created.tzinfo) - created).total_seconds() / 60
age_str = f"{age_mins:.0f}m ago" if age_mins < 60 else f"{age_mins/60:.1f}h ago"
else:
age_str = "unknown"
print(f"📨 @{username} ({age_str})")
print(f" {text[:100]}{'...' if len(text) > 100 else ''}")
print(f" 💡 Suggest: {generate_reply_suggestion(text)[:60]}...")
print(f" 📎 python3 x_mention_monitor.py reply {tweet_id} \"reply\"")
print()
def show_templates():
"""Show available reply templates."""
print("\n╔════════════════════════════════════════════════════════════╗")
print("║ QUICK REPLY TEMPLATES ║")
print("╚════════════════════════════════════════════════════════════╝\n")
for name, template in REPLY_TEMPLATES.items():
print(f" {name}: {template[:60]}...")
print()
# ═══════════════════════════════════════════════════════════════
# CLI
# ═══════════════════════════════════════════════════════════════
def main():
parser = argparse.ArgumentParser(description="X Mention Monitor for @RustchainPOA")
subparsers = parser.add_subparsers(dest="command", help="Commands")
# Monitor command
mon_p = subparsers.add_parser("monitor", help="Monitor for new mentions")
mon_p.add_argument("--duration", "-d", type=int, help="Minutes to monitor")
mon_p.add_argument("--once", action="store_true", help="Check once and exit")
# Reply command
reply_p = subparsers.add_parser("reply", help="Quick reply to a mention")
reply_p.add_argument("tweet_id", help="Tweet ID to reply to")
reply_p.add_argument("text", help="Reply text")
# Pending command
subparsers.add_parser("pending", help="Show unreplied mentions")
# Templates command
subparsers.add_parser("templates", help="Show reply templates")
# Check command (one-shot)
subparsers.add_parser("check", help="Check once for new mentions")
args = parser.parse_args()
if args.command == "monitor":
monitor_mentions(
duration_mins=args.duration,
continuous=not args.once
)
elif args.command == "reply":
quick_reply(args.tweet_id, args.text)
elif args.command == "pending":
show_pending()
elif args.command == "templates":
show_templates()
elif args.command == "check":
monitor_mentions(continuous=False)
else:
parser.print_help()
if __name__ == "__main__":
main()