PHP Classes

File: web_whatsapp_messages_reader.php

Recommend this page to a friend!
  Classes of Mr.shaggyy83   Web WhatsApp Messages Reader   web_whatsapp_messages_reader.php   Download  
File: web_whatsapp_messages_reader.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Web WhatsApp Messages Reader
Read messages from a WhatsApp user in the console
Author: By
Last change:
Date: 26 days ago
Size: 4,110 bytes
 

Contents

Class file image Download
<?php


$options
= getopt("l::d:n:");
$l = isset($options['l']) ? (int)$options['l'] : null;
$d = isset($options['d']) ? $options['d'] : null;
$n = $options['n'] ?? null;

if (!
$n) {
    die(
"The -n argument is required.\n");
}


require
'vendor/autoload.php';

use
Facebook\WebDriver\Chrome\ChromeOptions;
use
Facebook\WebDriver\Remote\DesiredCapabilities;
use
Facebook\WebDriver\Remote\RemoteWebDriver;

use
Facebook\WebDriver\WebDriverBy;

function
chrome_options() {
   
$options = new ChromeOptions();
   
$options->addArguments(["--user-data-dir=C:/Temp/ChromeProfile"]);
    return
$options;
}

// start webdriver
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, chrome_options());

$browser = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);

$browser->get('https://web.whatsapp.com');
$browser->manage()->window()->maximize();

echo
"Please Wait...\n";
sleep(25);

$user_name = $n;

try {
   
$user = $browser->findElement(WebDriverBy::xpath("//span[@title='{$user_name}']"));
   
$user->click();
    echo
"User Found\n";
   
sleep(15);

   
$messages = $browser->findElements(WebDriverBy::className('copyable-text'));
   
$myname_messages = [];

    if (
count($messages) > 0) {
        foreach (
$messages as $message) {
           
$message_text = $message->getText();
           
$time_stamp = $message->getAttribute('data-pre-plain-text');
            if (
$message_text !== null && $time_stamp !== null) {
               
$combined_message = "{$time_stamp} - {$message_text}";
                if (!
in_array($combined_message, $myname_messages)) {
                   
$myname_messages[] = $combined_message;
                }
            }
        }

       
$filter_date = null;
       
$filtered_records = [];
       
$current_format = (strpos(date('d.m.Y'), '.') !== false) ? 'd.m.Y' : 'd/m/Y';

        if (
$d) {
           
$filter_date = DateTime::createFromFormat($current_format, $d);
            if (!
$filter_date) {
                echo
"Date format is not correct.\n";
                exit;
            }
        }

       
// Get all messages
       
if ($l === null && $filter_date === null) {
            foreach (
$myname_messages as $record) {
                echo
$record . "\n";
               
$filtered_records[] = $record;
               
write_messages($filtered_records);
            }
        } else {
            if (
$l) {
               
$last_n = max($l, 1);
                echo
"Last {$last_n} messages:\n";
                foreach (
array_slice($myname_messages, -$last_n) as $record) {
                    echo
$record . "\n";
                   
$filtered_records[] = $record;
                   
write_messages($filtered_records);
                }
            }

            if (
$filter_date) {
                echo
"Messages after the date: " . $filter_date->format('d.m.Y') . "\n";
                foreach (
$myname_messages as $record) {
                   
$time_str = explode(' - ', $record)[0];

                    try {
                       
$time_str = substr($time_str, strpos($time_str, '[') + 1, strpos($time_str, ']') - strpos($time_str, '[') - 1);
                       
$message_date = DateTime::createFromFormat('H:i, d.m.Y', $time_str);
                        if (
$message_date > $filter_date) {
                            echo
$record . "\n";
                           
$filtered_records[] = $record;
                           
write_messages($filtered_records);
                        }
                    } catch (
Exception $e) {
                        echo
"Date format error: {$e->getMessage()}\n";
                    }
                }
            }
        }
    } else {
        echo
"Messages not found.\n";
    }
} catch (
Exception $e) {
    echo
"Error: {$e->getMessage()}\n";
}

$browser->quit();

// wait for qr code
sleep(35);

function
write_messages($messages) {
   

$file = fopen("wp_messages.txt", "w");
    if (
$file) {
    foreach (
$messages as $message) {
       
fwrite($file, $message . PHP_EOL);
    }
   
fclose($file);

}
}

?>