Deprecated: Creation of dynamic property Timber\Site::$pingback is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Site.php on line 180

Deprecated: Creation of dynamic property Timber\User::$name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/User.php on line 118

Deprecated: Creation of dynamic property Timber\Post::$has_disclaimer is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_lock is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_last is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_meta_box_present is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_send_notification is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_modify_title_and_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_heading is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_category is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_blz_affiliation_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_content_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_speciali is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_content_score is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskeywords is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_keywordsynonyms is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_estimated-reading-time-minutes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_wordproof_timestamp is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskw is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_metadesc is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_linkdex is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_thumbnail_id is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$response_body is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_indexnow_last_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_592afc7a62894e8243aeebad712fd95b is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_time_592afc7a62894e8243aeebad712fd95b is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$newsletter_subject is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_newsletter_subject is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$newsletter_summary is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_newsletter_summary is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_date_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$ping_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_password is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$to_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$pinged is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_content_filtered is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$guid is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$menu_order is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_mime_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_count is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$filter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71
Programmare invio messaggi WhatsApp senza app di terze parti | IlSoftware.it

Programmare invio messaggi WhatsApp senza app di terze parti

Abbiamo sviluppato uno script Python che permette di programmare l'invio di messaggi WhatsApp utilizzando Python e Selenium su un sistema Ubuntu in esecuzione su Windows Subsystem for Linux (WSL 2). Non sono necessarie applicazioni di terze parti e l'attività è gestita in locale.

WhatsApp rimane ad oggi la piattaforma di messaggistica istantanea più utilizzata al mondo. Programmare l’invio di messaggi WhatsApp può essere utile in diverse occasioni: si possono trasmettere aggiornamenti o avvisi ai membri di un team in orari specifici, migliorando la comunicazione all’interno di un’azienda o un gruppo di lavoro. Si può pianificare l’invio di comunicazioni in momenti strategici, come auguri di compleanno o promemoria di eventi, senza la necessità di inviarli manualmente all’ultimo minuto. Ancora, si possono automatizzare messaggi di marketing o invii promozionali.

Purtroppo però al momento WhatsApp non permette di programmare l’invio automatico dei messaggi. Alcune funzionalità sono esclusivamente prerogativa di WhatsApp Business ma anche in questo caso, l’applicazione si limita a gestire messaggi di benvenuto e di inattività.

Portandosi nelle impostazioni, scegliendo Strumenti attività, Messaggio di benvenuto, si può definire il testo da trasmettere agli interlocutori la prima volta o dopo 14 giorni di inattività. Ancora, gli utenti di WhatsApp Business possono eventualmente programmare i cosiddetti Messaggi d’assenza, utili nei periodi in cui non si possono fornire delle risposte.

Come programmare invio messaggi WhatsApp usando solo Python

Esistono alcune applicazioni che permettono di dialogare con WhatsApp e programmare l’invio di messaggi in automatico. Sono tuttavia app create da sviluppatori sconosciuti, che non godono di giudizi particolarmente brillanti e che, soprattutto, richiedono un gran numero di permessi per funzionare.

Ci siamo chiesti se fosse possibile programmare l’invio di messaggi WhatsApp usando soltanto codice Python, senza appoggiarsi a soluzioni sviluppate da terze parti.

Il codice che vi forniamo di seguito è uno spunto, che può essere facilmente riadattato alle proprie esigenze. Perfettamente funzionante, abbiamo deciso di utilizzarlo da una macchina Ubuntu Linux, in esecuzione all’interno di Windows Subsystem for Linux (WSL 2) su Windows 11.

Diamo per scontato che si sia provveduto ad aggiungere Ubuntu Linux come macchina WSL in Windows. Premendo Windows+X, quindi scegliendo Terminale (Admin) oppure Windows PowerShell (amministratore), impartito il comando wsl -l -v, al di sotto di Version deve comparire “2”.

Ancora, digitando wslg, deve apparire il messaggio Sottosistema Windows per Linux, con una serie di comandi: in questo modo si è sicuri che è possibile avviare le applicazioni Linux dotate di interfaccia grafica.

WSL Ubuntu: programmare messaggi WhatsApp

I requisiti per inviare messaggi WhatsApp da script Python

Nel nostro progetto, per programmare l’invio di messaggi WhatsApp con Python, utilizziamo alcune librerie e componenti software che devono essere installati manualmente. L’utilizzo di WSL in Windows ci aiuta a tenere separate le cose, senza modificare in alcun modo la configurazione del sistema operativo sottostante.

Il primo componente che usiamo nel nostro script Python si chiama Selenium. Si tratta di una suite di strumenti open source utilizzata per l’automazione dei browser Web. Permette di controllare un browser tramite codice, consentendo di eseguire vari tipi di operazioni: automatizzare attività (clic su pulsanti, inserimento di testo e navigazione tra le pagine) e raccogliere dati dai siti Web (Web scraping).

Ci avvaliamo inoltre di Chromedriver, un driver specifico sviluppato da Google che funge da intermediario tra Selenium e il browser. Con Chromedriver, Selenium può controllare Chrome interagire con la sua interfaccia utente. A proposito, con il codice che proponiamo di seguito, installiamo l’ultima versione stabile di Chrome per Linux.

I comandi da usare su Linux per predisporre l’ambiente di lavoro

Con i comandi proposti di seguito, effettuiamo in un colpo solo tutte le operazioni brevemente descritte in precedenza. Aggiorniamo Ubuntu, installiamo Python, il gestore di pacchetti pip, Selenium, Google Chrome, il software Unzip per decomprimere gli archivi compressi e la versione compatibile di Chromedriver:

# Aggiorna Ubuntu, installa Python, pip e Selenium
sudo apt update && sudo apt upgrade -y
sudo apt install python3 python3-pip -y
pip3 install selenium

# Aggiungi il repository di Google Chrome
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list

# Aggiorna il sistema e installa Google Chrome
sudo apt update
sudo apt install google-chrome-stable

# Installa Unzip
sudo apt install unzip -y

# Rileva la versione di Chrome installata e carica Chromedriver
CHROME_VERSION=$(google-chrome --version | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*')
wget https://storage.googleapis.com/chrome-for-testing-public/$CHROME_VERSION/linux64/chromedriver-linux64.zip
unzip chromedriver-linux64.zip
chmod +x chromedriver-linux64
sudo mv chromedriver-linux64 /usr/local/bin/

Lo script Python per programmare l’invio dei messaggi WhatsApp

Una volta predisposto l’ambiente di lavoro, abbiamo sviluppato un semplice script Python che utilizza WhatsApp Web per programmare l’invio dei messaggi ed eseguirlo alla data e ora specificate dall’utente.

Prendete questo script Python e salvatelo nella macchina Ubuntu con il nome wa.py.

Dopo l’importazione iniziale delle librerie necessaria, il codice configura il percorso del Chromedriver, necessario per controllare Chrome. Ai passaggi precedenti, Chromedriver è stato salvato nella cartella /usr/local/bin/chromedriver-linux64 quindi si conserva la stessa directory all’interno dello script Python.

Programmare invio messaggi WhatsApp

Stabilendo il percorso del profilo utente Chrome, si ha la possibilità di memorizzare il cookie di sessione utile per evitare che a ogni esecuzione dello script sia richiesta l’autenticazione WhatsApp mediante codice QR.

Utilizzando i nomi assegnati ai tag div nell’interfaccia di WhatsApp Web, riusciamo dapprima a cercare il nome dell’utente a cui si vuole inviare il messaggio quindi a predisporne l’invio alla data e ora indicate.

Per usare lo script non si deve fare altro che aprirlo con l’editor nano (nano wa.py) quindi modificare NOME_CONTATTO e la data e ora di invio del messaggio. Dopo aver digitato python3 wa.py, soltanto la prima volta è richiesto il superamento dell’autenticazione mediante codice QR.

Ulteriori possibilità di sviluppo e ottimizzazione

Il lavoro più grosso l’abbiamo già svolto. Adesso è facile modificare il codice Python, ad esempio, per ricevere in input i nomi dei contatti a cui mandare i messaggi e la data/ora d’invio direttamente dal prompt dei comandi.

Si può inoltre fare in modo che la schermata di Google Chrome, richiamato in formato grafico da Linux, non sia neppure mostrata. Per procedere in tal senso, basta richiamare il browser di Google con l’opzione headless:

options.add_argument('--headless')

Da tenere presente che se l’orario specificato nello script fosse antecedente a quello corrente, il messaggio sarà inviato subito.

Credit immagine in apertura: iStock.com – HStocks

Ti consigliamo anche

Link copiato negli appunti