📜  Python| Whatsapp 生日机器人

📅  最后修改于: 2022-05-13 01:55:16.610000             🧑  作者: Mango

Python| Whatsapp 生日机器人

您是否曾经希望自动祝您的朋友生日快乐,或在预设时间自动向您的朋友(或任何 Whastapp 联系人!)发送一组消息,或通过在 whatsapp 上发送数千条随机文本来发送您的朋友!使用浏览器自动化,您可以完成所有这些工作,甚至更多!
首先,您必须安装这些:-
1) Selenium的Python绑定(浏览器自动化软件)

pip install selenium

2) Chrome 网络驱动程序
从此处下载 Chrome 驱动程序:Chromedriver 下载页面(选择您的特定版本)

机器人是如何做到的

该脚本使用 PySelenium 包打开一个 Chrome webdriver 窗口,在该窗口中完成所有任务。它检查当前日期和月份是否与 json 文件中的日期和月份匹配。如果是,则返回它的 'name' 属性,用于在 Whatsapp web 中查找相应的聊天(通过 xpath 查找)。然后脚本模拟点击聊天,打开它,在聊天框中输入消息并模拟点击发送按钮。

什么是 JSON?

JSON 代表 JavaScript 对象表示法。这是一种非常简单和轻便的数据存储方式。虽然它是从 JavaScript 派生的,但它是独立于语言的,并且在外观上类似于Python字典
查看这篇文章了解更多关于 JSON 的信息。
下面是实现

Python3
# get current date in required format
import datetime
 
# store the birthdates of your contacts
import json
  
from selenium import webdriver
  
# add a delay so that all elements of
# the webpage are loaded before proceeding
import time
 
# Global variable Do not use elsewhere
eleNM = None
 
# This function is just to return a
# string of the message required
def wish_birth(name):
    return "Happy Birthday " + name.split(" ")[0] + "!!"
 
# This function returns a list of values of some
# attribute based on conditions on two attributes from the JSON file.
# use to return names of contacts having their birthday on current date.
def getJsonData(file, attr_ret, attr1, attr2, attr_val1, attr_val2):
     
    # Load the file's data in 'data' variable
    data = json.load(file)
    retv =[]
 
    # If the attributes' value conditions are satisfied,
    # append the name into the list to be returned.
    for i in data:
        if(i[attr1]== attr_val1 and i[attr2]== attr_val2):
           retv.append(i[attr_ret])
    return retv
 
# Opening the JSON file (birthdays.json) in read only mode.
data_file = open("birthdays.json", "r")
namev =[]
print("Script Running")
 
# This will keep rerunning the part of
# the code from 'while True' to 'break'.
# use to keep waiting for the JSON function
# to return a non empty list.
# In practice, this function will keep rerunning at
# 11:59pm a day before the birthday and break out at 12:00am.
while True:
    try:
        # to get 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 allows us use the userdata of chrome
# so that you don't have to sign in manually everytime.
chropt = webdriver.ChromeOptions()
 
# adding userdata argument to ChromeOptions object
chropt.add_argument("user-data-")
 
# Creating a Chrome webdriver object
driver = webdriver.Chrome(executable_path ="",
                                                          options = chropt)
driver.get("https://web.whatsapp.com/")
 
# delay added to give time for all elements to load
time.sleep(10)
 
print(namev)
 
# Finds the chat of your contacts (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 the element
    eleNM.click()
 
    while(True):
        # Finds the chat box element
        eleTF = driver.find_element_by_class_name("_13mgZ")
        # Writes the message(function call to wish_birth())
        eleTF.send_keys(wish_birth(inp))
        # Finds the Send button
        eleSND = driver.find_element_by_class_name("_3M-N-")
        # Simulates a click on it
        eleSND.click()
        break


JSON 文件的外观

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

]