Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added image support & improved functionality #28

Open
wants to merge 1 commit into
base: primary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ Note: Another version of similar project is available which supports sending med
# Steps

1. Enter the message you want to send inside `message.txt` file.
2. Enter the list of numbers line-separated in `numbers.txt` file.
3. Run `python automator.py`.
4. Once the program starts, you'll see the message in message.txt and count of numbers in the numbers.txt file.
5. After a while, Chrome should pop-up and open web.whatsapp.com.
6. Scan the QR code to login into whatsapp.
7. Press `Enter` to start sending out messages.
8. Sit back and relax!
2. Want to send an image,use location=your/path at end of `message.txt`(optional)(only 1).
3. Enter the list of numbers line-separated in `numbers.txt` file.
4. Run `python automator.py`.
5. Once the program starts, you'll see the message in message.txt and count of numbers in the numbers.txt file.
6. After a while, Chrome should pop-up and open web.whatsapp.com.
7. Scan the QR code to login into whatsapp.
8. Press `Enter` to start sending out messages.
9. Sit back and relax!

### Funding

Expand Down
63 changes: 43 additions & 20 deletions automator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from time import sleep
from urllib.parse import quote
import os

options = Options()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_argument("--profile-directory=Default")
options.add_argument("--user-data-dir=/var/tmp/chrome_user_data")
## if cannot read from /var/tmp/chrome_user_data error uncomment below line
##options.add_argument("user-data-dir=C:\environments\selenium")
## If binary error occur uncomment this
##options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe"

os.system("")
os.environ["WDM_LOG_LEVEL"] = "0"
Expand Down Expand Up @@ -39,23 +43,24 @@ class style():
print("**********************************************************")
print(style.RESET)

f = open("message.txt", "r", encoding="utf8")
message = f.read()
f.close()

with open("message.txt", "r", encoding="utf8") as f:
message = f.read()
## if location is in text message it means you have to attach that image
if "location=" in message:
message_data = message.split("location=")
else:
message_data = [message]
print(style.YELLOW + '\nThis is your message-')
print(style.GREEN + message)
print(style.GREEN + message_data[0])
print("\n" + style.RESET)
message = quote(message)

numbers = []
f = open("numbers.txt", "r")
for line in f.read().splitlines():
if line.strip() != "":
numbers.append(line.strip())
f.close()
total_number=len(numbers)
print(style.RED + 'We found ' + str(total_number) + ' numbers in the file' + style.RESET)
with open("numbers.txt", "r") as f:
numbers.extend(
line.strip() for line in f.read().splitlines() if line.strip() != ""
)
total_number = len(numbers)
print(style.RED + 'We found ' + str(total_number) +
' numbers in the file' + style.RESET)
delay = 30

driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
Expand All @@ -68,21 +73,39 @@ class style():
continue
print(style.YELLOW + '{}/{} => Sending message to {}.'.format((idx+1), total_number, number) + style.RESET)
try:
url = 'https://web.whatsapp.com/send?phone=' + number + '&text=' + message
url = 'https://web.whatsapp.com/send?phone=' + number
sent = False
for i in range(3):
if not sent:
driver.get(url)
## First trying sending the image
if len(message_data)>1:
try:
click_attach = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div/div[5]/div/footer/div[1]/div/span[2]/div/div[1]/div[2]/div/div")))
click_attach.click()
upload_button = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div/div[5]/div/footer/div[1]/div/span[2]/div/div[1]/div[2]/div/span/div/div/ul/li[1]/button/input")))
upload_button.send_keys(message_data[1])
# button = driver.find_element(By.XPATH, "/html/body/div[1]/div/div/div[3]/div[2]/span/div/span/div/div/div[2]/div/div[2]/div[2]/div/div/span")
button = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div/div[3]/div[2]/span/div/span/div/div/div[2]/div/div[2]/div[2]/div/div/span")))
button.click()
print(style.GREEN + 'Image sent to: ' + number + style.RESET)
except Exception as e:
print(style.RED + 'Failed to send image to ' + number + str(e) + style.RESET)
## clicking on the message box and sending text message
try:
click_btn = WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-testid='compose-btn-send']")))
sleep(3)
type = WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div/div[5]/div/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]")))
type.send_keys(message_data[0].replace('\n', ' '))
type.send_keys(Keys.ENTER)
except Exception as e:
print(style.RED + f"\nFailed to send message to: {number}, retry ({i+1}/3)")
print(style.RED +
f"\nFailed to send message to: {number}, retry ({i+1}/3)")
print("Make sure your phone and computer is connected to the internet.")
print("If there is an alert, please dismiss it." + style.RESET)
else:
sleep(1)
click_btn.click()
sent=True
sent = True
type.send_keys(Keys.ENTER)
sleep(3)
print(style.GREEN + 'Message sent to: ' + number + style.RESET)
except Exception as e:
Expand Down
2 changes: 2 additions & 0 deletions message.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Hello World,
This is my text to you from automated messaging system.

Thank You

location=C:\Users\docto\Downloads\static_asset\image.png