Change language

Python | Whatsapp birthday bot

First you must install these: — 
1) Python Bindings for Selenium (Browser Automation Software)

 pip install selenium 

2) Chrome Webdriver
Download the Chrome driver here: Chromedriver download page (select specific version)

How the bot does it

The script uses the PySelenium package to open the Chrome web driver window, where all tasks are performed. It checks if the current date and month matches the values ​​in the json file. If so, its name attribute is returned, which is used to search for the corresponding chat on the WhatsApp network (search by this article for more See JSON information.

Below is the implementation

# get the current date in the required format

import datetime

 
# store the dates of birth of your contacts

import json

 

from selenium import webdriver

 
 # add a delay so that all elements
# web page loads before proceeding

import time

 
# Global variable Do not use elsewhere

eleNM = None  

  
# This function is just to return
# message string required

def wish_birth (name ):

return "Happy Birthday " + name.split ( " " ) [ 0 ] + "!!"

 
# This function returns a list of values ​​for some
# attribute based subject to two attributes from the JSON file.
# use to return the names of contacts who have a birthday on the current date.

def getJsonData ( file , attr_ret, attr1, attr2, attr_val1, attr_val2):

 

  # Load file data into data variable

data = json.load ( file )

  retv = []

 

  # If the attribute value conditions are met

  # add name to list to return.

for i in data:

if (i [attr1] = = attr_val1 and i [attr2] = = attr_val2):

  retv.append (i [attr_ret])

return retv

 
# Opens the JSON file (birthdays.json) in read-only mode.

data_file = open ( "birthdays.json " , " r " )

namev = []

print ( "Script Running" )

 
# This will repeat the part
# code from & # 39; while True & # 39; before & # 39; break & # 39 ;.
# use to wait for the JSON function
# to return a non-empty list.
# In practice this function will be repeated when
# 11:59 pm on day before birthday and flare up at 12:00.

while True :

try :

  # to get the current date

datt = datetime. datetime.now ()

namev = getJsonData (data_file, " name " , " birth_month " , "birth_date" ,

str (datt.month), str (datt.day))

 

  except json.decoder.JSONDecodeError:

continue

if (namev! = []):

break

 
# ChromeOptions n Allows us to use custom chrome data
# so you don’t have to log in manually each time.

chropt = webdriver.ChromeOptions ()

 
# add the userdata argument to the ChromeOptions

chropt.add_argument ( "user-data- "LOCATION TO YOUR CHROME USER DATA"" )

 
# Create a Chrome Webdriver object

driver = webdriver.Chrome (executable_path = ""LOCATION TO CHROME WEBDRIVER""

  options = chropt)

driver.get ( " https://web.whatsapp.com/ " )

 
# a delay added to allow time for all items to load

time.sleep ( 10 )

  

print (namev)

 
# Find your contacts’ chat (as in the namev list)

for inp in namev:

  try :

  eleNM = driver.find_element_by_xpath ( ’ // span [@title = "{}"] ’ . format (inp))

except Exception as ex:

  print (ex)

  continue

# Simulates a mouse click on an element

eleNM.click ()

 

< code class = "keyword"> while ( True ):

# Finds a chat element

  eleTF = driver.find_element_by_class_name ( "_ 13mgZ" )

# Writes a message (call to the wish_birth () function)

eleTF. send_keys (wish_birth (inp))

# Finds the Submit button

eleSND = driver.find_element_by_class_name ( "_ 3M-N-" )

# Simulates clicking on it

eleSND.click ()

break

What the JSON file looks like

 [{"name": "NAME1", "birth_month": "1", "birth_date": "12"}, {" name ":" NAME2 "," birth_month ":" 5 "," birth_date ":" 15 "}] 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically