-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplatform_handlers.py
More file actions
628 lines (485 loc) · 22.5 KB
/
platform_handlers.py
File metadata and controls
628 lines (485 loc) · 22.5 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import logging
import time
import json
import re
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
logger = logging.getLogger(__name__)
class BasePlatformHandler(ABC):
"""Base class for all platform handlers"""
def __init__(self, credentials):
self.credentials = credentials
self.last_check = None
@abstractmethod
def get_messages(self, filters=None):
"""
Get messages from the platform
Args:
filters: Dictionary of filters to apply
Returns:
List of messages
"""
pass
@abstractmethod
def send_message(self, message):
"""
Send a message to the platform
Args:
message: Message to send
Returns:
True if successful, False otherwise
"""
pass
def cleanup(self):
"""Clean up resources"""
pass
class SeleniumBasedHandler(BasePlatformHandler):
"""Base class for platform handlers that use Selenium"""
def __init__(self, credentials):
super().__init__(credentials)
self.driver = None
self.initialize_driver()
def initialize_driver(self):
"""Initialize Selenium WebDriver"""
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920,1080")
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.implicitly_wait(10)
def cleanup(self):
"""Clean up WebDriver"""
if self.driver:
self.driver.quit()
self.driver = None
class TelegramHandler(SeleniumBasedHandler):
"""Handler for Telegram platform"""
def __init__(self, credentials):
super().__init__(credentials)
self._login()
def _login(self):
"""Login to Telegram Web"""
try:
self.driver.get("https://web.telegram.org/")
# Check if already logged in
if self._is_logged_in():
logger.info("Already logged in to Telegram")
return True
# Enter phone number
logger.info("Logging in to Telegram...")
phone_input = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='phone']"))
)
phone_input.send_keys(self.credentials.get("phone"))
# Click next
next_button = self.driver.find_element(By.CSS_SELECTOR, "button.btn-primary")
next_button.click()
# Wait for code input (manual step - notification will be shown)
logger.info("Waiting for user to enter verification code...")
# Here we would need manual intervention for the verification code
# In a real implementation, we might use a different approach like API-based authentication
# For demo purposes, we'll just wait a long time
WebDriverWait(self.driver, 300).until(self._is_logged_in)
logger.info("Successfully logged in to Telegram")
return True
except Exception as e:
logger.exception(f"Error logging in to Telegram: {str(e)}")
return False
def _is_logged_in(self):
"""Check if logged in to Telegram"""
try:
# Look for an element that is present when logged in
self.driver.find_element(By.CSS_SELECTOR, ".chat-list")
return True
except NoSuchElementException:
return False
def get_messages(self, filters=None):
"""Get messages from Telegram"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return []
messages = []
try:
# Navigate to specified chat if provided in filters
if filters and 'chat' in filters:
chat_name = filters['chat']
search_input = self.driver.find_element(By.CSS_SELECTOR, ".search-input input")
search_input.clear()
search_input.send_keys(chat_name)
time.sleep(2) # Wait for search results
chat_items = self.driver.find_elements(By.CSS_SELECTOR, ".chat-list .chat-item")
for chat in chat_items:
title = chat.find_element(By.CSS_SELECTOR, ".user-title").text
if title.lower() == chat_name.lower():
chat.click()
break
# Get message elements
message_elements = self.driver.find_elements(By.CSS_SELECTOR, ".message")
# Set last check time if not set
if not self.last_check:
self.last_check = datetime.now() - timedelta(hours=1) # Default to 1 hour ago
# Extract messages
for msg_elem in message_elements[-15:]: # Get latest 15 messages
try:
# Extract message text and time
text = msg_elem.find_element(By.CSS_SELECTOR, ".text-content").text
time_str = msg_elem.find_element(By.CSS_SELECTOR, ".time").text
# Apply keyword filters if specified
if filters and 'keywords' in filters:
keywords = filters['keywords']
if not any(keyword.lower() in text.lower() for keyword in keywords):
continue
messages.append(text)
except Exception as e:
logger.error(f"Error extracting message: {str(e)}")
# Update last check time
self.last_check = datetime.now()
except Exception as e:
logger.exception(f"Error getting messages from Telegram: {str(e)}")
return messages
def send_message(self, message):
"""Send a message on Telegram"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return False
try:
# Make sure chat is open and text input is available
message_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".composer .composer-input"))
)
# Enter message
message_input.clear()
message_input.send_keys(message)
# Send message
send_button = self.driver.find_element(By.CSS_SELECTOR, ".btn-send")
send_button.click()
# Wait for message to be sent
time.sleep(1)
return True
except Exception as e:
logger.exception(f"Error sending message on Telegram: {str(e)}")
return False
class WhatsAppHandler(SeleniumBasedHandler):
"""Handler for WhatsApp platform"""
def __init__(self, credentials):
super().__init__(credentials)
self._login()
def _login(self):
"""Login to WhatsApp Web"""
try:
self.driver.get("https://web.whatsapp.com/")
# Check if already logged in
if self._is_logged_in():
logger.info("Already logged in to WhatsApp")
return True
# Display QR code and wait for user to scan it
logger.info("Please scan the QR code to login to WhatsApp...")
# Wait for login to complete (10 minutes timeout)
WebDriverWait(self.driver, 600).until(self._is_logged_in)
logger.info("Successfully logged in to WhatsApp")
return True
except Exception as e:
logger.exception(f"Error logging in to WhatsApp: {str(e)}")
return False
def _is_logged_in(self):
"""Check if logged in to WhatsApp"""
try:
# Look for an element that is present when logged in
self.driver.find_element(By.CSS_SELECTOR, ".app")
return True
except NoSuchElementException:
return False
def get_messages(self, filters=None):
"""Get messages from WhatsApp"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return []
messages = []
try:
# Navigate to specified chat if provided in filters
if filters and 'chat' in filters:
chat_name = filters['chat']
search_input = self.driver.find_element(By.CSS_SELECTOR, ".copyable-text.selectable-text[data-tab='3']")
search_input.clear()
search_input.send_keys(chat_name)
time.sleep(2) # Wait for search results
chat_items = self.driver.find_elements(By.CSS_SELECTOR, ".chat")
for chat in chat_items:
title = chat.find_element(By.CSS_SELECTOR, ".chat-title span").text
if title.lower() == chat_name.lower():
chat.click()
break
# Get message elements
message_elements = self.driver.find_elements(By.CSS_SELECTOR, ".message-in, .message-out")
# Set last check time if not set
if not self.last_check:
self.last_check = datetime.now() - timedelta(hours=1) # Default to 1 hour ago
# Extract messages
for msg_elem in message_elements[-15:]: # Get latest 15 messages
try:
# Extract message text and time
text = msg_elem.find_element(By.CSS_SELECTOR, ".copyable-text").text
time_str = msg_elem.find_element(By.CSS_SELECTOR, ".copyable-text > .selectable-text").get_attribute("data-pre-plain-text")
# Apply keyword filters if specified
if filters and 'keywords' in filters:
keywords = filters['keywords']
if not any(keyword.lower() in text.lower() for keyword in keywords):
continue
messages.append(text)
except Exception as e:
logger.error(f"Error extracting message: {str(e)}")
# Update last check time
self.last_check = datetime.now()
except Exception as e:
logger.exception(f"Error getting messages from WhatsApp: {str(e)}")
return messages
def send_message(self, message):
"""Send a message on WhatsApp"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return False
try:
# Make sure chat is open and text input is available
message_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".copyable-text.selectable-text[data-tab='9']"))
)
# Enter message
message_input.clear()
message_input.send_keys(message)
# Send message
send_button = self.driver.find_element(By.CSS_SELECTOR, "span[data-icon='send']")
send_button.click()
# Wait for message to be sent
time.sleep(1)
return True
except Exception as e:
logger.exception(f"Error sending message on WhatsApp: {str(e)}")
return False
class SlackHandler(SeleniumBasedHandler):
"""Handler for Slack platform"""
def __init__(self, credentials):
super().__init__(credentials)
self._login()
def _login(self):
"""Login to Slack Web"""
try:
self.driver.get("https://slack.com/signin")
# Check if already logged in
if self._is_logged_in():
logger.info("Already logged in to Slack")
return True
# Enter email
logger.info("Logging in to Slack...")
email_input = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "input#email"))
)
email_input.send_keys(self.credentials.get("email"))
# Enter password
password_input = self.driver.find_element(By.CSS_SELECTOR, "input#password")
password_input.send_keys(self.credentials.get("password"))
# Click sign in button
sign_in_button = self.driver.find_element(By.CSS_SELECTOR, "button#signin_btn")
sign_in_button.click()
# Wait for login to complete
WebDriverWait(self.driver, 30).until(self._is_logged_in)
logger.info("Successfully logged in to Slack")
return True
except Exception as e:
logger.exception(f"Error logging in to Slack: {str(e)}")
return False
def _is_logged_in(self):
"""Check if logged in to Slack"""
try:
# Look for an element that is present when logged in
self.driver.find_element(By.CSS_SELECTOR, ".p-workspace__sidebar")
return True
except NoSuchElementException:
return False
def get_messages(self, filters=None):
"""Get messages from Slack"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return []
messages = []
try:
# Navigate to specified channel if provided in filters
if filters and 'channel' in filters:
channel_name = filters['channel']
channel_links = self.driver.find_elements(By.CSS_SELECTOR, ".p-channel_sidebar__channel a")
for link in channel_links:
if channel_name.lower() in link.text.lower():
link.click()
break
# Get message elements
message_elements = self.driver.find_elements(By.CSS_SELECTOR, ".c-message")
# Set last check time if not set
if not self.last_check:
self.last_check = datetime.now() - timedelta(hours=1) # Default to 1 hour ago
# Extract messages
for msg_elem in message_elements[-15:]: # Get latest 15 messages
try:
# Extract message text and time
text = msg_elem.find_element(By.CSS_SELECTOR, ".c-message__body").text
# Apply keyword filters if specified
if filters and 'keywords' in filters:
keywords = filters['keywords']
if not any(keyword.lower() in text.lower() for keyword in keywords):
continue
messages.append(text)
except Exception as e:
logger.error(f"Error extracting message: {str(e)}")
# Update last check time
self.last_check = datetime.now()
except Exception as e:
logger.exception(f"Error getting messages from Slack: {str(e)}")
return messages
def send_message(self, message):
"""Send a message on Slack"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return False
try:
# Make sure text input is available
message_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".ql-editor"))
)
# Enter message
message_input.clear()
message_input.send_keys(message)
# Send message (Enter key)
message_input.send_keys(u'\ue007') # Unicode for ENTER key
# Wait for message to be sent
time.sleep(1)
return True
except Exception as e:
logger.exception(f"Error sending message on Slack: {str(e)}")
return False
class DiscordHandler(SeleniumBasedHandler):
"""Handler for Discord platform"""
def __init__(self, credentials):
super().__init__(credentials)
self._login()
def _login(self):
"""Login to Discord Web"""
try:
self.driver.get("https://discord.com/login")
# Check if already logged in
if self._is_logged_in():
logger.info("Already logged in to Discord")
return True
# Enter email and password
logger.info("Logging in to Discord...")
email_input = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='email']"))
)
email_input.send_keys(self.credentials.get("email"))
password_input = self.driver.find_element(By.CSS_SELECTOR, "input[name='password']")
password_input.send_keys(self.credentials.get("password"))
# Click login button
login_button = self.driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()
# Wait for login to complete
WebDriverWait(self.driver, 30).until(self._is_logged_in)
logger.info("Successfully logged in to Discord")
return True
except Exception as e:
logger.exception(f"Error logging in to Discord: {str(e)}")
return False
def _is_logged_in(self):
"""Check if logged in to Discord"""
try:
# Look for an element that is present when logged in
self.driver.find_element(By.CSS_SELECTOR, ".guilds-1SWlCJ")
return True
except NoSuchElementException:
return False
def get_messages(self, filters=None):
"""Get messages from Discord"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return []
messages = []
try:
# Navigate to specified channel if provided in filters
if filters and 'channel' in filters:
# Would need to implement navigation to specific channel
pass
# Get message elements
message_elements = self.driver.find_elements(By.CSS_SELECTOR, ".message-2qnXI6")
# Set last check time if not set
if not self.last_check:
self.last_check = datetime.now() - timedelta(hours=1) # Default to 1 hour ago
# Extract messages
for msg_elem in message_elements[-15:]: # Get latest 15 messages
try:
# Extract message text
text = msg_elem.find_element(By.CSS_SELECTOR, ".messageContent-2qWWxC").text
# Apply keyword filters if specified
if filters and 'keywords' in filters:
keywords = filters['keywords']
if not any(keyword.lower() in text.lower() for keyword in keywords):
continue
messages.append(text)
except Exception as e:
logger.error(f"Error extracting message: {str(e)}")
# Update last check time
self.last_check = datetime.now()
except Exception as e:
logger.exception(f"Error getting messages from Discord: {str(e)}")
return messages
def send_message(self, message):
"""Send a message on Discord"""
if not self._is_logged_in():
self._login()
if not self._is_logged_in():
return False
try:
# Make sure text input is available
message_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div[data-slate-editor='true']"))
)
# Enter message
message_input.clear()
message_input.send_keys(message)
# Send message (Enter key)
message_input.send_keys(u'\ue007') # Unicode for ENTER key
# Wait for message to be sent
time.sleep(1)
return True
except Exception as e:
logger.exception(f"Error sending message on Discord: {str(e)}")
return False
def get_platform_handler(platform_type, credentials):
"""
Factory function to create a platform handler based on platform type
Args:
platform_type: Type of platform (telegram, whatsapp, slack, etc.)
credentials: Dictionary of credentials for the platform
Returns:
Platform handler instance
"""
handlers = {
'telegram': TelegramHandler,
'whatsapp': WhatsAppHandler,
'slack': SlackHandler,
'discord': DiscordHandler
}
if platform_type not in handlers:
raise ValueError(f"Unsupported platform type: {platform_type}")
return handlers[platform_type](credentials)